handbalancer

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2020 License: MIT Imports: 8 Imported by: 0

README

handbalancer

How to use

import (
    lb "github.com/OrangeFlag/handbalancer"
    "github.com/OrangeFlag/handbalancer/strategy"
)
Create balancer
var balancer = lb.NewBalancer("balancer_name", &strategy.RoundRobinStrategy{})
balancer.AddPerformer("performer_name", 
        func(i interface{}) (interface{}, error) {
		//some code for performer
	}, nil, nil)
Execute code as a command

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)
Defining fallback behavior for command

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
})
Synchronous API

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)
Configure settings

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,
    })
Enable dashboard metrics

In the next releases

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddBalancer

func AddBalancer(balancer *Balancer) error

func Do

func Do(name string, run model.RunFunc, fallback model.FallbackFunc) (interface{}, error)

func Go

func Go(name string, run model.RunFunc, fallback model.FallbackFunc) (chan interface{}, chan error)

Types

type Balancer

type Balancer struct {
	// contains filtered or unexported fields
}

func GetBalancer

func GetBalancer(name string) (*Balancer, error)

func NewBalancer

func NewBalancer(name string, strategy strategy.LoadBalancingStrategy) *Balancer

func NewBalancerL

func NewBalancerL(name string, strategy strategy.LoadBalancingStrategy, chanLen int) *Balancer

func (*Balancer) AddPerformer

func (b *Balancer) AddPerformer(name string, performer func(interface{}) (interface{}, error), fallback func(error) error, circuitBreakerConfig *CircuitBreakerConfig)

func (*Balancer) AddPerformerW

func (b *Balancer) AddPerformerW(name string, performer func(interface{}) (interface{}, error), fallback func(error) error, circuitBreakerConfig *CircuitBreakerConfig, weight int)

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	// Timeout is how long to wait for command to bitcoinMininode, in milliseconds
	Timeout int
	// MaxConcurrentRequests is how many commands of the same type can run at the same time
	MaxConcurrentRequests int
	// RequestVolumeThreshold is the minimum number of requests needed before a circuit can be tripped due to health
	RequestVolumeThreshold int
	// SleepWindow is how long, in milliseconds, to wait after a circuit opens before testing for recovery
	SleepWindow int
	// ErrorPercentThreshold causes circuits to open once the rolling measure of errors exceeds this percent of requests
	ErrorPercentThreshold int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL