mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 11:56:03 +03:00
add rate_limiting
This commit is contained in:
parent
ed85d19ad7
commit
b9a9dc740d
53
gomore/rate_limiting/rate_limiting.go
Normal file
53
gomore/rate_limiting/rate_limiting.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
@ -60,46 +60,6 @@ func TestRateLimiting(t *testing.T) {
|
|||||||
fmt.Println("request handled", req, time.Now())
|
fmt.Println("request handled", req, time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
rateLimiting()
|
//rateLimiting()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user