Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
reverse_proxy: CB docs; rename type -> factor (#2986)
* v2: add documentation for circuit breaker config and "random selection" load balancing policy * v2: rename circuit breaker config inline key from `type` to `breaker` to avoid json key clash between the `circuit_breaker` type and the `type` field of the generic circuit breaker Config struct used by circuit breaking implementations * v2: restore the circuit breaker inline key to `type` and rename the name circuit breaker config field from `Type` to `Factor`
Mohammed Al Sahaf committed
Jan 19, 2020 at 04:42 UTC
2bfaf8e89678fc1117984658f31f43fe90266763
2 files changed
+24
-16
modules/caddyhttp/reverseproxy/circuitbreaker.go
+21
-16
@@ -31,7 +31,7 @@ func init() {
31
// for requests within this process over a sliding time window.
32
type localCircuitBreaker struct {
33
tripped int32
34
- cbType int32
34
+ cbFactor int32
35
threshold float64
36
metrics *memmetrics.RTMetrics
37
tripTime time.Duration
@@ -48,7 +48,7 @@ func (localCircuitBreaker) CaddyModule() caddy.ModuleInfo {
48
49
// Provision sets up a configured circuit breaker.
50
func (c *localCircuitBreaker) Provision(ctx caddy.Context) error {
51
- t, ok := typeCB[c.Type]
51
+ f, ok := typeCB[c.Factor]
52
if !ok {
53
return fmt.Errorf("type is not defined")
54
}
@@ -67,7 +67,7 @@ func (c *localCircuitBreaker) Provision(ctx caddy.Context) error {
67
return fmt.Errorf("cannot create new metrics: %v", err.Error())
68
}
69
70
- c.cbType = t
70
+ c.cbFactor = f
71
c.tripTime = tw
72
c.threshold = c.Threshold
73
c.metrics = mt
@@ -92,13 +92,13 @@ func (c *localCircuitBreaker) RecordMetric(statusCode int, latency time.Duration
92
func (c *localCircuitBreaker) checkAndSet() {
93
var isTripped bool
94
95
- switch c.cbType {
96
- case typeErrorRatio:
95
+ switch c.cbFactor {
96
+ case factorErrorRatio:
97
// check if amount of network errors exceed threshold over sliding window, threshold for comparison should be < 1.0 i.e. .5 = 50th percentile
98
if c.metrics.NetworkErrorRatio() > c.threshold {
99
isTripped = true
100
}
101
- case typeLatency:
101
+ case factorLatency:
102
// check if threshold in milliseconds is reached and trip
103
hist, err := c.metrics.LatencyHistogram()
104
if err != nil {
@@ -109,7 +109,7 @@ func (c *localCircuitBreaker) checkAndSet() {
109
if l.Nanoseconds()/int64(time.Millisecond) > int64(c.threshold) {
110
isTripped = true
111
}
112
- case typeStatusCodeRatio:
112
+ case factorStatusCodeRatio:
113
// check ratio of error status codes of sliding window, threshold for comparison should be < 1.0 i.e. .5 = 50th percentile
114
if c.metrics.ResponseCodeRatio(500, 600, 0, 600) > c.threshold {
115
isTripped = true
@@ -130,23 +130,28 @@ func (c *localCircuitBreaker) checkAndSet() {
130
131
// Config represents the configuration of a circuit breaker.
132
type Config struct {
133
+ // The threshold over sliding window that would trip the circuit breaker
134
Threshold float64 `json:"threshold"`
134
- Type string `json:"type"`
135
- TripTime string `json:"trip_time"`
135
+ // Possible values: latency, error_ratio, and status_ratio. It
136
+ // defaults to latency.
137
+ Factor string `json:"factor"`
138
+ // How long to wait after the circuit is tripped before allowing operations to resume.
139
+ // The default is 5s.
140
+ TripTime string `json:"trip_time"`
141
}
142
143
const (
139
- typeLatency = iota + 1
140
- typeErrorRatio
141
- typeStatusCodeRatio
144
+ factorLatency = iota + 1
145
+ factorErrorRatio
146
+ factorStatusCodeRatio
147
defaultTripTime = "5s"
148
)
149
150
var (
146
- // typeCB handles converting a Config Type value to the internal circuit breaker types.
151
+ // typeCB handles converting a Config Factor value to the internal circuit breaker types.
152
typeCB = map[string]int32{
148
- "latency": typeLatency,
149
- "error_ratio": typeErrorRatio,
150
- "status_ratio": typeStatusCodeRatio,
153
+ "latency": factorLatency,
154
+ "error_ratio": factorErrorRatio,
155
+ "status_ratio": factorStatusCodeRatio,
156
}
157
)
modules/caddyhttp/reverseproxy/selectionpolicies.go
+3
@@ -78,6 +78,8 @@ func (r RandomSelection) Select(pool UpstreamPool, request *http.Request) *Upstr
78
// two or more available hosts at random, then
79
// chooses the one with the least load.
80
type RandomChoiceSelection struct {
81
+ // The size of the sub-pool created from the larger upstream pool. The default value
82
+ // is 2 and the maximum at selection time is the size of the upstream pool.
83
Choose int `json:"choose,omitempty"`
84
}
85
@@ -283,6 +285,7 @@ func (URIHashSelection) Select(pool UpstreamPool, req *http.Request) *Upstream {
285
// HeaderHashSelection is a policy that selects
286
// a host based on a given request header.
287
type HeaderHashSelection struct {
288
+ // The HTTP header field whose value is to be hashed and used for upstream selection.
289
Field string `json:"field,omitempty"`
290
}
291