diff --git a/gomore/06_circuit_breaker/gobreaker/README.md b/gomore/06_circuit_breaker/gobreaker/README.md new file mode 100644 index 0000000..4f8eed3 --- /dev/null +++ b/gomore/06_circuit_breaker/gobreaker/README.md @@ -0,0 +1,3 @@ +# README + +说明,这是一个由sony的作者完整实现的breaker模式,可以用于生产环境. diff --git a/gomore/06_circuit_breaker/gobreaker/gobreaker_test.go b/gomore/06_circuit_breaker/gobreaker/gobreaker_test.go index eb8ecc5..b32ff89 100644 --- a/gomore/06_circuit_breaker/gobreaker/gobreaker_test.go +++ b/gomore/06_circuit_breaker/gobreaker/gobreaker_test.go @@ -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 } diff --git a/gomore/07_rate_limiting/rate_limiting.go b/gomore/07_rate_limiting/rate_limiting.go index 451bdfd..23282fd 100644 --- a/gomore/07_rate_limiting/rate_limiting.go +++ b/gomore/07_rate_limiting/rate_limiting.go @@ -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()) diff --git a/gomore/07_rate_limiting/rate_limiting_test.go b/gomore/07_rate_limiting/rate_limiting_test.go index 6c660ee..f51f1b4 100644 --- a/gomore/07_rate_limiting/rate_limiting_test.go +++ b/gomore/07_rate_limiting/rate_limiting_test.go @@ -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) }