标签:highlight org time golang int pac back perl print
直接上demo
package main import ( "context" "fmt" limt "go.uber.org/ratelimit" "golang.org/x/time/rate" "github.com/juju/ratelimit" "time" ) func Demo1() { r1 := limt.New(100) prev := time.Now() for i :=0;i<100;i++{ now := r1.Take() fmt.Println(i,now.Sub(prev)) prev = now } } //令牌桶 func Demo2() { limiter := rate.NewLimiter(3,5) ctx ,cancel := context.WithCancel(context.Background()) defer cancel() for i :=0;i<100;i++{ fmt.Printf("time:%s\n",time.Now().Format("2006-01-02 15:04:05.000")) limiter.Wait(ctx) //每秒消费三个 fmt.Println(i) } } //令牌桶 func Demo3() { limt := ratelimit.NewBucket(100,10) for i :=0;i<100;i++{ now := limt.Take(5) fmt.Println(i,now) fmt.Println(i) } } func main() { //Demo1() Demo2() //Demo3() }
标签:highlight org time golang int pac back perl print
原文地址:https://www.cnblogs.com/pebblecome/p/14312311.html