Go Channel Practice III - Leaky Bucket
https://en.wikipedia.org/wiki/Leaky_bucket
实现自己的 Leaky Bucket,首先定义 struct。
type LeakyBucket struct { Quota int Remain int ticker *time.Ticker quit chan bool mutex sync.Mutex }
ticker 用来每一个时间段来做什么东西,quit 是用来 close 相应的 channel,mutex 是为了避免竞争,给资源加锁。
实现一个 LB
func New(quota int) *LeakyBucket { ticker := time.NewTicker(time.Second) lb := &LeakyBucket{ Quota: quota, Remain: quota, ticker: ticker, quit: make(chan bool), } go func() { for { select { case <-ticker.C: fmt.Println("tick") lb.mutex.Lock() lb.Remain = lb.Quota lb.mutex.Unlock() case <-lb.quit: return } } }() return lb }
实现一个 LeakyBucket 的函数,来关闭 ticker 和 New 出来的 goroutine。
func (l *LeakyBucket) Close() { l.ticker.Stop() l.quit <- true }
每次 Leak 的时候,首先判断 remain 的数目是否大于输入。如果小于输入,失败返回;如果大于等于,则 remain 减去输入。
func (l *LeakyBucket) Leak(count int) bool { l.mutex.Lock() defer l.mutex.Unlock() if l.Remain < count { fmt.Println("leak failed") return false } l.Remain -= count fmt.Println("leak success") return true }
整体实现
package main import ( "fmt" "sync" "time" ) type LeakyBucket struct { Quota int Remain int ticker *time.Ticker quit chan bool mutex sync.Mutex } func New(quota int) *LeakyBucket { ticker := time.NewTicker(time.Second) lb := &LeakyBucket{ Quota: quota, Remain: quota, ticker: ticker, quit: make(chan bool), } go func() { for { select { case <-ticker.C: fmt.Println("tick") lb.mutex.Lock() lb.Remain = lb.Quota lb.mutex.Unlock() case <-lb.quit: return } } }() return lb } func (l *LeakyBucket) Close() { l.ticker.Stop() l.quit <- true } func main() { lb := New(1000) fmt.Println(lb.Leak(500)) fmt.Println(lb.Leak(800)) time.Sleep(time.Millisecond * 1100) fmt.Println(lb.Leak(800)) } func (l *LeakyBucket) Leak(count int) bool { l.mutex.Lock() defer l.mutex.Unlock() if l.Remain < count { fmt.Println("leak failed") return false } l.Remain -= count fmt.Println("leak success") return true }
Sep 07, 2022 02:47:58 PM
Indeed I'm completely concurred with this article and I simply need say that this article is exceptionally great and extremely useful article.I will make a point to peruse your blog more. You made a valid statement however I can't resist the urge to ponder, what might be said about the opposite side?
Sep 07, 2022 02:48:32 PM
Great piece of content on a Building Front. I was shocked to guess this thoughts blowing post and like your blog introducing style.
Sep 07, 2022 02:49:01 PM
Your post is so perfect and entirely helpful .I really esteem appreciation for sharing keep it up I visit again your site.
Sep 07, 2022 02:49:25 PM
Every one of the information in this post is heavenly and extraordinarily entrancing such countless gratitude for sharing this post.
Sep 07, 2022 02:49:54 PM
Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day!
Oct 08, 2022 01:46:18 PM
I love your presenting your content like this if anybody wants to show their homework in an attractive manner we are here to help you.
Oct 08, 2022 01:47:11 PM
<a href="https://text.in/"> text </a>
[url=https://text.in/]text[/url]
Oct 08, 2022 01:48:27 PM
Your writing is perfect and complete However, I think it will be more wonderful if your post includes additional topics that I am thinking of. I have a lot of posts on my site topic.
Would you like to visit once?
Oct 08, 2022 01:49:09 PM
Great post I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.
Oct 08, 2022 01:50:05 PM
This is the post I was looking for roulette I am very happy to finally read about the Thank you very much. Your post was of great help to me If you are interested in the column I wrote, please visit my site .
Oct 08, 2022 01:50:53 PM
Thank you for this wonderful post! It has long been extremely helpful. I wish that you will carry on posting your knowledge with us thanks for sharing this post.
Dec 01, 2022 10:58:49 PM
Thank you for this wonderfull post! It has long been extremely helpful. I wish that you will carry on posting your knowledge with us thanks for sharing this post.