mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2025-02-16 17:23:15 +03:00
finish rate-limit pattern
This commit is contained in:
parent
637bebc5b4
commit
94005c8dc8
@ -12,22 +12,10 @@ With limiting you can controll resource utilization and maintain quality of serv
|
|||||||
Go supports rate limiting by using goroutines, channels, and tickers.
|
Go supports rate limiting by using goroutines, channels, and tickers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func rateLimiting(requestQueueSize, allowedBurstCount int) {
|
func rateLimiting(requestQueue chan int, duration int64, allowedBurstCount int) {
|
||||||
|
|
||||||
requests := make(chan int, requestQueueSize)
|
//根据允许的突发数量,创建ch
|
||||||
for i := 1; i <= requestQueueSize; i++ {
|
//只要该队列中有内容,就可以一直取出来,即便ch已经关闭
|
||||||
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, allowedBurstCount)
|
burstyLimiter := make(chan time.Time, allowedBurstCount)
|
||||||
|
|
||||||
//初始化允许突发的数量
|
//初始化允许突发的数量
|
||||||
@ -37,20 +25,29 @@ func rateLimiting(requestQueueSize, allowedBurstCount int) {
|
|||||||
|
|
||||||
//控制请求频率的计时器
|
//控制请求频率的计时器
|
||||||
go func() {
|
go func() {
|
||||||
for t := range time.Tick(200 * time.Millisecond) {
|
for t := range time.Tick(time.Duration(duration) * time.Millisecond) {
|
||||||
burstyLimiter <- t
|
burstyLimiter <- t
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//请求队列
|
for req := range requestQueue {
|
||||||
burstyRequests := make(chan int, requestQueueSize)
|
<-burstyLimiter //突发控制器是限流的关键
|
||||||
for i := 1; i <= requestQueueSize; i++ {
|
fmt.Println("request", req, time.Now())
|
||||||
burstyRequests <- i
|
}
|
||||||
}
|
}
|
||||||
close(burstyRequests)
|
|
||||||
|
func simpleRateLimiting(duration int64, requestQueueSize int) {
|
||||||
for req := range burstyRequests {
|
|
||||||
<-burstyLimiter
|
requests := make(chan int, requestQueueSize)
|
||||||
|
for i := 1; i <= requestQueueSize; i++ {
|
||||||
|
requests <- i
|
||||||
|
}
|
||||||
|
close(requests)
|
||||||
|
|
||||||
|
limiter := time.Tick(time.Duration(duration) * time.Millisecond)
|
||||||
|
|
||||||
|
for req := range requests {
|
||||||
|
<-limiter
|
||||||
fmt.Println("request", req, time.Now())
|
fmt.Println("request", req, time.Now())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* @Description: https://github.com/crazybber
|
||||||
|
* @Author: Edward
|
||||||
|
* @Date: 2020-05-21 12:14:27
|
||||||
|
* @Last Modified by: Edward
|
||||||
|
* @Last Modified time: 2020-05-21 12:27:56
|
||||||
|
*/
|
||||||
|
|
||||||
package ratelimit
|
package ratelimit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -11,8 +19,23 @@ With limiting you can controll resource utilization and maintain quality of serv
|
|||||||
Go supports rate limiting by using goroutines, channels, and tickers.
|
Go supports rate limiting by using goroutines, channels, and tickers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var (
|
||||||
|
requestQueueSize = 10
|
||||||
|
)
|
||||||
|
|
||||||
func TestRateLimiting(t *testing.T) {
|
func TestRateLimiting(t *testing.T) {
|
||||||
|
|
||||||
rateLimiting(5, 3)
|
//请求队列
|
||||||
|
burstyRequests := make(chan int, requestQueueSize)
|
||||||
|
|
||||||
|
for i := 1; i <= requestQueueSize; i++ {
|
||||||
|
burstyRequests <- i
|
||||||
|
}
|
||||||
|
close(burstyRequests)
|
||||||
|
|
||||||
|
//对请求进行限流
|
||||||
|
|
||||||
|
//200ms允许一次请求,最多同时3个突发
|
||||||
|
rateLimiting(burstyRequests, 200, 3)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user