proxies

package
v2.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package proxies provides the common interface and implementation of proxies.

Index

Constants

View Source
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"
	// LoadBalancePolicyCookieHash is the load balance policy of HTTP cookie hash,
	// which is the shorthand of headerHash with hash key Set-Cookie.
	LoadBalancePolicyCookieHash = "cookieHash"
)
View Source
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,
	hc 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 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,omitempty" jsonschema:"format=duration"`
	// Timeout is the timeout duration for health check, default is 3.
	Timeout string `json:"timeout,omitempty" jsonschema:"format=duration"`
	// Fails is the consecutive fails count for assert fail, default is 1.
	Fails int `json:"fails,omitempty" jsonschema:"minimum=1"`
	// Passes is the consecutive passes count for assert pass, default is 1.
	Passes int `json:"passes,omitempty" jsonschema:"minimum=1"`

	// Deprecated: other fields in this struct are general, but this field is
	// specific to HTTP health check. It should be moved to HTTP health check.
	// In HTTP health check, we should use URI instead of path.
	Path string `json:"path,omitempty"`
}

HealthCheckSpec is the spec for health check.

func (*HealthCheckSpec) GetInterval added in v2.6.4

func (s *HealthCheckSpec) GetInterval() time.Duration

GetInterval returns the interval duration.

func (*HealthCheckSpec) GetTimeout added in v2.6.4

func (s *HealthCheckSpec) GetTimeout() time.Duration

GetTimeout returns the timeout duration.

type HealthChecker

type HealthChecker interface {
	BaseSpec() HealthCheckSpec
	Check(svr *Server) bool
	Close()
}

HealthChecker checks whether a server is healthy or not.

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,omitempty"`
	HeaderHashKey string             `json:"headerHashKey,omitempty"`
	ForwardKey    string             `json:"forwardKey,omitempty"`
	StickySession *StickySessionSpec `json:"stickySession,omitempty"`
	// Deprecated: HealthCheck is protocol related. It should be moved to protocol spec.
	// This one is kept for backward compatibility.
	HealthCheck *HealthCheckSpec `json:"healthCheck,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

type RequestMatcher interface {
	Match(req protocols.Request) bool
}

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,omitempty" jsonschema:"enum=,enum=general,enum=ipHash,enum=headerHash,enum=random"`
	MatchAllHeaders bool                                 `json:"matchAllHeaders,omitempty"`
	Headers         map[string]*stringtool.StringMatcher `json:"headers,omitempty"`
	Permil          uint32                               `json:"permil,omitempty" jsonschema:"minimum=0,maximum=1000"`
	HeaderHashKey   string                               `json:"headerHashKey,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,omitempty" jsonschema:"uniqueItems=true"`
	Weight         int      `json:"weight,omitempty" jsonschema:"minimum=0,maximum=100"`
	KeepHost       bool     `json:"keepHost,omitempty" jsonschema:"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.

func (*Server) Healthy

func (s *Server) Healthy() bool

Healthy returns whether the server is healthy

func (*Server) ID

func (s *Server) ID() string

ID return identifier for server

func (*Server) String

func (s *Server) String() string

String implements the Stringer interface.

type ServerGroup

type ServerGroup struct {
	TotalWeight int
	Servers     []*Server
}

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) Close

func (spb *ServerPoolBase) Close()

Close closes the 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,omitempty" jsonschema:"uniqueItems=true"`
	Servers         []*Server        `json:"servers,omitempty"`
	SetUpstreamHost bool             `json:"setUpstreamHost,omitempty"`
	ServiceRegistry string           `json:"serviceRegistry,omitempty"`
	ServiceName     string           `json:"serviceName,omitempty"`
	LoadBalance     *LoadBalanceSpec `json:"loadBalance,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,omitempty"`
	// LBCookieName is the generated cookie name in DurationBased and ApplicationBased mode.
	LBCookieName string `json:"lbCookieName,omitempty"`
	// LBCookieExpire is the expire seconds of generated cookie in DurationBased and ApplicationBased mode.
	LBCookieExpire string `json:"lbCookieExpire,omitempty" jsonschema:"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

ChooseServer chooses a server randomly by weight.

Directories

Path Synopsis
Package grpcproxy provides the proxy filter of gRPC.
Package grpcproxy provides the proxy filter of gRPC.
Package httpproxy provides the Proxy of HTTP.
Package httpproxy provides the Proxy of HTTP.

Jump to

Keyboard shortcuts

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