mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 11:56:03 +03:00
54 lines
955 B
Go
54 lines
955 B
Go
package ratelimit
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
/*
|
|
|
|
Rate limiting is an very important mechanism
|
|
With limiting you can controll resource utilization and maintain quality of service.
|
|
Go supports rate limiting by using goroutines, channels, and tickers.
|
|
*/
|
|
|
|
func rateLimiting() {
|
|
|
|
requests := make(chan int, 5)
|
|
for i := 1; i <= 5; i++ {
|
|
requests <- i
|
|
}
|
|
close(requests)
|
|
|
|
limiter := time.Tick(200 * time.Millisecond)
|
|
|
|
for req := range requests {
|
|
<-limiter
|
|
fmt.Println("request", req, time.Now())
|
|
}
|
|
|
|
//突发限流器
|
|
burstyLimiter := make(chan time.Time, 3)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
burstyLimiter <- time.Now()
|
|
}
|
|
|
|
go func() {
|
|
for t := range time.Tick(200 * time.Millisecond) {
|
|
burstyLimiter <- t
|
|
}
|
|
}()
|
|
|
|
//请求队列
|
|
burstyRequests := make(chan int, 5)
|
|
for i := 1; i <= 5; i++ {
|
|
burstyRequests <- i
|
|
}
|
|
close(burstyRequests)
|
|
for req := range burstyRequests {
|
|
<-burstyLimiter
|
|
fmt.Println("request", req, time.Now())
|
|
}
|
|
}
|