-
Notifications
You must be signed in to change notification settings - Fork 3
/
hystrix.go
64 lines (51 loc) · 1.19 KB
/
hystrix.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
/*
* Go OAuth2 Client
*
* MIT License
*
* Copyright (c) 2015 Globo.com
*/
package galf
import (
"errors"
"fmt"
"sync"
"github.com/afex/hystrix-go/hystrix"
)
var hystrixConfigs map[string]*HystrixConfig
var hystrixMutex *sync.RWMutex
func init() {
hystrixConfigs = make(map[string]*HystrixConfig)
hystrixMutex = &sync.RWMutex{}
}
type HystrixConfig struct {
Name string
configName string
}
func NewHystrixConfig(configName string) *HystrixConfig {
return &HystrixConfig{
Name: formatHystrixConfigName(configName),
configName: configName,
}
}
func (hc *HystrixConfig) valid() error {
hystrixMutex.RLock()
_, exists := hystrixConfigs[hc.Name]
hystrixMutex.RUnlock()
if !exists {
msg := fmt.Sprintf("Hystrix config name not found: %s", hc.configName)
return errors.New(msg)
}
return nil
}
// ConfigureCommand applies settings for a circuit
func HystrixConfigureCommand(configName string, config hystrix.CommandConfig) {
hystrixMutex.Lock()
defer hystrixMutex.Unlock()
hc := NewHystrixConfig(configName)
hystrix.ConfigureCommand(hc.Name, config)
hystrixConfigs[hc.Name] = hc
}
func formatHystrixConfigName(name string) string {
return fmt.Sprintf("%s_galf", name)
}