-
Notifications
You must be signed in to change notification settings - Fork 9
/
eapache.go
82 lines (72 loc) · 1.94 KB
/
eapache.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Package eapache provides a circuit breaker adapter using the github.com/eapache/go-resiliency/breaker lib.
Sample backend extra config
...
"extra_config": {
...
"github.com/devopsfaith/krakend-circuitbreaker/eapache": {
"success": 60,
"timeout": "10s",
"error": 5
},
...
},
...
The eapache package provides an efficient circuit breaker implementation. See https://github.com/eapache/go-resiliency/breaker
and https://martinfowler.com/bliki/CircuitBreaker.html for more details.
*/
package eapache
import (
"time"
"github.com/eapache/go-resiliency/breaker"
"github.com/luraproject/lura/v2/config"
)
// Namespace is the key to use to store and access the custom config data
const Namespace = "github.com/devopsfaith/krakend-circuitbreaker/eapache"
// Config is the custom config struct containing the params for the eapache/go-resiliency/breaker package
type Config struct {
Error int
Success int
Timeout time.Duration
}
// ZeroCfg is the zero value for the Config struct
var ZeroCfg = Config{}
// ConfigGetter implements the config.ConfigGetter interface. It parses the extra config for the
// eapache adapter and returns a ZeroCfg if something goes wrong.
func ConfigGetter(e config.ExtraConfig) interface{} {
v, ok := e[Namespace]
if !ok {
return ZeroCfg
}
tmp, ok := v.(map[string]interface{})
if !ok {
return ZeroCfg
}
cfg := Config{}
if v, ok := tmp["error"]; ok {
switch i := v.(type) {
case int:
cfg.Error = i
case float64:
cfg.Error = int(i)
}
}
if v, ok := tmp["success"]; ok {
switch i := v.(type) {
case int:
cfg.Success = i
case float64:
cfg.Success = int(i)
}
}
if v, ok := tmp["timeout"]; ok {
if d, err := time.ParseDuration(v.(string)); err == nil {
cfg.Timeout = d
}
}
return cfg
}
// NewCircuitBreaker builds a eapache circuit breaker with the injected config
func NewCircuitBreaker(cfg Config) *breaker.Breaker {
return breaker.New(cfg.Error, cfg.Success, cfg.Timeout)
}