- Implemented a simple leaky bucket rate limitter and throttler.
client → HTTP handler → Submit() → [bucket: buffered chan] → leak() → outputCh → consumer
↑ (capacity N) (1/tick)
429 on full 202 on accepted
- It is a rate limitting strategy that takes in the requests at any rate but only lets out the requests at a certain rate.
- It also rejects all the incoming requests once its buffer is full.
go run .
go test .
- To test manually:
curl -i -X POST localhost:4444/meow -d '{"name":"a","quote":"hi"}'- 40 requests in a burst -
for i in $(seq 1 40); do curl -s -o /dev/null -w "%{http_code}\n" -X POST localhost:4444/meow -d '{"name":"a","quote":"hi"}'; done
- The requests are dropped when the bucket is full.
- OutputCh is unbuffered.
- HTTP handler returns 202 immediately and the work happens async via the drip.