Created
September 24, 2024 11:14
-
-
Save hxzhouh/ba78993644ce58958127f64fc0976c55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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