grant: replace the rate check with a token bucket
The live throughput test found it: a 3-second run delivered 104 packets and stopped after 50 milliseconds. The rate check exempted the first 50 ms entirely, meaning to be lenient at startup. The effect was the opposite. A sender could dump an unbounded burst into that free window, and the instant the check switched on it compared those bytes against 50 ms worth of allowance and refused everything until real time caught up. Every short test passed — downtrain sends 50 packets, big_send seven — and every sustained send died about fifty milliseconds in. A token bucket (allowance = burst + rate x elapsed) has no such cliff; it is smooth from t=0. The burst is 100 ms of the allowed rate, floored at one ordinary datagram so a single packet is never refused outright. The floor is deliberately one datagram: at 8 kbps a 64 KB floor would be sixty-four seconds' worth, which is precisely the instant dump the ceiling exists to prevent. The existing rate test caught that when I first tried it, and it was right. Second half of the same bug: callers treated any refusal as terminal. TryAllow now says why, so a sender can pace through a transient "too fast just now" and still stop dead on a spent budget or an expired grant. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
3333788d9e
commit
3c9af04e6f
@@ -74,22 +74,64 @@ func (s *Session) NewGrant(actionID string, wantBytes int64, wantKbps int, lim G
|
||||
// enforces the byte ceiling, the expiry, and the average rate (by refusing early sends rather
|
||||
// than sleeping, so callers stay in control of pacing).
|
||||
func (g *Grant) Allow(n int) bool {
|
||||
ok, _ := g.TryAllow(n)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Refusal reasons from TryAllow. The distinction is not cosmetic: "too fast just now" is
|
||||
// transient and a caller should pace and carry on, while "budget" and "expired" are terminal and
|
||||
// a caller that keeps trying is only wasting its own run.
|
||||
const (
|
||||
RefusalNone = ""
|
||||
RefusalBudget = "budget"
|
||||
RefusalExpired = "expired"
|
||||
RefusalRate = "rate"
|
||||
)
|
||||
|
||||
// TryAllow reports whether n more bytes may be sent now, consuming the budget when they may, and
|
||||
// says why not when they may not.
|
||||
//
|
||||
// The rate limit is a token bucket: allowance = burst + rate x elapsed. An earlier version
|
||||
// exempted the first 50 ms from the check entirely, meaning to be lenient at startup. The effect
|
||||
// was the opposite - a sender could dump an unbounded burst into that window, and the moment the
|
||||
// check switched on it compared those bytes against 50 ms worth of allowance and refused
|
||||
// everything until real time caught up. A sustained send died about fifty milliseconds in, having
|
||||
// looked perfectly fine in every short test. A bucket has no such cliff: it is smooth from t=0.
|
||||
func (g *Grant) TryAllow(n int) (bool, string) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if time.Now().After(g.ExpiresAt) {
|
||||
return false
|
||||
return false, RefusalExpired
|
||||
}
|
||||
if g.sentBytes+int64(n) > g.MaxBytes {
|
||||
return false
|
||||
return false, RefusalBudget
|
||||
}
|
||||
// Average-rate check: bytes allowed so far = kbps/8 * elapsed_seconds.
|
||||
// kbps -> bytes/s is kbps*1000/8 = kbps*125.
|
||||
bytesPerSec := float64(g.MaxKbps) * 125
|
||||
elapsed := time.Since(g.started).Seconds()
|
||||
allowed := float64(g.MaxKbps) * 125 * elapsed // kbps -> bytes/s is kbps*1000/8 = kbps*125
|
||||
if elapsed > 0.05 && float64(g.sentBytes+int64(n)) > allowed {
|
||||
return false
|
||||
allowed := burstBytes(bytesPerSec) + bytesPerSec*elapsed
|
||||
if float64(g.sentBytes+int64(n)) > allowed {
|
||||
return false, RefusalRate
|
||||
}
|
||||
g.sentBytes += int64(n)
|
||||
return true
|
||||
return true, RefusalNone
|
||||
}
|
||||
|
||||
// burstBytes is the bucket's depth: 100 ms of the allowed rate, floored at a single ordinary
|
||||
// datagram.
|
||||
//
|
||||
// The floor exists only so that one packet is never refused outright by a very slow grant — it is
|
||||
// deliberately one datagram and not more. A generous floor would undo the rate ceiling at low
|
||||
// rates: at 8 kbps a 64 KB burst is sixty-four seconds' worth, which is exactly the instant dump
|
||||
// the ceiling is there to prevent. One datagram is 1.5 seconds' worth at that rate and nothing at
|
||||
// any realistic one.
|
||||
func burstBytes(bytesPerSec float64) float64 {
|
||||
const oneDatagram = 1500
|
||||
b := bytesPerSec * 0.1
|
||||
if b < oneDatagram {
|
||||
b = oneDatagram
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Sent returns how many bytes this grant has consumed.
|
||||
|
||||
Reference in New Issue
Block a user