add concurrency and syncs

This commit is contained in:
Edward 2020-04-27 23:36:22 +08:00
parent c485e98abb
commit 20cee1421f
3 changed files with 109 additions and 3 deletions

View File

@ -44,9 +44,7 @@ Go常用的、面向工程化和最佳实践的模式套路包含常见的23
+ [访问者模式(Visitor)](./behavior/23_visitor) + [访问者模式(Visitor)](./behavior/23_visitor)
+ [闭包选项模式(Function Option)](./behavior/26_option) + [闭包选项模式(Function Option)](./behavior/26_option)
## 参考资(Design patters Articles) ## 参考资料(Design patters Articles)
[大话设计模式](https://book.douban.com/subject/2334288/)
[GO模式文档](https://github.com/nynicg/go-patterns) [GO模式文档](https://github.com/nynicg/go-patterns)

View File

@ -0,0 +1,105 @@
package concurrency
import (
"fmt"
"testing"
"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 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()
}
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())
}
}

3
syncs/README.md Normal file
View File

@ -0,0 +1,3 @@
# 同步模式
s同步处理中的一些常用模式