mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 13:16:02 +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 {
|
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
|
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.
|
Go supports rate limiting by using goroutines, channels, and tickers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func rateLimiting() {
|
func rateLimiting(requestQueueSize, allowedBurstCount int) {
|
||||||
|
|
||||||
requests := make(chan int, 5)
|
requests := make(chan int, requestQueueSize)
|
||||||
for i := 1; i <= 5; i++ {
|
for i := 1; i <= requestQueueSize; i++ {
|
||||||
requests <- i
|
requests <- i
|
||||||
}
|
}
|
||||||
close(requests)
|
close(requests)
|
||||||
@ -27,13 +27,15 @@ func rateLimiting() {
|
|||||||
fmt.Println("request", req, time.Now())
|
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()
|
burstyLimiter <- time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//控制请求频率的计时器
|
||||||
go func() {
|
go func() {
|
||||||
for t := range time.Tick(200 * time.Millisecond) {
|
for t := range time.Tick(200 * time.Millisecond) {
|
||||||
burstyLimiter <- t
|
burstyLimiter <- t
|
||||||
@ -41,11 +43,12 @@ func rateLimiting() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
//请求队列
|
//请求队列
|
||||||
burstyRequests := make(chan int, 5)
|
burstyRequests := make(chan int, requestQueueSize)
|
||||||
for i := 1; i <= 5; i++ {
|
for i := 1; i <= requestQueueSize; i++ {
|
||||||
burstyRequests <- i
|
burstyRequests <- i
|
||||||
}
|
}
|
||||||
close(burstyRequests)
|
close(burstyRequests)
|
||||||
|
|
||||||
for req := range burstyRequests {
|
for req := range burstyRequests {
|
||||||
<-burstyLimiter
|
<-burstyLimiter
|
||||||
fmt.Println("request", req, time.Now())
|
fmt.Println("request", req, time.Now())
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package ratelimit
|
package ratelimit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -14,52 +12,7 @@ Go supports rate limiting by using goroutines, channels, and tickers.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
func TestRateLimiting(t *testing.T) {
|
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)
|
rateLimiting(5, 3)
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user