Documentation ¶
Overview ¶
Package proxies provides the common interface and implementation of proxies.
Index ¶
- Constants
- type GeneralLoadBalancer
- func (glb *GeneralLoadBalancer) ChooseServer(req protocols.Request) *Server
- func (glb *GeneralLoadBalancer) Close()
- func (glb *GeneralLoadBalancer) Init(fnNewSessionSticker func(*StickySessionSpec) SessionSticker, ...)
- func (glb *GeneralLoadBalancer) ReturnServer(server *Server, req protocols.Request, resp protocols.Response)
- type HTTPHealthChecker
- type HTTPSessionSticker
- type HeaderHashLoadBalancePolicy
- type HealthCheckSpec
- type HealthChecker
- type IPHashLoadBalancePolicy
- type LoadBalancePolicy
- type LoadBalanceSpec
- type LoadBalancer
- type RandomLoadBalancePolicy
- type RequestMatcher
- type RequestMatcherBaseSpec
- type RoundRobinLoadBalancePolicy
- type Server
- type ServerGroup
- type ServerPoolBase
- type ServerPoolBaseSpec
- type ServerPoolImpl
- type SessionSticker
- type StickySessionSpec
- type WeightedRandomLoadBalancePolicy
Constants ¶
const ( // LoadBalancePolicyRoundRobin is the load balance policy of round robin. LoadBalancePolicyRoundRobin = "roundRobin" // LoadBalancePolicyRandom is the load balance policy of random. LoadBalancePolicyRandom = "random" // LoadBalancePolicyWeightedRandom is the load balance policy of weighted random. LoadBalancePolicyWeightedRandom = "weightedRandom" // LoadBalancePolicyIPHash is the load balance policy of IP hash. LoadBalancePolicyIPHash = "ipHash" // LoadBalancePolicyHeaderHash is the load balance policy of HTTP header hash. LoadBalancePolicyHeaderHash = "headerHash" )
const ( // StickySessionModeCookieConsistentHash is the sticky session mode of consistent hash on app cookie. StickySessionModeCookieConsistentHash = "CookieConsistentHash" // StickySessionModeDurationBased uses a load balancer-generated cookie for stickiness. StickySessionModeDurationBased = "DurationBased" // StickySessionModeApplicationBased uses a load balancer-generated cookie depends on app cookie for stickiness. StickySessionModeApplicationBased = "ApplicationBased" // StickySessionDefaultLBCookieName is the default name of the load balancer-generated cookie. StickySessionDefaultLBCookieName = "EG_SESSION" // KeyLen is the key length used by HMAC. KeyLen = 8 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GeneralLoadBalancer ¶
type GeneralLoadBalancer struct {
// contains filtered or unexported fields
}
GeneralLoadBalancer implements a general purpose load balancer.
func NewGeneralLoadBalancer ¶
func NewGeneralLoadBalancer(spec *LoadBalanceSpec, servers []*Server) *GeneralLoadBalancer
NewGeneralLoadBalancer creates a new GeneralLoadBalancer.
func (*GeneralLoadBalancer) ChooseServer ¶
func (glb *GeneralLoadBalancer) ChooseServer(req protocols.Request) *Server
ChooseServer chooses a server according to the load balancing spec.
func (*GeneralLoadBalancer) Close ¶
func (glb *GeneralLoadBalancer) Close()
Close closes the load balancer
func (*GeneralLoadBalancer) Init ¶
func (glb *GeneralLoadBalancer) Init( fnNewSessionSticker func(*StickySessionSpec) SessionSticker, fnNewHealthChecker func(*HealthCheckSpec) HealthChecker, lbp LoadBalancePolicy, )
Init initializes the load balancer.
func (*GeneralLoadBalancer) ReturnServer ¶
func (glb *GeneralLoadBalancer) ReturnServer(server *Server, req protocols.Request, resp protocols.Response)
ReturnServer returns a server to the load balancer.
type HTTPHealthChecker ¶
type HTTPHealthChecker struct {
// contains filtered or unexported fields
}
HTTPHealthChecker is a health checker for HTTP protocol.
func (*HTTPHealthChecker) Check ¶
func (hc *HTTPHealthChecker) Check(svr *Server) bool
Check checks whether a server is healthy or not.
func (*HTTPHealthChecker) Close ¶
func (hc *HTTPHealthChecker) Close()
Close closes the health checker
type HTTPSessionSticker ¶
type HTTPSessionSticker struct {
// contains filtered or unexported fields
}
HTTPSessionSticker implements sticky session for HTTP.
func (*HTTPSessionSticker) Close ¶
func (ss *HTTPSessionSticker) Close()
Close closes the HTTPSessionSticker.
func (*HTTPSessionSticker) GetServer ¶
func (ss *HTTPSessionSticker) GetServer(req protocols.Request, sg *ServerGroup) *Server
GetServer returns the server for the request.
func (*HTTPSessionSticker) ReturnServer ¶
func (ss *HTTPSessionSticker) ReturnServer(server *Server, req protocols.Request, resp protocols.Response)
ReturnServer returns the server to the session sticker.
func (*HTTPSessionSticker) UpdateServers ¶
func (ss *HTTPSessionSticker) UpdateServers(servers []*Server)
UpdateServers update the servers for the HTTPSessionSticker.
type HeaderHashLoadBalancePolicy ¶
type HeaderHashLoadBalancePolicy struct {
// contains filtered or unexported fields
}
HeaderHashLoadBalancePolicy is a load balance policy that chooses a server by header hash.
func (*HeaderHashLoadBalancePolicy) ChooseServer ¶
func (lbp *HeaderHashLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server
ChooseServer chooses a server by header hash.
type HealthCheckSpec ¶
type HealthCheckSpec struct { // Interval is the interval duration for health check. Interval string `json:"interval" jsonschema:"omitempty,format=duration"` // Path is the health check path for server Path string `json:"path" jsonschema:"omitempty"` // Timeout is the timeout duration for health check, default is 3. Timeout string `json:"timeout" jsonschema:"omitempty,format=duration"` // Fails is the consecutive fails count for assert fail, default is 1. Fails int `json:"fails" jsonschema:"omitempty,minimum=1"` // Passes is the consecutive passes count for assert pass, default is 1. Passes int `json:"passes" jsonschema:"omitempty,minimum=1"` }
HealthCheckSpec is the spec for health check.
type HealthChecker ¶
HealthChecker checks whether a server is healthy or not.
func NewHTTPHealthChecker ¶
func NewHTTPHealthChecker(spec *HealthCheckSpec) HealthChecker
NewHTTPHealthChecker creates a new HTTPHealthChecker.
type IPHashLoadBalancePolicy ¶
type IPHashLoadBalancePolicy struct { }
IPHashLoadBalancePolicy is a load balance policy that chooses a server by ip hash.
func (*IPHashLoadBalancePolicy) ChooseServer ¶
func (lbp *IPHashLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server
ChooseServer chooses a server by ip hash.
type LoadBalancePolicy ¶
type LoadBalancePolicy interface {
ChooseServer(req protocols.Request, sg *ServerGroup) *Server
}
LoadBalancePolicy is the interface of a load balance policy.
type LoadBalanceSpec ¶
type LoadBalanceSpec struct { Policy string `json:"policy" jsonschema:"omitempty"` HeaderHashKey string `json:"headerHashKey" jsonschema:"omitempty"` ForwardKey string `json:"forwardKey" jsonschema:"omitempty"` StickySession *StickySessionSpec `json:"stickySession" jsonschema:"omitempty"` HealthCheck *HealthCheckSpec `json:"healthCheck" jsonschema:"omitempty"` }
LoadBalanceSpec is the spec to create a load balancer.
TODO: this spec currently include all options for all load balance policies, this is not good as new policies could be added in the future, we should convert it to a map later.
type LoadBalancer ¶
type LoadBalancer interface { ChooseServer(req protocols.Request) *Server ReturnServer(server *Server, req protocols.Request, resp protocols.Response) Close() }
LoadBalancer is the interface of a load balancer.
type RandomLoadBalancePolicy ¶
type RandomLoadBalancePolicy struct { }
RandomLoadBalancePolicy is a load balance policy that chooses a server randomly.
func (*RandomLoadBalancePolicy) ChooseServer ¶
func (lbp *RandomLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server
ChooseServer chooses a server randomly.
type RequestMatcher ¶
RequestMatcher is the interface to match requests.
func NewRequestMatcher ¶
func NewRequestMatcher(spec *RequestMatcherBaseSpec) RequestMatcher
NewRequestMatcher creates a new traffic matcher according to spec.
type RequestMatcherBaseSpec ¶
type RequestMatcherBaseSpec struct { Policy string `json:"policy" jsonschema:"omitempty,enum=,enum=general,enum=ipHash,enum=headerHash,enum=random"` MatchAllHeaders bool `json:"matchAllHeaders" jsonschema:"omitempty"` Headers map[string]*stringtool.StringMatcher `json:"headers" jsonschema:"omitempty"` Permil uint32 `json:"permil" jsonschema:"omitempty,minimum=0,maximum=1000"` HeaderHashKey string `json:"headerHashKey" jsonschema:"omitempty"` }
RequestMatcherBaseSpec describe RequestMatcher
func (*RequestMatcherBaseSpec) Validate ¶
func (s *RequestMatcherBaseSpec) Validate() error
Validate validates the RequestMatcherBaseSpec.
type RoundRobinLoadBalancePolicy ¶
type RoundRobinLoadBalancePolicy struct {
// contains filtered or unexported fields
}
RoundRobinLoadBalancePolicy is a load balance policy that chooses a server by round robin.
func (*RoundRobinLoadBalancePolicy) ChooseServer ¶
func (lbp *RoundRobinLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server
ChooseServer chooses a server by round robin.
type Server ¶
type Server struct { URL string `json:"url" jsonschema:"required,format=url"` Tags []string `json:"tags" jsonschema:"omitempty,uniqueItems=true"` Weight int `json:"weight" jsonschema:"omitempty,minimum=0,maximum=100"` KeepHost bool `json:"keepHost" jsonschema:"omitempty,default=false"` AddrIsHostName bool `json:"-"` Unhealth bool `json:"-"` // HealthCounter is used to count the number of successive health checks // result, positive for healthy, negative for unhealthy HealthCounter int `json:"-"` }
Server is a backend proxy server.
func (*Server) CheckAddrPattern ¶
func (s *Server) CheckAddrPattern()
CheckAddrPattern checks whether the server address is host name or ip:port, not all error cases are handled.
type ServerGroup ¶
ServerGroup is a group of servers.
type ServerPoolBase ¶
type ServerPoolBase struct { Name string // contains filtered or unexported fields }
ServerPoolBase defines a base server pool.
func (*ServerPoolBase) Done ¶
func (spb *ServerPoolBase) Done() <-chan struct{}
Done returns the done channel, which indicates the closing of the server pool.
func (*ServerPoolBase) Init ¶
func (spb *ServerPoolBase) Init(spImpl ServerPoolImpl, super *supervisor.Supervisor, name string, spec *ServerPoolBaseSpec)
Init initialize the base server pool according to the spec.
func (*ServerPoolBase) LoadBalancer ¶
func (spb *ServerPoolBase) LoadBalancer() LoadBalancer
LoadBalancer returns the load balancer of the server pool.
type ServerPoolBaseSpec ¶
type ServerPoolBaseSpec struct { ServerTags []string `json:"serverTags" jsonschema:"omitempty,uniqueItems=true"` Servers []*Server `json:"servers" jsonschema:"omitempty"` ServiceRegistry string `json:"serviceRegistry" jsonschema:"omitempty"` ServiceName string `json:"serviceName" jsonschema:"omitempty"` LoadBalance *LoadBalanceSpec `json:"loadBalance" jsonschema:"omitempty"` }
ServerPoolBaseSpec is the spec for a base server pool.
func (*ServerPoolBaseSpec) Validate ¶
func (sps *ServerPoolBaseSpec) Validate() error
Validate validates ServerPoolSpec.
type ServerPoolImpl ¶
type ServerPoolImpl interface {
CreateLoadBalancer(spec *LoadBalanceSpec, servers []*Server) LoadBalancer
}
ServerPoolImpl is the interface for server pool.
type SessionSticker ¶
type SessionSticker interface { UpdateServers(servers []*Server) GetServer(req protocols.Request, sg *ServerGroup) *Server ReturnServer(server *Server, req protocols.Request, resp protocols.Response) Close() }
SessionSticker is the interface for session stickiness.
func NewHTTPSessionSticker ¶
func NewHTTPSessionSticker(spec *StickySessionSpec) SessionSticker
NewHTTPSessionSticker creates a new HTTPSessionSticker.
type StickySessionSpec ¶
type StickySessionSpec struct { Mode string `json:"mode" jsonschema:"required,enum=CookieConsistentHash,enum=DurationBased,enum=ApplicationBased"` // AppCookieName is the user-defined cookie name in CookieConsistentHash and ApplicationBased mode. AppCookieName string `json:"appCookieName" jsonschema:"omitempty"` // LBCookieName is the generated cookie name in DurationBased and ApplicationBased mode. LBCookieName string `json:"lbCookieName" jsonschema:"omitempty"` // LBCookieExpire is the expire seconds of generated cookie in DurationBased and ApplicationBased mode. LBCookieExpire string `json:"lbCookieExpire" jsonschema:"omitempty,format=duration"` }
StickySessionSpec is the spec for sticky session.
type WeightedRandomLoadBalancePolicy ¶
type WeightedRandomLoadBalancePolicy struct { }
WeightedRandomLoadBalancePolicy is a load balance policy that chooses a server randomly by weight.
func (*WeightedRandomLoadBalancePolicy) ChooseServer ¶
func (lbp *WeightedRandomLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server
ChooseServer chooses a server randomly by weight.