import (
lb "github.com/OrangeFlag/handbalancer"
"github.com/OrangeFlag/handbalancer/strategy"
)
var balancer = lb.NewBalancer("balancer_name", &strategy.RoundRobinStrategy{})
balancer.AddPerformer("performer_name",
func(i interface{}) (interface{}, error) {
//some code for performer
}, nil, nil)
Define your application logic which relies on external systems, passing your function to lb.Go
. When that system is healthy this will be the only thing which executes.
lb.Go("balancer_name", func() (interface{}, error) {
// talk to other services
result := nil
return result, nil
}, nil)
If you want code to execute during a service outage, pass in a second function to lb.Go
. Ideally, the logic here will allow your application to gracefully handle external services being unavailable.
lb.Go("balancer_name", func() (interface{}, error) {
// talk to other services
result := nil
return result, nil
}, func(error) (interface{}, error) {
// try to fallback
result := nil
return result, nil
})
Since calling a command and immediately waiting for it to finish is a common pattern, a synchronous API is available with the lb.Do
function.
result, error := lb.Do("balancer_name", func() (interface{}, error) {
// talk to other services
result := nil
return result, nil
}, nil)
During adding performer you can configure circuit breaker
balancer.AddPerformer("performer_name",
func(i interface{}) (interface{}, error) {
//some code for performer
}, nil,
&lb.CircuitBreakerConfig{
Timeout: 10000,
MaxConcurrentRequests: 100,
RequestVolumeThreshold: 5,
SleepWindow: 5000,
ErrorPercentThreshold: 25,
})
In the next releases