feat(warp): preload half the pool and register without a fixed gap

Registrations were paced by a fixed 12-second gap, which made a cold pool
and a 429 replacement wait minutes for capacity. Remove the gap: the
registrar now issues registrations back to back in the background,
serialized by the pool's create mutex, and only a rejected attempt backs
off (doJSON still honours Retry-After).

Keep half of warp.pool_size (at least one) hot as a preload: the first
exit still gates readiness, the rest register in the background, and
desiredLocked/scaleDown treat the preload as a floor so idle surplus is
still retired after 60 seconds but the warm bench survives. A 429 or 403
removes the exit from the ring and the next request uses a preloaded exit
immediately.

/healthz reports warp.preload, and the WebUI and all four READMEs
describe the behaviour.
This commit is contained in:
Jason Xu committed 2026-10-25 07:42:10 +08:00
1 parent b8b1454a0e
commit 7d34bdde33
9 files changed
+51 -45

No files matched your search

+2
View File
@@ -54,6 +54,7 @@ type healthWarp struct {
Status string `json:"status"`
Stack string `json:"stack"`
Size int `json:"size"`
Preload int `json:"preload"`
Active int `json:"active"`
Standby int `json:"standby"`
Cooling int `json:"cooling"`
@@ -79,6 +80,7 @@ func (g *Gateway) handleHealth(w http.ResponseWriter, _ *http.Request) {
Status: "unavailable",
Stack: snapshot.Stack,
Size: snapshot.Size,
Preload: snapshot.Preload,
Active: snapshot.Active,
Standby: snapshot.Standby,
Cooling: snapshot.Cooling,
+30 -11
View File
@@ -19,8 +19,8 @@ const DefaultPoolSize = 2
// that block, so a new tunnel inside it would only repeat the same answer.
const egressCooldown = 30 * time.Minute
// refillRetryDelay spaces out a failed registration attempt. A successful
// attempt is only limited by the registrar's own 12-second pacing.
// refillRetryDelay spaces out a failed registration attempt. Successful
// registrations run back to back in the background.
const refillRetryDelay = 5 * time.Second
// scaleDownDelay keeps exits created for a burst hot for a while after the
@@ -73,6 +73,7 @@ type Pool struct {
cancel context.CancelFunc
logger logger
size int
preload int
stack Stack
newExit func(context.Context) (*exit, error)
@@ -101,6 +102,10 @@ type Lease struct {
type Snapshot struct {
Stack string
Size int
// Preload is the number of exits kept hot even when the pool is idle:
// half of Size (at least one), registered in the background after the
// first exit so a burst does not wait for a registration.
Preload int
// Active is the number of exits currently in the ring. Standby counts the
// idle part of it: exits not held by a lease and ready to replace a
// penalized one immediately.
@@ -112,9 +117,9 @@ type Snapshot struct {
// Options configure a WARP pool.
type Options struct {
// Size is the upper bound on exits. The pool keeps as many exits hot as
// the current concurrency needs (leases in flight plus queued requests,
// plus a spare) and never exceeds Size.
// Size is the upper bound on exits. Half of it (at least one) is kept hot
// as a preload even while the pool is idle; beyond that the pool keeps as
// many exits hot as the current concurrency needs and never exceeds Size.
Size int
// Stack selects the address family inside the tunnel. An IPv6 stack makes
// the public egress an IPv6 address.
@@ -189,6 +194,14 @@ func newPool(ctx context.Context, log logger, size int, create func(context.Cont
if size <= 0 {
size = DefaultPoolSize
}
// Keep half of the ceiling warm (at least one) so a burst finds ready
// exits instead of paying for registrations while requests queue. The
// first exit is synchronous and gates readiness; the rest register in the
// background.
preload := size / 2
if preload < 1 {
preload = 1
}
// The caller's context bounds the synchronous first registration, not the
// pool: it is typically an initialization deadline that expires moments
// after NewWithOptions returns. Deriving the pool lifetime from it would
@@ -200,6 +213,7 @@ func newPool(ctx context.Context, log logger, size int, create func(context.Cont
cancel: cancel,
logger: log,
size: size,
preload: preload,
newExit: create,
reserved: make(map[string]bool),
cooling: make(map[string]time.Time),
@@ -212,7 +226,7 @@ func newPool(ctx context.Context, log logger, size int, create func(context.Cont
}
p.active = []*exit{first}
if log != nil {
log.Infof("OpenCode WARP/%s pool ready (max %d exits, one spare)", first.stack, size)
log.Infof("OpenCode WARP/%s pool ready (max %d exits, preloading %d)", first.stack, size, preload)
}
p.startRefill()
return p, nil
@@ -220,10 +234,14 @@ func newPool(ctx context.Context, log logger, size int, create func(context.Cont
// desiredLocked is the number of exits the pool should keep hot: one per
// in-flight lease, one for every request waiting for an exit, plus a spare, so
// a 429 can switch to a ready exit instead of waiting for a registration. Size
// is the upper bound; at idle a pool of two or more keeps one spare.
// a 429 can switch to a ready exit instead of waiting for a registration. The
// preload floor keeps half the ceiling registered while the pool is idle.
// Size is the upper bound; at idle a pool of two or more keeps one spare.
func (p *Pool) desiredLocked() int {
want := p.inflight + p.waiting + 1
if want < p.preload {
want = p.preload
}
if want < 2 {
want = 2
}
@@ -305,7 +323,7 @@ func (p *Pool) Size() int { return p.size }
func (p *Pool) Snapshot() Snapshot {
p.mu.Lock()
defer p.mu.Unlock()
snapshot := Snapshot{Stack: string(p.stack), Size: p.size, Active: len(p.active), Cooling: len(p.cooling)}
snapshot := Snapshot{Stack: string(p.stack), Size: p.size, Preload: p.preload, Active: len(p.active), Cooling: len(p.cooling)}
for _, active := range p.active {
if active.idle() {
snapshot.Standby++
@@ -446,8 +464,9 @@ func (p *Pool) refill() {
if p.logger != nil {
p.logger.Warnf("Cannot pre-register OpenCode WARP egress: %v", err)
}
// Every registration is already paced by the registrar; retry soon
// instead of leaving the pool under its target for a long window.
// A rejected registration (for example an API rate limit) backs off a
// little instead of leaving the pool under its target for a long
// window.
p.after(refillRetryDelay, p.startRefill)
return
}
+7 -22
View File
@@ -15,14 +15,12 @@ import (
"net/netip"
"strconv"
"strings"
"sync"
"time"
)
const (
defaultAPIBase = "https://api.cloudflareclient.com/v0i1909051800"
defaultPeer = "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo="
registerGap = 12 * time.Second
)
var fallbackEndpoints = []string{
@@ -45,11 +43,8 @@ type registration struct {
}
type registrar struct {
base string
client *http.Client
minGap time.Duration
mu sync.Mutex
lastReg time.Time
base string
client *http.Client
}
func newRegistrar() *registrar {
@@ -65,25 +60,15 @@ func newRegistrar() *registrar {
TLSHandshakeTimeout: 10 * time.Second,
},
},
minGap: registerGap,
}
}
// register creates and enables one WARP device. Registrations are no longer
// paced by a fixed gap: the pool registers in the background while traffic is
// served, so a fixed delay only slowed down prefills and 429 replacements.
// Callers already serialize registrations through the pool's create mutex; a
// rejected attempt backs off in the refill loop.
func (r *registrar) register(ctx context.Context) (registration, error) {
r.mu.Lock()
defer r.mu.Unlock()
if wait := r.minGap - time.Since(r.lastReg); wait > 0 {
timer := time.NewTimer(wait)
defer timer.Stop()
select {
case <-ctx.Done():
return registration{}, ctx.Err()
case <-timer.C:
}
}
r.lastReg = time.Now()
privateKey, publicKey, err := wireGuardKeypair()
if err != nil {
return registration{}, err