mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 03:46:03 +03:00
add contents for rateLimiting
This commit is contained in:
parent
41d70fee70
commit
b6a3fef5d9
3
gomore/06_circuit_breaker/gobreaker/README.md
Normal file
3
gomore/06_circuit_breaker/gobreaker/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# README
|
||||
|
||||
说明,这是一个由sony的作者完整实现的breaker模式,可以用于生产环境.
|
@ -74,7 +74,7 @@ func fail2Step(cb *TwoStepCircuitBreaker) error {
|
||||
}
|
||||
|
||||
func causePanic(cb *CircuitBreaker) error {
|
||||
_, err := cb.Execute(func() (interface{}, error) { panic("oops"); return nil, nil })
|
||||
_, err := cb.Execute(func() (interface{}, error) { panic("oops") })
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -12,10 +12,10 @@ With limiting you can controll resource utilization and maintain quality of serv
|
||||
Go supports rate limiting by using goroutines, channels, and tickers.
|
||||
*/
|
||||
|
||||
func rateLimiting() {
|
||||
func rateLimiting(requestQueueSize, allowedBurstCount int) {
|
||||
|
||||
requests := make(chan int, 5)
|
||||
for i := 1; i <= 5; i++ {
|
||||
requests := make(chan int, requestQueueSize)
|
||||
for i := 1; i <= requestQueueSize; i++ {
|
||||
requests <- i
|
||||
}
|
||||
close(requests)
|
||||
@ -27,13 +27,15 @@ func rateLimiting() {
|
||||
fmt.Println("request", req, time.Now())
|
||||
}
|
||||
|
||||
//突发限流器
|
||||
burstyLimiter := make(chan time.Time, 3)
|
||||
//允许的突发数量
|
||||
burstyLimiter := make(chan time.Time, allowedBurstCount)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
//初始化允许突发的数量
|
||||
for i := 0; i < allowedBurstCount; i++ {
|
||||
burstyLimiter <- time.Now()
|
||||
}
|
||||
|
||||
//控制请求频率的计时器
|
||||
go func() {
|
||||
for t := range time.Tick(200 * time.Millisecond) {
|
||||
burstyLimiter <- t
|
||||
@ -41,11 +43,12 @@ func rateLimiting() {
|
||||
}()
|
||||
|
||||
//请求队列
|
||||
burstyRequests := make(chan int, 5)
|
||||
for i := 1; i <= 5; i++ {
|
||||
burstyRequests := make(chan int, requestQueueSize)
|
||||
for i := 1; i <= requestQueueSize; i++ {
|
||||
burstyRequests <- i
|
||||
}
|
||||
close(burstyRequests)
|
||||
|
||||
for req := range burstyRequests {
|
||||
<-burstyLimiter
|
||||
fmt.Println("request", req, time.Now())
|
||||
|
@ -1,9 +1,7 @@
|
||||
package ratelimit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -14,52 +12,7 @@ Go supports rate limiting by using goroutines, channels, and tickers.
|
||||
*/
|
||||
|
||||
func TestRateLimiting(t *testing.T) {
|
||||
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
|
||||
t.Log("Sev request by 2000 Millisecond", req, time.Now())
|
||||
}
|
||||
|
||||
burstyLimiter := make(chan struct{}, 3)
|
||||
|
||||
//init burstyLimiter
|
||||
for i := 0; i < 3; i++ {
|
||||
burstyLimiter <- struct{}{}
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-time.Tick(200 * time.Millisecond):
|
||||
burstyLimiter <- struct{}{}
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
//max request queue
|
||||
burstyRequestsQueue := make(chan int, 5)
|
||||
for i := 1; i <= 5; i++ {
|
||||
burstyRequestsQueue <- i
|
||||
}
|
||||
close(burstyRequestsQueue)
|
||||
|
||||
for req := range burstyRequestsQueue {
|
||||
<-burstyLimiter
|
||||
if len(burstyLimiter) > 0 {
|
||||
fmt.Println("working current in bursting status!")
|
||||
} else {
|
||||
fmt.Println("working current in normal status!")
|
||||
}
|
||||
fmt.Println("request handled", req, time.Now())
|
||||
}
|
||||
|
||||
//rateLimiting()
|
||||
rateLimiting(5, 3)
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user