-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Trendyol/circuits-module
add circuit breaker module
- Loading branch information
Showing
3 changed files
with
138 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"github.com/afex/hystrix-go/hystrix" | ||
"time" | ||
) | ||
|
||
type CircuitFunc func() error | ||
type CircuitErrorFilter func(error) (bool, error) | ||
|
||
type CircuitConfig struct { | ||
Name string | ||
Timeout int | ||
MaxConcurrentRequests int | ||
ErrorPercentThreshold int | ||
RequestVolumeThreshold int | ||
SleepWindow int | ||
Commands []string | ||
} | ||
|
||
type Circuit struct { | ||
config CircuitConfig | ||
} | ||
|
||
func NewCircuit(c CircuitConfig) *Circuit { | ||
hystrixConfig := hystrix.CommandConfig{ | ||
Timeout: c.Timeout, | ||
MaxConcurrentRequests: c.MaxConcurrentRequests, | ||
ErrorPercentThreshold: c.ErrorPercentThreshold, | ||
RequestVolumeThreshold: c.RequestVolumeThreshold, | ||
SleepWindow: c.SleepWindow, | ||
} | ||
|
||
for _, command := range c.Commands { | ||
hystrix.ConfigureCommand(fmt.Sprintf("%s:%s", c.Name, command), hystrixConfig) | ||
} | ||
|
||
return &Circuit{ | ||
config: c, | ||
} | ||
} | ||
|
||
func (c *Circuit) Do(command string, fu CircuitFunc, fallback func(error) error, fi ...CircuitErrorFilter) error { | ||
var e error | ||
var ok bool | ||
|
||
function := func() error { | ||
|
||
err := fu() | ||
|
||
for _, filter := range fi { | ||
if ok, e = filter(err); ok { | ||
return err | ||
} | ||
} | ||
|
||
if len(fi) > 0 { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
hystrixErr := hystrix.Do(fmt.Sprintf("%s:%s", c.config.Name, command), function, fallback) | ||
|
||
if hystrixErr != nil { | ||
return hystrixErr | ||
} | ||
|
||
if e != nil { | ||
return e | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Circuit) DoR(command string, fu CircuitFunc, fallback func(error) error, retry int, delay time.Duration, fi ...CircuitErrorFilter) error { | ||
var e error | ||
var ok bool | ||
|
||
filter := func() error { | ||
err := fu() | ||
|
||
for _, filter := range fi { | ||
if ok, e = filter(err); ok { | ||
return err | ||
} | ||
} | ||
|
||
if len(fi) > 0 { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
function := func() error { | ||
err := filter() | ||
|
||
for c := 0; c < retry && err != nil; c++ { | ||
time.Sleep(delay) | ||
err = filter() | ||
} | ||
|
||
return err | ||
} | ||
|
||
hystrixErr := hystrix.Do(fmt.Sprintf("%s:%s", c.config.Name, command), function, fallback) | ||
|
||
if hystrixErr != nil { | ||
return hystrixErr | ||
} | ||
|
||
if e != nil { | ||
return e | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters