Skip to content

Commit d40309a

Browse files
alice-yydsAsterDY
andauthored
doc: sync circuitbreaker.md (#838)
Co-authored-by: Yi Duan <[email protected]>
1 parent 507de02 commit d40309a

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed

content/en/docs/kitex/Tutorials/service-governance/circuitbreaker.md

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
---
22
title: "Circuit Breaker"
3-
date: 2021-08-31
4-
weight: 5
5-
keywords: ["Kitex", "Circuit Breaker"]
6-
description: "This doc covers Kitex Circuit Breaker use guide and principle introduction."
3+
date: 2023-10-23
4+
weight: 1
5+
keywords: ["Kitex - EN","Circuit Breaker"]
6+
description: ""
77
---
88

9-
Kitex provides a default implementation of Circuit Breaker, but it's disabled by default.
9+
# Circuit Breaker
10+
11+
## Introduction
12+
13+
Kitex provides a default implementation of Circuit Breaker, but it’s disabled by default.
1014

1115
The following document will introduce that how to enable circuit breaker and configure the policy.
1216

@@ -16,41 +20,43 @@ The following document will introduce that how to enable circuit breaker and con
1620

1721
```go
1822
import (
19-
...
20-
"github.com/cloudwego/kitex/client"
21-
"github.com/cloudwego/kitex/pkg/circuitbreak"
22-
"github.com/cloudwego/kitex/pkg/rpcinfo"
23+
...
24+
"github.com/cloudwego/kitex/client"
25+
"github.com/cloudwego/kitex/pkg/circuitbreak"
26+
"github.com/cloudwego/kitex/pkg/rpcinfo"
2327
)
2428

2529
// GenServiceCBKeyFunc returns a key which determines the granularity of the CBSuite
2630
func GenServiceCBKeyFunc(ri rpcinfo.RPCInfo) string {
27-
// circuitbreak.RPCInfo2Key returns "$fromServiceName/$toServiceName/$method"
28-
return circuitbreak.RPCInfo2Key(ri)
31+
// circuitbreak.RPCInfo2Key returns "$fromServiceName/$toServiceName/$method"
32+
return circuitbreak.RPCInfo2Key(ri)
2933
}
3034

3135
func main() {
32-
// build a new CBSuite with
33-
cbs := circuitbreak.NewCBSuite(GenServiceCBKeyFunc)
34-
35-
var opts []client.Option
36-
37-
// add to the client options
38-
opts = append(opts, client.WithCircuitBreaker(cbs))
39-
40-
// init client
41-
cli, err := echoservice.NewClient(targetService, opts...)
42-
43-
// update circuit breaker config for a certain key (should be consistent with GenServiceCBKeyFunc)
44-
// this can be called at any time, and will take effect for following requests
45-
cbs.UpdateServiceCBConfig("fromServiceName/toServiceName/method", circuitbreak.CBConfig{
46-
Enable: true,
47-
ErrRate: 0.3, // requests will be blocked if error rate >= 30%
48-
MinSample: 200, // this config takes effect if sampled requests are more than `MinSample`
49-
})
50-
51-
// send requests with the client above
52-
...
36+
// build a new CBSuite with
37+
cbs := circuitbreak.NewCBSuite(GenServiceCBKeyFunc)
38+
39+
var opts []client.Option
40+
41+
// add to the client options
42+
opts = append(opts, client.WithCircuitBreaker(cbs))
43+
44+
// init client
45+
cli, err := echoservice.NewClient(targetService, opts...)
46+
47+
// update circuit breaker config for a certain key (should be consistent with GenServiceCBKeyFunc)
48+
// this can be called at any time, and will take effect for following requests
49+
cbs.UpdateServiceCBConfig("fromServiceName/toServiceName/method", circuitbreak.CBConfig{
50+
Enable: true,
51+
ErrRate: 0.3, // requests will be blocked if error rate >= 30%
52+
MinSample: 200, // this config takes effect if sampled requests are more than `MinSample`
53+
})
54+
55+
// send requests with the client above
56+
...
5357
}
58+
59+
5460
```
5561

5662
### Introduction
@@ -59,29 +65,24 @@ Kitex provides a set of CBSuite that encapsulates both service-level breaker and
5965

6066
- Service-Level Breaker
6167

62-
Statistics by service granularity, enabled via WithMiddleware.
63-
64-
The specific division of service granularity depends on the Circuit Breaker Key, which is the key for breaker statistics. When initializing the CBSuite, you need to pass it in **GenServiceCBKeyFunc**. The default key is `circuitbreak.RPCInfo2Key`, and the format of RPCInfo2Key is ` fromServiceName/toServiceName/method`.
65-
68+
- Statistics by service granularity, enabled via WithMiddleware.
69+
- The specific service granularity depends on the Circuit Breaker Key, which is the key for breaker statistics. When initializing the CBSuite, you need to pass it in **GenServiceCBKeyFunc**. The default key is `circuitbreak.RPCInfo2Key`, and the format of RPCInfo2Key is `fromServiceName/toServiceName/method`.
6670
- Instance-Level Breaker
6771

68-
Statistics by instance granularity, enabled via WithInstanceMW.
69-
70-
Instance-Level Breaker is used to solve the single-instance exception problem. If it's triggered, the framework will automatically retry the request.
71-
72-
Note that the premise of retry is that you need to enable breaker with **WithInstanceMW**, which will be executed after load balancing.
73-
72+
- Statistics by instance granularity, enabled via WithInstanceMW.
73+
- Instance-Level Breaker is used to solve the single-instance exception problem. If it’s triggered, the framework will automatically retry the request.
74+
- Note that the premise of retry is that you need to enable breaker with **WithInstanceMW**, which will be executed after load balancing.
7475
- Threshold and **Threshold Change**
7576

76-
The default breaker threshold is `ErrRate: 0.5, MinSample: 200`, which means it's triggered by an error rate of 50% and requires the count of requests > 200.
77+
The default breaker threshold is `ErrRate: 0.5, MinSample: 200`, which means its triggered by an error rate of 50% and requires the amount of requests > 200.
7778

7879
If you want to change the threshold, you can modify the `UpdateServiceCBConfig` and `UpdateInstanceCBConfig` in CBSuite.
7980

8081
## The Role of Circuit Breaker
8182

8283
When making RPC calls, errors are inevitable for downstream services.
8384

84-
When a downstream has a problem, if the upstream continues to make calls to it, it both prevents the downstream from recovering and wastes the upstream's resources.
85+
When a downstream has a problem, if the upstream continues to make calls to it, it both prevents the downstream from recovering and wastes the upstreams resources.
8586

8687
To solve this problem, you can set up some dynamic switches that manually shut down calls to the downstream when it goes wrong.
8788

@@ -115,16 +116,15 @@ In general, the transition of the three states is roughly as follows:
115116
The timeout for cooling
116117
| v
117118
+-- detect succeed --<-[HALFOPEN]-->--+
119+
118120
```
119121

120122
### Trigger Strategies
121123

122124
Kitex provides three basic fuse triggering strategies by default:
123125

124126
- Number of consecutive errors reaches threshold (ConsecutiveTripFunc)
125-
126127
- Error count reaches threshold (ThresholdTripFunc)
127-
128128
- Error rate reaches the threshold (RateTripFunc)
129129

130130
Of course, you can write your own triggering strategy by implementing the TripFunc function.
@@ -145,9 +145,9 @@ During HALFOPEN, the breaker will let a request go every "interval", and after a
145145

146146
The process is a gradual-trial process.
147147

148-
Both the "interval" (DetectTimeout) and the "number" (DEFAULT_HALFOPEN_SUCCESSES) are configurable.
148+
Both the interval (DetectTimeout) and the "number" (DEFAULT_HALFOPEN_SUCCESSES) are configurable.
149149

150-
## Statistics
150+
## Statistics Algothrithm
151151

152152
### Default Config
153153

@@ -171,14 +171,12 @@ As time moves, the oldest bucket in the window expires. The jitter occurs when t
171171

172172
As an example:
173173

174-
- You divide 10 seconds into 10 buckets, bucket 0 corresponds to a time of [0S, 1S), bucket 1 corresponds to [1S, 2S), ... , and bucket 9 corresponds to [9S, 10S).
175-
174+
- You divide 10 seconds into 10 buckets, bucket 0 corresponds to a time of [0S, 1S), bucket 1 corresponds to [1S, 2S), … , and bucket 9 corresponds to [9S, 10S).
176175
- At 10.1S, a Succ is executed, and the following operations occur within the circuitbreaker.
177176

178-
- (1) detects that bucket 0 has expired and discards it;
179-
- (2) creates a new bucket 10, corresponding to [10S, 11S);
180-
- (3) puts that Succ into bucket 10.
181-
177+
- (1) detects that bucket 0 has expired and discards it;
178+
- (2) creates a new bucket 10, corresponding to [10S, 11S);
179+
- (3) puts that Succ into bucket 10.
182180
- At 10.2S, you execute Successes() to query the number of successes in the window, then you get the actual statistics for [1S, 10.2S), not [0.2S, 10.2S).
183181

184182
Such jitter cannot be avoided if you use time-window-bucket statistics. A compromise approach is to increase the number of buckets, which can reduce the impact of jitter.

content/zh/docs/kitex/Tutorials/service-governance/circuitbreaker.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
---
2+
title: "熔断"
3+
date: 2023-10-23
4+
weight: 1
5+
keywords: ["Kitex","熔断"]
6+
description: ""
7+
---
8+
19
# 熔断
210

311
## 介绍

0 commit comments

Comments
 (0)