Documentation
¶
Index ¶
- Constants
- func GetRetryFromContext(r *http.Request) int
- func TypeByURLPath(path string) string
- type Backend
- func (b *Backend) DecrementConnections()
- func (b *Backend) GetConnectionCount() int
- func (b *Backend) GetCurrentWeight() int
- func (b *Backend) GetURL() string
- func (b *Backend) GetWeight() int
- func (b *Backend) IncrementConnections() bool
- func (b *Backend) IsAlive() bool
- func (b *Backend) SetAlive(alive bool)
- func (b *Backend) SetCurrentWeight(weight int)
- type BackendSnapshot
- type BufferPool
- type HeaderHandler
- type PoolAlgorithm
- type PoolConfig
- type ProxyOption
- type Rewrite
- type Route
- type ServerPool
- func (s *ServerPool) AddBackend(cfg config.Backend, rc Route, hcCfg *config.HealthCheck) error
- func (s *ServerPool) GetAlgorithm() algorithm.Algorithm
- func (s *ServerPool) GetAllBackends() []*Backend
- func (s *ServerPool) GetBackendByURL(url string) *Backend
- func (s *ServerPool) GetBackends() []*algorithm.Server
- func (s *ServerPool) GetConfig() PoolConfig
- func (s *ServerPool) GetCurrentIndex() uint64
- func (s *ServerPool) GetMaxConnections() int32
- func (s *ServerPool) GetNextPeer() *Backend
- func (s *ServerPool) GetNextProxy(r *http.Request) *URLRewriteProxy
- func (s *ServerPool) MarkBackendStatus(backendUrl *url.URL, alive bool)
- func (s *ServerPool) RemoveBackend(backendURL string) error
- func (s *ServerPool) SetAlgorithm(algorithm *PoolAlgorithm)
- func (s *ServerPool) SetCurrentIndex(idx uint64)
- func (s *ServerPool) SetMaxConnections(maxConns int32)
- func (s *ServerPool) UpdateBackends(configs []config.Backend, serviceHealthCheck *config.HealthCheck) error
- func (s *ServerPool) UpdateConfig(update PoolConfig)
- type Transport
- type URLRewriteProxy
- type URLRewriter
Constants ¶
const ( StatusMovedPermanently = http.StatusMovedPermanently StatusFound = http.StatusFound StatusSeeOther = http.StatusSeeOther StatusTemporaryRedirect = http.StatusTemporaryRedirect StatusPermanentRedirect = http.StatusPermanentRedirect HeaderServer = "Server" // The Server header identifies the server software handling the request. HeaderLocation = "Location" // The Location header is used in redirection or when a new resource has been created. HeaderHost = "Host" // The Host header specifies the domain name of the server and the TCP port number on which the server is listening. HeaderXPoweredBy = "X-Powered-By" // The X-Powered-By header indicates technologies supporting the server. HeaderXProxyBy = "X-Proxy-By" // The X-Proxy-By header identifies the proxy handling the request. HeaderXRealIP = "X-Real-IP" // The X-Real-IP header identifies the originating IP address of a client HeaderXForwardedFor = "X-Forwarded-For" // The X-Forwarded-For header identifies the originating IP address of a client connecting to a web server through a proxy. HeaderXForwardedHost = "X-Forwarded-Host" // The X-Forwarded-Host header identifies the original host requested by the client. DefaultScheme = "http" DefaultProxyLabel = "terraster" )
Constants representing various HTTP status codes used for redirection.
const ( // DefaultMaxIdleConnsPerHost is the maximum number of idle connections to keep per-host DefaultMaxIdleConnsPerHost = 32 // DefaultIdleConnTimeout is the maximum amount of time an idle connection will remain idle before closing DefaultIdleConnTimeout = 30 * time.Second )
const ( // RetryKey is used as a key to store and retrieve retry counts from the request context. RetryKey contextKey = iota )
Variables ¶
This section is empty.
Functions ¶
func GetRetryFromContext ¶
GetRetryFromContext extracts the retry count from the request's context. If no retry count is present, it returns 0. This is used to track the number of retry attempts for a given request.
func TypeByURLPath ¶ added in v0.4.4
TypeByURLPath checks if provided URL path (image123.jpg) is in whitelised extensions
Types ¶
type Backend ¶
type Backend struct {
URL *url.URL // The URL of the backend server, including scheme, host, and port.
Host string // The hostname extracted from the URL, used for logging and identification.
Alive atomic.Bool // Atomic flag indicating whether the backend is currently alive and reachable.
Weight int // The weight assigned to the backend for load balancing purposes.
CurrentWeight atomic.Int32 // The current weight used in certain load balancing algorithms (e.g., weighted round-robin).
Proxy *URLRewriteProxy // The proxy instance responsible for handling HTTP requests to this backend.
ConnectionCount int32 // The current number of active connections to this backend.
MaxConnections int32 // The maximum number of concurrent connections allowed to this backend.
SuccessCount int32 // The total number of successful requests processed by this backend.
FailureCount int32 // The total number of failed requests processed by this backend.
HealthCheckCfg *config.HealthCheck // Configuration settings for health checks specific to this backend.
}
func (*Backend) DecrementConnections ¶
func (b *Backend) DecrementConnections()
DecrementConnections decrements the active connection count for the backend. This should be called when a connection to the backend is closed or terminated. It ensures that the connection count accurately reflects the current load.
func (*Backend) GetConnectionCount ¶
GetConnectionCount returns the current number of active connections to the backend.
func (*Backend) GetCurrentWeight ¶
GetCurrentWeight fetches the current weight of the backend.
func (*Backend) GetWeight ¶
GetWeight retrieves the current weight assigned to the backend. The weight influences the load balancing decision, determining the proportion of traffic this backend receives.
func (*Backend) IncrementConnections ¶
IncrementConnections attempts to increment the active connection count for the backend. It ensures that the connection count does not exceed the maximum allowed. Returns true if the increment was successful, or false if the backend is at maximum capacity.
func (*Backend) IsAlive ¶
IsAlive checks whether the backend is currently marked as alive. An alive backend is considered healthy and eligible to receive traffic.
func (*Backend) SetCurrentWeight ¶
SetCurrentWeight sets the current weight of the backend to the specified value.
type BackendSnapshot ¶ added in v0.1.5
type BackendSnapshot struct {
Backends []*Backend // Slice of all backend servers in the pool.
BackendCache map[string]*Backend // Map for quick access to backends by their URL string.
}
BackendSnapshot represents a snapshot of the current state of backends in the ServerPool.
type BufferPool ¶
BufferPool is a wrapper around sync.Pool that provides a pool of reusable byte slices.
func NewBufferPool ¶
func NewBufferPool() *BufferPool
NewBufferPool initializes and returns a new instance of BufferPool. It sets up the underlying sync.Pool with a default byte slice size of 32KB.
func (*BufferPool) Get ¶
func (b *BufferPool) Get() []byte
Get retrieves a byte slice from the BufferPool. If the pool is empty, it allocates a new byte slice using the New function defined in NewBufferPool.
func (*BufferPool) Put ¶
func (b *BufferPool) Put(buf []byte)
Put returns a byte slice back to the BufferPool for reuse. By recycling buffers, the application can significantly reduce memory fragmentation and garbage collection pressure.
type HeaderHandler ¶ added in v0.4.2
type HeaderHandler struct {
// contains filtered or unexported fields
}
HeaderHandler manages request and response header modifications
func NewHeaderHandler ¶ added in v0.4.2
func NewHeaderHandler(cfg config.Header) *HeaderHandler
NewHeaderHandler creates a new HeaderHandler
func (*HeaderHandler) ProcessRequestHeaders ¶ added in v0.4.2
func (h *HeaderHandler) ProcessRequestHeaders(req *http.Request)
ProcessRequestHeaders modifies the request headers
func (*HeaderHandler) ProcessResponseHeaders ¶ added in v0.4.2
func (h *HeaderHandler) ProcessResponseHeaders(resp *http.Response)
ProcessResponseHeaders modifies the response headers
type PoolAlgorithm ¶ added in v0.4.3
PoolAlgorithm wrapps backend load balancing algorithm
type PoolConfig ¶
type ProxyOption ¶
type ProxyOption func(*URLRewriteProxy)
ProxyOption defines a function type for applying optional configurations to URLRewriteProxy instances.
func WithHeaderConfig ¶ added in v0.4.1
func WithHeaderConfig(cfg *config.Header) ProxyOption
WithHeaderConfig sets custom req/res headers
func WithLogger ¶
func WithLogger(logger *zap.Logger) ProxyOption
WithLogger is configuring the URLRewriteProxy with a custom logger.
func WithPluginManager ¶ added in v0.4.5
func WithPluginManager(pm *plugin.Manager) ProxyOption
func WithURLRewriter ¶
func WithURLRewriter(config Route, backendURL *url.URL) ProxyOption
WithURLRewriter is configuring the URLRewriteProxy. It sets up a URL rewriter based on the provided Route and backend URL. This allows the proxy to modify incoming request URLs according to the specified rewrite rules, ensuring that requests are correctly routed to the intended backend services.
type Rewrite ¶ added in v0.4.6
type Rewrite struct {
ProxyPath string // The path prefix that the proxy should handle and potentially strip from incoming requests.
RewriteURL string // The URL to which the incoming request's path should be rewritten.
Redirect string // The URL to redirect the request to, if redirection is enabled.
}
Rewrite holds configuration settings for URL rewriting and redirection. It defines how incoming request paths should be transformed before being forwarded to the backend services.
type Route ¶ added in v0.4.6
type Route struct {
Path string // Path is the proxy path (upstream) used to match incoming requests (optional).
RewriteURL string // RewriteURL is the URL to rewrite the incoming request to (downstream) (optional).
Redirect string // Redirect is the URL to redirect the request to (optional).
SkipTLSVerify bool // SkipTLSVerify determines whether to skip TLS certificate verification for backend connections (optional).
SNI string // SNI (Server Name Indication) is the backend virtual host name separate from proxy server name
HTTP2 bool // HTTP2 enables proxy to force connect to backend server via HTTP/2 protocol
}
Route holds configuration settings for routing requests through the proxy.
type ServerPool ¶
type ServerPool struct {
// contains filtered or unexported fields
}
ServerPool manages a pool of backend servers, handling load balancing and connection management.
func NewServerPool ¶
func (*ServerPool) AddBackend ¶
func (s *ServerPool) AddBackend(cfg config.Backend, rc Route, hcCfg *config.HealthCheck) error
AddBackend adds a new backend to the ServerPool with the specified configuration, route settings, and health check configuration. Parses the backend URL, creates a reverse proxy, initializes the backend, and updates the BackendSnapshot atomically.
func (*ServerPool) GetAlgorithm ¶
func (s *ServerPool) GetAlgorithm() algorithm.Algorithm
GetAlgorithm returns the current load balancing algorithm used by the ServerPool.
func (*ServerPool) GetAllBackends ¶ added in v0.1.4
func (s *ServerPool) GetAllBackends() []*Backend
GetAllBackends returns a slice of all backends currently managed by the ServerPool.
func (*ServerPool) GetBackendByURL ¶
func (s *ServerPool) GetBackendByURL(url string) *Backend
GetBackendByURL retrieves a backend from the pool based on its URL. Returns the Backend if found, otherwise returns nil.
func (*ServerPool) GetBackends ¶
func (s *ServerPool) GetBackends() []*algorithm.Server
GetBackends returns a slice of all backend servers converted to algorithm.Server type for use in load balancing algorithms.
func (*ServerPool) GetConfig ¶
func (s *ServerPool) GetConfig() PoolConfig
GetConfig retrieves the current configuration of the ServerPool, including the load balancing algorithm and maximum connections.
func (*ServerPool) GetCurrentIndex ¶
func (s *ServerPool) GetCurrentIndex() uint64
GetCurrentIndex retrieves the current index used for round-robin load balancing.
func (*ServerPool) GetMaxConnections ¶
func (s *ServerPool) GetMaxConnections() int32
GetMaxConnections retrieves the current maximum number of connections allowed per backend.
func (*ServerPool) GetNextPeer ¶
func (s *ServerPool) GetNextPeer() *Backend
GetNextPeer selects the next available backend based on the current load balancing algorithm. It returns the selected backend or nil if no suitable backend is available.
func (*ServerPool) GetNextProxy ¶
func (s *ServerPool) GetNextProxy(r *http.Request) *URLRewriteProxy
GetNextProxy retrieves the next available backend proxy based on the load balancing algorithm and increments its connection count. Returns the selected URLRewriteProxy or nil if no suitable backend is available.
func (*ServerPool) MarkBackendStatus ¶
func (s *ServerPool) MarkBackendStatus(backendUrl *url.URL, alive bool)
MarkBackendStatus updates the alive status of a backend based on its URL. It is used by health checkers to mark backends as alive or dead.
func (*ServerPool) RemoveBackend ¶
func (s *ServerPool) RemoveBackend(backendURL string) error
RemoveBackend removes an existing backend from the ServerPool based on its URL. It updates the BackendSnapshot atomically to exclude the specified backend. Returns an error if the backend URL is invalid or if the backend does not exist in the pool.
func (*ServerPool) SetAlgorithm ¶
func (s *ServerPool) SetAlgorithm(algorithm *PoolAlgorithm)
SetAlgorithm sets a new load balancing algorithm for the ServerPool.
func (*ServerPool) SetCurrentIndex ¶
func (s *ServerPool) SetCurrentIndex(idx uint64)
SetCurrentIndex sets the current index used for round-robin load balancing.
func (*ServerPool) SetMaxConnections ¶
func (s *ServerPool) SetMaxConnections(maxConns int32)
SetMaxConnections sets a new maximum number of connections allowed per backend.
func (*ServerPool) UpdateBackends ¶
func (s *ServerPool) UpdateBackends(configs []config.Backend, serviceHealthCheck *config.HealthCheck) error
UpdateBackends completely replaces the existing list of backends with a new set based on the provided configurations. It updates the load balancing algorithm and health check settings for each backend. Returns an error if any backend configuration is invalid.
func (*ServerPool) UpdateConfig ¶
func (s *ServerPool) UpdateConfig(update PoolConfig)
UpdateConfig updates the ServerPool's configuration based on the provided PoolConfig. It allows changing the load balancing algorithm and the maximum number of connections dynamically.
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport provides a custom implementation of http.RoundTripper that wraps the standard http.Transport with additional configuration options for TLS, connection pooling, and HTTP/2 support.
func NewTransport ¶
NewTransport creates and returns a new Transport instance with default configuration. The default configuration includes: - Connection pooling with DefaultMaxIdleConnsPerHost idle connections per host - Idle connection timeout set to DefaultIdleConnTimeout - Initialized TLS configuration
func (*Transport) ConfigureTransport ¶ added in v0.4.4
ConfigureTransport sets up TLS and HTTP/2 settings for the transport. It configures SNI (Server Name Indication), TLS verification, and HTTP/2 support. When h2 is false, the transport will be configured to use HTTP/1.1 exclusively
func (*Transport) GetTransport ¶ added in v0.4.4
func (t *Transport) GetTransport() http.RoundTripper
GetTransport returns the underlying http.Transport.
type URLRewriteProxy ¶
type URLRewriteProxy struct {
// contains filtered or unexported fields
}
URLRewriteProxy is a custom reverse proxy that handles URL rewriting and redirection based on RouteConfig.
func NewReverseProxy ¶
func NewReverseProxy( target *url.URL, config Route, px *httputil.ReverseProxy, logger *zap.Logger, opts ...ProxyOption, ) *URLRewriteProxy
This sets up the reverse proxy with the specified target, route configurations, and applies any additional proxy options. The function also configures the reverse proxy's Director, ModifyResponse, Transport, ErrorHandler, and BufferPool.
func (*URLRewriteProxy) ServeHTTP ¶
func (p *URLRewriteProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles incoming HTTP requests by determining whether to redirect or proxy the request. If a redirect is necessary based on the URLRewriter's logic, it performs the redirection. Otherwise, it forwards the request to the configured backend proxy.
type URLRewriter ¶
type URLRewriter struct {
// contains filtered or unexported fields
}
URLRewriter is responsible for rewriting incoming request URLs based on predefined rules. It handles path prefix stripping, URL rewriting, and redirects to ensure that requests are correctly routed to the appropriate backend services.
func NewURLRewriter ¶
func NewURLRewriter(config Rewrite, backendURL *url.URL) *URLRewriter
NewURLRewriter initializes and returns a new instance of URLRewriter based on the provided configuration. Determines whether the path prefix should be stripped and sets up the necessary rewrite and redirect rules.