Skip to content

Commit 5187184

Browse files
committed
fix(warp): wait out a rate limit during the first registration
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.
1 parent 5143e12 commit 5187184

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎internal/gateway/runtime.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ func (m *RuntimeController) selectWARP(cfg config.Config) (*warp.Pool, string, b
241241
if timeout <= 0 || timeout > 2*time.Minute {
242242
timeout = 2 * time.Minute
243243
}
244+
// The first registration is synchronous: it decides whether the runtime can
245+
// serve free traffic at all. Cloudflare can answer it with a rate limit even
246+
// on a fresh process, so give initialization room for one full window
247+
// instead of failing startup on a pause that is about to end.
248+
if timeout < warp.RegistrationWindow+30*time.Second {
249+
timeout = warp.RegistrationWindow + 30*time.Second
250+
}
244251
ctx, cancel := context.WithTimeout(m.root, timeout)
245252
defer cancel()
246253
pool, err := warp.NewWithOptions(ctx, m.logger, warp.Options{Size: cfg.Warp.PoolSize, Stack: warp.Stack(cfg.Warp.Stack), SourceIPv6: source})

‎internal/warp/pool.go‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,30 @@ func newPool(ctx context.Context, log logger, size int, create func(context.Cont
237237
cooling: make(map[string]time.Time),
238238
changed: make(chan struct{}),
239239
}
240+
// The first registration gates readiness. Cloudflare can answer it with a
241+
// rate limit even when this process has just started (the limit follows the
242+
// source address), so wait out that window inside the caller's deadline
243+
// instead of failing startup over a pause that is about to end. Any other
244+
// failure still fails closed.
240245
first, err := p.createExit(ctx)
241-
if err != nil {
242-
cancel()
243-
return nil, err
246+
for err != nil {
247+
pause := registrationPause(err)
248+
if pause <= 0 {
249+
cancel()
250+
return nil, err
251+
}
252+
if log != nil {
253+
log.Infof("OpenCode WARP first registration paused for %s", pause.Round(time.Second))
254+
}
255+
timer := time.NewTimer(pause)
256+
select {
257+
case <-ctx.Done():
258+
timer.Stop()
259+
cancel()
260+
return nil, ctx.Err()
261+
case <-timer.C:
262+
}
263+
first, err = p.createExit(ctx)
244264
}
245265
p.active = []*exit{first}
246266
if log != nil {

‎internal/warp/registrar.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const (
3030
maxRateLimitPause = 5 * time.Minute
3131
)
3232

33+
// RegistrationWindow is the longest pause Cloudflare's registration rate limit
34+
// can impose: a caller that needs a first exit before it can serve waits out one
35+
// full window rather than failing on a pause that is about to end.
36+
const RegistrationWindow = maxRateLimitPause
37+
3338
// registrationPausedError reports that Cloudflare asked for a pause before
3439
// the next device registration. The pool schedules its retry from it instead
3540
// of sleeping inside the registration path.

0 commit comments

Comments
 (0)