Documentation
¶
Overview ¶
Package proxy provides the core reverse proxy engine with support for HTTP, gRPC, WebSocket, and SSE protocols.
Index ¶
- Constants
- func ProxyGRPC(w http.ResponseWriter, r *http.Request, route *bastion.Route, ...)
- func ProxySSE(w http.ResponseWriter, r *http.Request, route *bastion.Route, ...)
- func ProxyWebSocket(w http.ResponseWriter, r *http.Request, route *bastion.Route, ...)
- type Engine
- func (pe *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (pe *Engine) SetAuth(auth *bastion.GatewayAuth)
- func (pe *Engine) SetCache(cache *bastion.ResponseCache)
- func (pe *Engine) SetMetrics(m *observability.Metrics)
- func (pe *Engine) SetRetryPolicy(p bastion.RetryPolicy)
- func (pe *Engine) SetTLSManager(tlsMgr *bastion.TLSManager)
- func (pe *Engine) SetTrafficRouter(t bastion.TrafficRouter)
- type GRPCProxy
- type StatsCollector
- func (sc *StatsCollector) RecordCacheHit()
- func (sc *StatsCollector) RecordCacheMiss()
- func (sc *StatsCollector) RecordCircuitBreak()
- func (sc *StatsCollector) RecordError(routeID string)
- func (sc *StatsCollector) RecordLatency(routeID string, d time.Duration)
- func (sc *StatsCollector) RecordRateLimited()
- func (sc *StatsCollector) RecordRequest(routeID, path string)
- func (sc *StatsCollector) RecordRetryAttempt()
- func (sc *StatsCollector) RecordRetryBudgetExhausted()
- func (sc *StatsCollector) Snapshot(routes []*bastion.Route) *bastion.GatewayStats
- type TransportPool
- type TransportPoolConfig
Constants ¶
const GatewayLatencyWindow = 4096
GatewayLatencyWindow is how many recent responses the gateway-wide p99 covers.
const RouteLatencyWindow = 1024
RouteLatencyWindow is how many recent responses a route's p99 covers.
Variables ¶
This section is empty.
Functions ¶
func ProxyGRPC ¶
func ProxyGRPC(w http.ResponseWriter, r *http.Request, route *bastion.Route, target *bastion.Target, config bastion.Config, logger forge.Logger)
ProxyGRPC proxies a gRPC request to the upstream target. It handles:
- gRPC metadata (headers) propagation
- gRPC deadline/timeout propagation
- Unary, server-streaming, client-streaming, and bidirectional streaming RPCs
- gRPC trailers
- gRPC status codes
func ProxySSE ¶
func ProxySSE( w http.ResponseWriter, r *http.Request, route *bastion.Route, target *bastion.Target, config bastion.Config, logger forge.Logger, )
ProxySSE proxies a Server-Sent Events connection to an upstream target.
func ProxyWebSocket ¶
func ProxyWebSocket( w http.ResponseWriter, r *http.Request, route *bastion.Route, target *bastion.Target, config bastion.WebSocketConfig, logger forge.Logger, )
ProxyWebSocket proxies a WebSocket connection to an upstream target.
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine handles HTTP reverse proxying to upstream targets.
func NewEngine ¶
func NewEngine( config bastion.Config, logger forge.Logger, rm bastion.RouteRegistry, hm *health.Monitor, cbm bastion.CircuitControl, rl *bastion.RateLimiter, stats *StatsCollector, hooks *bastion.HookEngine, lb bastion.LoadBalancer, ) *Engine
NewEngine creates a new proxy engine.
func (*Engine) ServeHTTP ¶
func (pe *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the main gateway handler.
func (*Engine) SetAuth ¶
func (pe *Engine) SetAuth(auth *bastion.GatewayAuth)
SetAuth sets the gateway auth handler on the proxy engine.
func (*Engine) SetCache ¶
func (pe *Engine) SetCache(cache *bastion.ResponseCache)
SetCache sets the response cache on the proxy engine.
func (*Engine) SetMetrics ¶
func (pe *Engine) SetMetrics(m *observability.Metrics)
SetMetrics sets where the engine records its metrics. Nil records none.
func (*Engine) SetRetryPolicy ¶
func (pe *Engine) SetRetryPolicy(p bastion.RetryPolicy)
SetRetryPolicy sets the policy that decides whether a failed upstream attempt is retried and how long to wait first. Without one the proxy sends every request once.
func (*Engine) SetTLSManager ¶
func (pe *Engine) SetTLSManager(tlsMgr *bastion.TLSManager)
SetTLSManager sets the TLS manager on the proxy engine.
func (*Engine) SetTrafficRouter ¶
func (pe *Engine) SetTrafficRouter(t bastion.TrafficRouter)
SetTrafficRouter sets the router that applies a route's TrafficPolicy: which targets a request may go to, and where to mirror it. Without one every target of the route is a candidate and nothing is mirrored.
type GRPCProxy ¶
type GRPCProxy struct {
// contains filtered or unexported fields
}
GRPCProxy handles gRPC reverse proxying for both unary and streaming RPCs. It works at the HTTP/2 transport layer, forwarding raw gRPC frames between client and upstream, preserving headers, trailers, and streaming semantics.
func NewGRPCProxy ¶
NewGRPCProxy creates a new gRPC reverse proxy engine.
type StatsCollector ¶
type StatsCollector struct {
// contains filtered or unexported fields
}
StatsCollector collects gateway-level statistics.
func NewStatsCollector ¶
func NewStatsCollector() *StatsCollector
NewStatsCollector creates a new stats collector.
func (*StatsCollector) RecordCacheHit ¶
func (sc *StatsCollector) RecordCacheHit()
RecordCacheHit records a cache hit.
func (*StatsCollector) RecordCacheMiss ¶
func (sc *StatsCollector) RecordCacheMiss()
RecordCacheMiss records a cache miss.
func (*StatsCollector) RecordCircuitBreak ¶
func (sc *StatsCollector) RecordCircuitBreak()
RecordCircuitBreak records a circuit breaker activation.
func (*StatsCollector) RecordError ¶
func (sc *StatsCollector) RecordError(routeID string)
RecordError records an error for a route.
func (*StatsCollector) RecordLatency ¶
func (sc *StatsCollector) RecordLatency(routeID string, d time.Duration)
RecordLatency records how long an upstream took to answer a proxied HTTP request. Cache hits, transport errors and streaming protocols are not recorded, so latency describes answered upstream requests only.
func (*StatsCollector) RecordRateLimited ¶
func (sc *StatsCollector) RecordRateLimited()
RecordRateLimited records a rate-limited request.
func (*StatsCollector) RecordRequest ¶
func (sc *StatsCollector) RecordRequest(routeID, path string)
RecordRequest records a request for a route.
func (*StatsCollector) RecordRetryAttempt ¶
func (sc *StatsCollector) RecordRetryAttempt()
RecordRetryAttempt records a retry attempt.
func (*StatsCollector) RecordRetryBudgetExhausted ¶
func (sc *StatsCollector) RecordRetryBudgetExhausted()
RecordRetryBudgetExhausted records a retry the retry budget refused.
func (*StatsCollector) Snapshot ¶
func (sc *StatsCollector) Snapshot(routes []*bastion.Route) *bastion.GatewayStats
Snapshot returns the current gateway stats.
type TransportPool ¶
type TransportPool struct {
// contains filtered or unexported fields
}
TransportPool manages per-upstream HTTP transports with configurable connection pooling.
func NewTransportPool ¶
func NewTransportPool(config TransportPoolConfig, tlsCfg *tls.Config) *TransportPool
NewTransportPool creates a new transport pool with defaults.
func (*TransportPool) CloseAll ¶
func (tp *TransportPool) CloseAll()
CloseAll closes all transports and their idle connections.
func (*TransportPool) Default ¶
func (tp *TransportPool) Default() *http.Transport
Default returns the default shared transport.
func (*TransportPool) Get ¶
func (tp *TransportPool) Get(targetID string) *http.Transport
Get returns an HTTP transport for the given target, creating one if necessary. Each target gets its own transport with independent connection pool settings.
func (*TransportPool) Remove ¶
func (tp *TransportPool) Remove(targetID string)
Remove closes idle connections and removes the transport for a target.
type TransportPoolConfig ¶
type TransportPoolConfig struct {
// MaxIdleConns is the maximum number of idle connections across all hosts.
MaxIdleConns int `json:"maxIdleConns" yaml:"max_idle_conns"`
// MaxIdleConnsPerHost is the maximum idle connections per upstream host.
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost" yaml:"max_idle_conns_per_host"`
// MaxConnsPerHost limits total connections per host (0 = unlimited).
MaxConnsPerHost int `json:"maxConnsPerHost" yaml:"max_conns_per_host"`
// IdleConnTimeout is how long idle connections stay in the pool.
IdleConnTimeout time.Duration `json:"idleConnTimeout" yaml:"idle_conn_timeout"`
// ConnectTimeout is the timeout for establishing new connections.
ConnectTimeout time.Duration `json:"connectTimeout" yaml:"connect_timeout"`
// ResponseHeaderTimeout is the timeout for reading response headers.
ResponseHeaderTimeout time.Duration `json:"responseHeaderTimeout" yaml:"response_header_timeout"`
// ForceHTTP2 enables HTTP/2 for upstream connections.
ForceHTTP2 bool `json:"forceHttp2" yaml:"force_http2"`
}
TransportPoolConfig configures per-upstream HTTP transport pools.