Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Flush by name, support force closed options #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion hystrix/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"sync/atomic"
"time"
"errors"
)

// CircuitBreaker is created for each ExecutorPool to track whether requests
Expand All @@ -13,13 +14,19 @@ type CircuitBreaker struct {
Name string
open bool
forceOpen bool
forceClosed bool
mutex *sync.RWMutex
openedOrLastTestedTime int64

executorPool *executorPool
metrics *metricExchange
}

var (
// ErrCBNotExist occurs when no CircuitBreaker exists
ErrCBNotExist = errors.New("circuit breaker not exist")
)

var (
circuitBreakersMutex *sync.RWMutex
circuitBreakers map[string]*CircuitBreaker
Expand All @@ -30,6 +37,17 @@ func init() {
circuitBreakers = make(map[string]*CircuitBreaker)
}

// IsCircuitBreakerOpen returns whether a circuitBreaker is open for an interface
func IsCircuitBreakerOpen(name string) (bool, error) {
circuitBreakersMutex.Lock()
defer circuitBreakersMutex.Unlock()
if c, ok := circuitBreakers[name]; ok {
return c.IsOpen(), nil
} else {
return false, ErrCBNotExist
}
}

// GetCircuit returns the circuit for the given command and whether this call created it.
func GetCircuit(name string) (*CircuitBreaker, bool, error) {
circuitBreakersMutex.RLock()
Expand All @@ -51,6 +69,17 @@ func GetCircuit(name string) (*CircuitBreaker, bool, error) {

return circuitBreakers[name], !ok, nil
}
func FlushByName(name string) {
circuitBreakersMutex.Lock()
defer circuitBreakersMutex.Unlock()
cb, ok := circuitBreakers[name]
if ok {
cb.metrics.Reset()
cb.executorPool.Metrics.Reset()
delete(circuitBreakers, name)
}

}

// Flush purges all circuit and metric information from memory.
func Flush() {
Expand All @@ -71,7 +100,8 @@ func newCircuitBreaker(name string) *CircuitBreaker {
c.metrics = newMetricExchange(name)
c.executorPool = newExecutorPool(name)
c.mutex = &sync.RWMutex{}

c.forceOpen = getSettings(name).ForceOpen
c.forceClosed = getSettings(name).ForceClose
return c
}

Expand Down Expand Up @@ -115,6 +145,13 @@ func (circuit *CircuitBreaker) IsOpen() bool {
// When the circuit is open, this call will occasionally return true to measure whether the external service
// has recovered.
func (circuit *CircuitBreaker) AllowRequest() bool {
//force open has highest priority
if circuit.forceOpen {
return false
}
if circuit.forceClosed {
return true
}
return !circuit.IsOpen() || circuit.allowSingleTest()
}

Expand Down
2 changes: 1 addition & 1 deletion hystrix/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (m *metricExchange) Monitor() {
wg := &sync.WaitGroup{}
for _, collector := range m.metricCollectors {
wg.Add(1)
go m.IncrementMetrics(wg, collector, update, totalDuration)
m.IncrementMetrics(wg, collector, update, totalDuration)
}
wg.Wait()

Expand Down
3 changes: 3 additions & 0 deletions hystrix/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Settings struct {
RequestVolumeThreshold uint64
SleepWindow time.Duration
ErrorPercentThreshold int
ForceFallback bool
ForceOpen bool
ForceClose bool
}

// CommandConfig is used to tune circuit settings at runtime
Expand Down