Skip to content

Instantly share code, notes, and snippets.

@hxzhouh
Created September 24, 2024 11:14
Show Gist options
  • Save hxzhouh/ba78993644ce58958127f64fc0976c55 to your computer and use it in GitHub Desktop.
Save hxzhouh/ba78993644ce58958127f64fc0976c55 to your computer and use it in GitHub Desktop.
type LeakyBucket struct {
queue chan struct{} // Use a channel to store requests
}
// NewLeakyBucketLimit creates a new LeakyBucket instance.
func NewLeakyBucketLimit(limit int) *LeakyBucket {
lb := &LeakyBucket{
queue: make(chan struct{}, limit),
}
go lb.process()
return lb
}
func (lb *LeakyBucket) Allow() bool {
return lb.push()
}
func (lb *LeakyBucket) push() bool {
select {
case lb.queue <- struct{}{}:
return true
default:
return false
}
}
func (lb *LeakyBucket) process() {
for range lb.queue { // Use range to continuously receive requests from the queue
fmt.Println("Request processed at", time.Now().Format("2006-01-02 15:04:05"))
time.Sleep(100 * time.Millisecond) // Simulate request processing time
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment