Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 103 additions & 3 deletions internal/gateway/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type proxyTransport struct {
healthy atomic.Bool
checking atomic.Bool
blockedUntil atomic.Int64
drainedUntil atomic.Int64

// freeLimitSession and freeLimitAt record the last session this exit
// rejected with the free-model allowance, so a rejection for another
// session within freeLimitRepeatWindow identifies a spent egress.
freeLimitMu sync.Mutex
freeLimitSession string
freeLimitAt time.Time
}

// proxyTransports holds every configured proxy route, indexed by
Expand All @@ -54,6 +62,21 @@ func (t *proxyTransport) blocked(now time.Time) bool {
return t.blockedUntil.Load() > now.UnixNano()
}

// noteFreeLimit records the session this exit rejected for the free-model
// allowance and reports whether the previous rejection came from a different
// session recently, which a per-session throttle cannot explain.
func (t *proxyTransport) noteFreeLimit(session string, now time.Time) bool {
if t == nil {
return false
}
t.freeLimitMu.Lock()
defer t.freeLimitMu.Unlock()
repeat := t.freeLimitSession != "" && t.freeLimitSession != session && now.Sub(t.freeLimitAt) <= freeLimitRepeatWindow
t.freeLimitSession = session
t.freeLimitAt = now
return repeat
}

// markBlocked keeps this exit out of the anonymous cursor until until. The
// block is a property of the egress IP, so it is never cleared by a response
// from the same exit: only another exit can serve traffic in the meantime.
Expand All @@ -64,6 +87,25 @@ func (t *proxyTransport) markBlocked(until time.Time) {
t.blockedUntil.Store(until.UnixNano())
}

// drained reports whether this exit spent the free tier's daily free-model
// allowance for its egress IP.
func (t *proxyTransport) drained(now time.Time) bool {
if t == nil {
return false
}
return t.drainedUntil.Load() > now.UnixNano()
}

// markDrained keeps this exit out of the anonymous cursor until the free
// tier's per-IP daily allowance resets. Unlike an abuse block, this is not the
// exit's fault: the answer is a quota 429 whose Retry-After names the reset.
func (t *proxyTransport) markDrained(until time.Time) {
if t == nil {
return
}
t.drainedUntil.Store(until.UnixNano())
}

// anonymousNodes gives the shared OpenCode "public" credential an independent
// cooldown per proxy. Unlike key nodes, anonymous nodes are never rebound:
// changing proxy is the failover mechanism because Zen rate-limits them by IP.
Expand Down Expand Up @@ -105,6 +147,10 @@ type anonymousCursor struct {
offset int
used []bool
single bool
// probedDrained records that the cursor already sent the one probe allowed
// to an exit parked on its daily allowance, so a fully drained pool answers
// with one real 429 instead of re-walking every parked exit.
probedDrained bool
}

// anonymousPool builds one anonymous node per proxy transport, or an empty
Expand Down Expand Up @@ -330,9 +376,15 @@ func (c *anonymousCursor) Next() *anonymousNode {
}
return c.pool.nodes[index]
}
now := time.Now().UnixNano()
now := time.Now()
nowNanos := now.UnixNano()
// unusable marks an exit the free tier itself told us to leave alone: an
// abuse block, or a spent daily free-model allowance parked until its reset.
isBlocked := func(node *anonymousNode) bool {
return node.proxy != nil && node.proxy.blockedUntil.Load() > now
if node.proxy == nil {
return false
}
return node.proxy.blockedUntil.Load() > nowNanos || node.proxy.drained(now)
}
for c.offset < n {
idx := (c.start + c.offset) % n
Expand All @@ -345,7 +397,7 @@ func (c *anonymousCursor) Next() *anonymousNode {
c.used[idx] = true
continue
}
if node.proxy.pool || (!c.used[idx] && node.proxy.healthy.Load() && node.cooldownUntil.Load() <= now) {
if node.proxy.pool || (!c.used[idx] && node.proxy.healthy.Load() && node.cooldownUntil.Load() <= nowNanos) {
c.used[idx] = true
return node
}
Expand All @@ -372,6 +424,13 @@ func (c *anonymousCursor) Next() *anonymousNode {
if isBlocked(node) {
continue
}
if node.cooldownUntil.Load() > nowNanos {
// A cooled exit already failed on this traffic; retrying it costs
// a full attempt timeout and turns a clean 429 into a gateway
// error when the relay blackholes. Let the caller answer with the
// last upstream response instead.
continue
}
healthy := node.proxy.healthy.Load()
until := node.cooldownUntil.Load()
if choice < 0 || (healthy && !choiceHealthy) || (healthy == choiceHealthy && until < earliest) {
Expand All @@ -382,9 +441,50 @@ func (c *anonymousCursor) Next() *anonymousNode {
c.used[choice] = true
return c.pool.nodes[choice]
}
// Every exit is parked because its daily free-model allowance is spent.
// The client must still get the upstream's 429 (with its reset hint), not a
// gateway error, so probe the exit that resets soonest once per request. A
// parked exit's allowance can also recover early when the observed quota is
// shared with other traffic on that IP.
if !c.probedDrained && c.allParked(now) {
best, earliest := -1, int64(0)
for step := range n {
idx := (c.start + step) % n
node := c.pool.nodes[idx]
if node.proxy == nil || !node.proxy.drained(now) {
continue
}
until := node.proxy.drainedUntil.Load()
if best < 0 || until < earliest {
best, earliest = idx, until
}
}
if best >= 0 {
c.probedDrained = true
c.used[best] = true
return c.pool.nodes[best]
}
}
return nil
}

// allParked reports whether the free tier has parked every exit: each proxy is
// either under an abuse block or drained of its daily free-model allowance.
func (c *anonymousCursor) allParked(now time.Time) bool {
if c.pool == nil || len(c.pool.nodes) == 0 {
return false
}
for _, node := range c.pool.nodes {
if node.proxy == nil {
return false
}
if !node.proxy.blocked(now) && !node.proxy.drained(now) {
return false
}
}
return true
}

func (p *anonymousNodes) MarkSuccess(node *anonymousNode) {
if node == nil {
return
Expand Down
Loading
Loading