分布式系统中限流和熔断机制至关重要,go 框架提供了广泛的实现机制。限流旨在防止过载,例如限制每秒 HTTP 请求数量。熔断则在服务失败或响应时间过长时,暂时阻止对服务的访问,以避免级联故障。
Go 框架限流和熔断在分布式系统中的应用
在现代分布式系统中,限流和熔断机制对于确保系统稳定性和弹性至关重要。Go 语言提供了广泛的框架来简化这些机制的实现。
限流
立即学习“go语言免费学习笔记(深入)”;
限流机制旨在防止系统因过载而崩溃。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import (
"context"
"Github.com/golang/groupcache/lru"
"time"
)
func LimitRequests(ctx context.Context, maxRequests, d time.Duration) ( func (), error ) {
l := lru. New (maxRequests)
go func () {
for {
time.Sleep(d)
l.Purge()
}
}()
return func () bool {
return l.Add( 0 , nil ) != nil
}, nil
}
|
实战示例:
使用限流来限制每秒处理的 HTTP 请求数量:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import (
"github.com/buaazp/fasthttprouter"
"time"
)
func main() {
limiter, err := LimitRequests(context.Background(), 10 , time.Second)
if err != nil {
log.Fatal(err)
}
router := fasthttprouter. New ()
router.GET( "/" , func (ctx *fasthttp.RequestCtx) {
if limiter() {
ctx. Error ( "Too many requests" , fasthttp.StatusTooManyRequests)
return
}
})
server.ListeNANDServe( ":8080" )
}
|
熔断
熔断机制旨在在服务失败或响应时间过长时暂时阻止对服务的访问。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import (
"context"
"errors"
"sync"
"time"
)
type CircuitBreaker struct {
sync.Mutex
state State
closEDAt time.Time
failedReq int
successReq int
}
type State int
const (
Closed State = iota
HalfOpen
Open
)
func (cb *CircuitBreaker) Trip() {
cb.Lock()
defer cb.Unlock()
cb.failedReq++
cb.state = Open
cb.closedAt = time.now().Add(time.Minute)
}
func (cb *CircuitBreaker) Close () {
cb.Lock()
defer cb.Unlock()
cb.state = Closed
cb.closedAt = time.Now()
}
func (cb *CircuitBreaker) Allow() error {
cb.Lock()
defer cb.Unlock()
now := time.Now()
if cb.state == Open && cb.closedAt.After(now) {
return errors. New ( "circuit open" )
}
if cb.state == HalfOpen {
cb.state = Closed
cb.successReq++
}
return nil
}
|
实战示例:
使用熔断器来保护对外部服务的调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import (
"errors"
"time"
"github.com/sony/gobreaker"
"github.com/uber/jaeger-client-go"
)
var (
jaegerCloser jaeger.Closer
cb = gobreaker.NewCircuitBreaker(gobreaker.Settings{
Interval: 10 * time.Second,
})
fallbackcallCount int
)
func MakeRemoteCall() ( string , error ) {
err := cb.Execute( func () ( string , error ) {
return "some result" , nil
})
if err != nil {
fallbackCallCount++
return "fallback result" , nil
}
return "result" , nil
}
|