mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
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.
This commit is contained in:
1 parent
5143e12e48
commit
51871842fd
3 files changed
+35
-3
No files matched your search
@@ -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})
|
||||
|
||||
+23
-3
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user