Files
x/limiter/traffic/generator.go
T
ginuerzh 64f9a048f8 feat(limiter/traffic): add burst support, dropped-packet counters, and cache fixes
- Add NewLimiterWithBurst(rate, burst) for independent burst control
- Extend parseLimit to accept optional 4th field as burst size
- Add DroppedPacketCounter interface with DroppedPackets() for packet/udp conns
- Export ErrRateLimited sentinel for rate-limit error checks
- Fix IP-level cache expiration (NoExpiration → defaultExpiration) to prevent
  unbounded growth; reset TTL on access in In/Out
- Short-circuit single-limiter path to avoid limiterGroup allocation
- Add nil guard in cachedTrafficLimiter when both old and new values are nil
- Remove unused ctx field from wrapper structs
2026-05-24 21:52:02 +08:00

40 lines
653 B
Go

package traffic
import (
limiter "github.com/go-gost/core/limiter/traffic"
)
type limitGenerator struct {
in int
out int
burst int
}
func newLimitGenerator(in, out, burst int) *limitGenerator {
return &limitGenerator{
in: in,
out: out,
burst: burst,
}
}
func (p *limitGenerator) In() limiter.Limiter {
if p == nil || p.in <= 0 {
return nil
}
if p.burst > 0 {
return NewLimiterWithBurst(p.in, p.burst)
}
return NewLimiter(p.in)
}
func (p *limitGenerator) Out() limiter.Limiter {
if p == nil || p.out <= 0 {
return nil
}
if p.burst > 0 {
return NewLimiterWithBurst(p.out, p.burst)
}
return NewLimiter(p.out)
}