From 1de0f3fa47c796b30659836b584dc3a16c7ac015 Mon Sep 17 00:00:00 2001 From: Jason Xu Date: Sun, 25 Oct 2026 07:55:40 +0800 Subject: [PATCH] fix(warp): wait out a rate limit during the first registration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Startup registers the first exit synchronously and failed closed on any error, including Cloudflare's rate limit — which a fresh process can still receive because the limit follows the source address. Wait out the pause inside the initialization deadline instead, and give WARP initialization one full rate-limit window so a deploy does not fail on a limit that is about to expire. --- internal/gateway/runtime.go | 7 +++++++ internal/warp/pool.go | 26 +++++++++++++++++++++++--- internal/warp/registrar.go | 5 +++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/gateway/runtime.go b/internal/gateway/runtime.go index 34f9722..1c68bc5 100644 --- a/internal/gateway/runtime.go +++ b/internal/gateway/runtime.go @@ -241,6 +241,13 @@ func (m *RuntimeController) selectWARP(cfg config.Config) (*warp.Pool, string, b if timeout <= 0 || timeout > 2*time.Minute { timeout = 2 * time.Minute } + // The first registration is synchronous: it decides whether the runtime can + // serve free traffic at all. Cloudflare can answer it with a rate limit even + // on a fresh process, so give initialization room for one full window + // instead of failing startup on a pause that is about to end. + if timeout < warp.RegistrationWindow+30*time.Second { + timeout = warp.RegistrationWindow + 30*time.Second + } ctx, cancel := context.WithTimeout(m.root, timeout) defer cancel() pool, err := warp.NewWithOptions(ctx, m.logger, warp.Options{Size: cfg.Warp.PoolSize, Stack: warp.Stack(cfg.Warp.Stack), SourceIPv6: source}) diff --git a/internal/warp/pool.go b/internal/warp/pool.go index ce1f900..d725b61 100644 --- a/internal/warp/pool.go +++ b/internal/warp/pool.go @@ -237,10 +237,30 @@ func newPool(ctx context.Context, log logger, size int, create func(context.Cont cooling: make(map[string]time.Time), changed: make(chan struct{}), } + // The first registration gates readiness. Cloudflare can answer it with a + // rate limit even when this process has just started (the limit follows the + // source address), so wait out that window inside the caller's deadline + // instead of failing startup over a pause that is about to end. Any other + // failure still fails closed. first, err := p.createExit(ctx) - if err != nil { - cancel() - return nil, err + for err != nil { + pause := registrationPause(err) + if pause <= 0 { + cancel() + return nil, err + } + if log != nil { + log.Infof("OpenCode WARP first registration paused for %s", pause.Round(time.Second)) + } + timer := time.NewTimer(pause) + select { + case <-ctx.Done(): + timer.Stop() + cancel() + return nil, ctx.Err() + case <-timer.C: + } + first, err = p.createExit(ctx) } p.active = []*exit{first} if log != nil { diff --git a/internal/warp/registrar.go b/internal/warp/registrar.go index d73ef96..1dcfe0f 100644 --- a/internal/warp/registrar.go +++ b/internal/warp/registrar.go @@ -30,6 +30,11 @@ const ( maxRateLimitPause = 5 * time.Minute ) +// RegistrationWindow is the longest pause Cloudflare's registration rate limit +// can impose: a caller that needs a first exit before it can serve waits out one +// full window rather than failing on a pause that is about to end. +const RegistrationWindow = maxRateLimitPause + // registrationPausedError reports that Cloudflare asked for a pause before // the next device registration. The pool schedules its retry from it instead // of sleeping inside the registration path.