Documentation ¶
Overview ¶
A simple reverse proxy and load balancer
supported balancing strategies:
- Round Robin
- Random
- Least Connected
Example of a json config:
{ "servers": [ { "name": "server1", "port": 7777, "services": [ { "route_prefix": "/microservice/", "instances": [ { "host": "127.0.0.1:1111", "weight": 3 }, { "host": "127.0.0.1:2222" }, { "host": "127.0.0.1:3333" } ], "balancing_strategy": "round_robin", "max_connections": 300, "health_check": { "enabled": true, "interval": 30 } } ] } ] }
Index ¶
- Constants
- Variables
- func ListenAndServe(p string) error
- func ListenAndServeConfig(c *Config) error
- func NewProxy() *httputil.ReverseProxy
- func NewPylonHandler(p *Pylon) http.HandlerFunc
- func NewPylonHealthHandler(p *Pylon) http.HandlerFunc
- func SetDebugLogger(l Logger)
- func SetErrorLogger(l Logger)
- func SetInfoLogger(l Logger)
- func SetLogLevels(mask int8)
- func SetLogWriter(w io.Writer)
- func SetVerboseLogger(l Logger)
- type Config
- type ConfigParser
- type Error
- type HealthCheck
- type Instance
- type InstanceRender
- type JSONConfigParser
- type Logger
- type MicroService
- type PrefixRoute
- type ProxyPool
- type Pylon
- type PylonTransport
- type RegexRoute
- type Route
- type RouteType
- type Server
- type Service
- type ServiceRender
- type SharedInt
- type Strategy
Constants ¶
const ( LOG_DEBUG int8 = 1 << 0 LOG_ERROR int8 = 1 << 1 LOG_INFO int8 = 1 << 2 LOG_VERBOSE int8 = 1 << 3 LOG_NONE int8 = 0 LOG_EXCEPT_VERBOSE int8 = LOG_DEBUG | LOG_ERROR | LOG_INFO LOG_ALL int8 = LOG_DEBUG | LOG_ERROR | LOG_INFO | LOG_VERBOSE )
const ( ErrServiceNoRouteCode = iota + 30 ErrInvalidRouteTypeCode ErrRouteNoRouteCode ErrServiceNoInstanceCode ErrAllInstancesDownCode ErrInvalidStrategyCode ErrFailedRoundRobinCode ErrInvalidRouteRegexCode ErrInvalidHostCode )
Variables ¶
var ( ErrServiceNoRoute = NewError(ErrServiceNoRouteCode, "Service has no route") ErrInvalidRouteType = NewError(ErrInvalidRouteTypeCode, "Route has invalid type") ErrServiceNoInstance = NewError(ErrServiceNoInstanceCode, "Service has no instances") ErrAllInstancesDown = NewError(ErrAllInstancesDownCode, "All instances are dead") ErrFailedRoundRobin = NewError(ErrFailedRoundRobinCode, "No instance can be round robin picked") )
Functions ¶
func ListenAndServe ¶
ListenAndServe tries to parse the config at the given path and serve it
func ListenAndServeConfig ¶
ListenAndServeConfig converts a given config to an exploitable structure (MicroService) and serves them
func NewProxy ¶
func NewProxy() *httputil.ReverseProxy
func NewPylonHandler ¶
func NewPylonHandler(p *Pylon) http.HandlerFunc
NewPylonHandler returns a func(w http.ResponseWriter, r *http.Request) that will handle incoming requests to the given Pylon
func NewPylonHealthHandler ¶
func NewPylonHealthHandler(p *Pylon) http.HandlerFunc
NewPylonHealthHandler returns a func(w http.ResponseWriter, r *http.Request) that will collect and render some stats about the given Pylon: (Name / Strategy / Current request count) For every instance: (UP or DOWN / Host / Weight / Current request count)
func SetDebugLogger ¶
func SetDebugLogger(l Logger)
func SetErrorLogger ¶
func SetErrorLogger(l Logger)
func SetInfoLogger ¶
func SetInfoLogger(l Logger)
func SetLogLevels ¶
func SetLogLevels(mask int8)
func SetLogWriter ¶
func SetVerboseLogger ¶
func SetVerboseLogger(l Logger)
Types ¶
type HealthCheck ¶
type InstanceRender ¶
type JSONConfigParser ¶
type JSONConfigParser struct { }
func (*JSONConfigParser) Parse ¶
func (p *JSONConfigParser) Parse(r io.Reader) (c *Config, err error)
func (*JSONConfigParser) ParseFromPath ¶
func (p *JSONConfigParser) ParseFromPath(path string) (c *Config, err error)
type MicroService ¶
type MicroService struct { Name string Route Route Instances []*Instance Strategy Strategy LastUsedIdx SharedInt BlackList map[int]bool ReqCount chan int // Caching the weight sum for faster retrieval WeightSum float32 Mutex *sync.RWMutex HealthCheck HealthCheck }
func NewMicroService ¶
func NewMicroService(s *Service) (*MicroService, error)
NewMicroService returns a new MicroService object given a Service
type PrefixRoute ¶
type PrefixRoute struct {
Prefix string
}
func (PrefixRoute) Data ¶
func (p PrefixRoute) Data() interface{}
func (PrefixRoute) Type ¶
func (p PrefixRoute) Type() RouteType
type ProxyPool ¶
type ProxyPool struct {
// contains filtered or unexported fields
}
func NewProxyPool ¶
func (*ProxyPool) Get ¶
func (p *ProxyPool) Get() *httputil.ReverseProxy
func (*ProxyPool) Put ¶
func (p *ProxyPool) Put(rp *httputil.ReverseProxy)
type Pylon ¶
type Pylon struct {
Services []*MicroService
}
type PylonTransport ¶
type PylonTransport struct {
// contains filtered or unexported fields
}
func NewPylonTransport ¶
func NewPylonTransport() *PylonTransport
type RegexRoute ¶
func (RegexRoute) Data ¶
func (r RegexRoute) Data() interface{}
func (RegexRoute) Type ¶
func (r RegexRoute) Type() RouteType
type ServiceRender ¶
type ServiceRender struct { Name string CurrConn int Strat Strategy Instances []InstanceRender }
type SharedInt ¶
type SharedInt struct { // contains filtered or unexported fields }