runtime

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Overview

Package runtime contains Vale's collectionx-backed compiled data plane model, route matcher, middleware chain, health checker, access logging, and metrics recorder contracts.

Config file DTOs live in package config. Runtime values are already compiled and are suitable for embedded library users that want to construct snapshots directly with NewSnapshot, NewService, NewRoute, and related helpers.

Index

Constants

View Source
const (
	PredicateHost = iota
	PredicatePathPrefix
	PredicateMethod
	PredicateHeaders
)
View Source
const MiddlewareTypeBuiltin = "builtin"

Variables

This section is empty.

Functions

func WrapMiddlewares

func WrapMiddlewares(handler http.Handler, middlewares *collectionlist.List[MiddlewareRuntime]) http.Handler

func WrapMiddlewaresWithRegistry

func WrapMiddlewaresWithRegistry(handler http.Handler, middlewares *collectionlist.List[MiddlewareRuntime], registry *MiddlewareRegistry) http.Handler

Types

type ACMERuntime

type ACMERuntime struct {
	Enabled  bool
	Email    string
	CacheDir string
	Domains  *collectionlist.List[string]
}

type AccessEvent

type AccessEvent struct {
	Method     string `json:"method"`
	Path       string `json:"path"`
	Host       string `json:"host"`
	StatusCode int    `json:"status_code"`
	DurationMs int64  `json:"duration_ms"`
	Route      string `json:"route"`
	Service    string `json:"service"`
	Endpoint   string `json:"endpoint"`
	UserAgent  string `json:"user_agent"`
	RemoteAddr string `json:"remote_addr"`
}

type AccessLogger

type AccessLogger struct {
	// contains filtered or unexported fields
}

func NewAccessLogger

func NewAccessLogger(logger *slog.Logger, enabled bool) *AccessLogger

func (*AccessLogger) Enabled added in v0.1.1

func (l *AccessLogger) Enabled() bool

func (*AccessLogger) Log

func (l *AccessLogger) Log(event AccessEvent)

type BasicAuthRuntime added in v0.1.1

type BasicAuthRuntime struct {
	Enabled bool
	Realm   string
	Users   *mapping.Map[string, string]
}

type CORSMiddlewareRuntime

type CORSMiddlewareRuntime struct {
	Enabled              bool
	AllowedOrigins       *collectionlist.List[string]
	AllowedMethods       *collectionlist.List[string]
	AllowedHeaders       *collectionlist.List[string]
	ExposedHeaders       *collectionlist.List[string]
	MaxAge               int
	AllowCredentials     bool
	AllowPrivateNetwork  bool
	OptionsPassthrough   bool
	OptionsSuccessStatus int
}

type Catalog

type Catalog struct {
	// contains filtered or unexported fields
}

Catalog is a control-plane index over a compiled snapshot. It is not used by the request hot path; route matching stays on EntrypointMatcher.

func BuildCatalog

func BuildCatalog(snapshot *CompiledSnapshot) (*Catalog, error)

func (*Catalog) RouteViews

func (c *Catalog) RouteViews(filter RouteFilter) (*collectionlist.List[RouteView], error)

func (*Catalog) Routes

func (c *Catalog) Routes(filter RouteFilter) (*collectionlist.List[RouteRecord], error)

type CircuitBreakerRuntime

type CircuitBreakerRuntime struct {
	Enabled          bool
	MaxRequests      uint32
	Interval         string
	Timeout          string
	FailureThreshold uint32
}

type CompiledRoute

type CompiledRoute struct {
	Name        string
	Entrypoint  string
	Host        string
	PathPrefix  string
	Method      string
	Headers     *mapping.Map[string, string]
	Service     *ServiceRuntime
	Predicates  *bitset.BitSet
	Middlewares *collectionlist.List[MiddlewareRuntime]
}

func MatchRoute

func MatchRoute(matcher *EntrypointMatcher, routes *collectionlist.List[*CompiledRoute], request *http.Request) *CompiledRoute

func NewRoute

func NewRoute(name, entrypoint string, service *ServiceRuntime) *CompiledRoute

func (*CompiledRoute) WithHeader

func (r *CompiledRoute) WithHeader(key, value string) *CompiledRoute

func (*CompiledRoute) WithHost

func (r *CompiledRoute) WithHost(host string) *CompiledRoute

func (*CompiledRoute) WithMethod

func (r *CompiledRoute) WithMethod(method string) *CompiledRoute

func (*CompiledRoute) WithMiddleware

func (r *CompiledRoute) WithMiddleware(middleware MiddlewareRuntime) *CompiledRoute

func (*CompiledRoute) WithPathPrefix

func (r *CompiledRoute) WithPathPrefix(pathPrefix string) *CompiledRoute

type CompiledSnapshot

type CompiledSnapshot struct {
	Entrypoints        *mapping.Map[string, string]
	EntrypointConfigs  *mapping.Map[string, EntrypointRuntime]
	RoutesByEntrypoint *mapping.MultiMap[string, *CompiledRoute]
	EntrypointMatchers *mapping.Map[string, *EntrypointMatcher]
	Catalog            *Catalog
	Services           *mapping.Map[string, *ServiceRuntime]
	AdminAddress       string
	AccessLogEnabled   bool
	MetricsEnabled     bool
	HealthInterval     string
	HealthTimeout      string
	Security           SecurityRuntime
	ProxyEngine        string
	Fingerprint        string
	BuiltAt            time.Time
}

func NewSnapshot

func NewSnapshot() *CompiledSnapshot

func (*CompiledSnapshot) AddEntrypoint

func (s *CompiledSnapshot) AddEntrypoint(name, address string, entrypoint EntrypointRuntime) *CompiledSnapshot

func (*CompiledSnapshot) AddRoute

func (s *CompiledSnapshot) AddRoute(route *CompiledRoute) *CompiledSnapshot

func (*CompiledSnapshot) AddService

func (s *CompiledSnapshot) AddService(service *ServiceRuntime) *CompiledSnapshot

func (*CompiledSnapshot) BuildCatalog

func (s *CompiledSnapshot) BuildCatalog() *CompiledSnapshot

func (*CompiledSnapshot) BuildMatchers

func (s *CompiledSnapshot) BuildMatchers() *CompiledSnapshot

func (*CompiledSnapshot) QueryRoutes

func (s *CompiledSnapshot) QueryRoutes(filter RouteFilter) *collectionlist.List[RouteView]

func (*CompiledSnapshot) Routes

func (*CompiledSnapshot) ServicesView

func (s *CompiledSnapshot) ServicesView() *collectionlist.List[ServiceView]

type CompressRuntime added in v0.1.1

type CompressRuntime struct {
	Enabled  bool
	MinBytes int
}

type EndpointRecord

type EndpointRecord struct {
	ID      string
	Service string
	URL     string
	Weight  int
}

type EndpointRuntime

type EndpointRuntime struct {
	URL         *url.URL
	Weight      int
	Proxy       http.Handler
	Healthy     atomic.Bool
	LastChecked atomic.Int64
}

func NewEndpoint

func NewEndpoint(rawURL string, weight int, proxy http.Handler) (*EndpointRuntime, error)

type EndpointView

type EndpointView struct {
	URL         string `json:"url"`
	Weight      int    `json:"weight"`
	Healthy     bool   `json:"healthy"`
	LastChecked int64  `json:"last_checked"`
}

type EntrypointMatcher

type EntrypointMatcher struct {
	// contains filtered or unexported fields
}

func BuildEntrypointMatcher

func BuildEntrypointMatcher(routes *collectionlist.List[*CompiledRoute]) *EntrypointMatcher

type EntrypointRuntime

type EntrypointRuntime struct {
	Name    string
	Address string
	TLS     TLSRuntime
}

type ForwardAuthRuntime added in v0.1.2

type ForwardAuthRuntime struct {
	Enabled              bool
	Address              string
	Timeout              time.Duration
	TrustForwardHeader   bool
	ForwardBody          bool
	MaxBodyBytes         int64
	AuthRequestHeaders   *collectionlist.List[string]
	AuthResponseHeaders  *collectionlist.List[string]
	MaxResponseBodyBytes int64
}

type Gateway

type Gateway struct {
	// contains filtered or unexported fields
}

func NewGateway

func NewGateway(snapshot *CompiledSnapshot, logger *slog.Logger, accessEnabled bool, metrics MetricsRecorder) *Gateway

func NewGatewayWithMiddlewareRegistry

func NewGatewayWithMiddlewareRegistry(snapshot *CompiledSnapshot, logger *slog.Logger, accessEnabled bool, metrics MetricsRecorder, registry *MiddlewareRegistry) *Gateway

func (*Gateway) Handler

func (g *Gateway) Handler(entrypoint string) http.Handler

func (*Gateway) MetricsHandler

func (g *Gateway) MetricsHandler() http.Handler

func (*Gateway) ObserveHealth

func (g *Gateway) ObserveHealth(endpoint *EndpointRuntime, healthy bool)

func (*Gateway) ObserveHealthCheck added in v0.1.1

func (g *Gateway) ObserveHealthCheck(endpoint *EndpointRuntime, healthy bool, duration time.Duration)

func (*Gateway) ObserveRaftApply added in v0.1.4

func (g *Gateway) ObserveRaftApply(group string, duration time.Duration, result string)

func (*Gateway) ObserveReload

func (g *Gateway) ObserveReload(result string)

func (*Gateway) ObserveReloadDebounce added in v0.1.4

func (g *Gateway) ObserveReloadDebounce(delay time.Duration, sourceCount int)

func (*Gateway) ObserveReloadDuration added in v0.1.4

func (g *Gateway) ObserveReloadDuration(result string, duration time.Duration)

func (*Gateway) ObserveRouteCache added in v0.1.1

func (g *Gateway) ObserveRouteCache(hit bool)

func (*Gateway) ObserveSnapshot

func (g *Gateway) ObserveSnapshot(snapshot *CompiledSnapshot)

func (*Gateway) Snapshot

func (g *Gateway) Snapshot() *CompiledSnapshot

func (*Gateway) Swap

func (g *Gateway) Swap(snapshot *CompiledSnapshot)

type HealthCheckMetricsRecorder added in v0.1.1

type HealthCheckMetricsRecorder interface {
	ObserveHealthCheck(endpoint *EndpointRuntime, healthy bool, duration time.Duration)
}

type HealthChecker

type HealthChecker struct {
	// contains filtered or unexported fields
}

func NewHealthChecker

func NewHealthChecker(interval, timeout time.Duration) *HealthChecker

func NewHealthCheckerWithLogger

func NewHealthCheckerWithLogger(interval, timeout time.Duration, logger *slog.Logger) *HealthChecker

func (*HealthChecker) Start

func (h *HealthChecker) Start(ctx context.Context, gateway *Gateway)

func (*HealthChecker) Stop

func (h *HealthChecker) Stop()

type HealthMetricsRecorder

type HealthMetricsRecorder interface {
	ObserveHealth(endpoint *EndpointRuntime, healthy bool)
}

type IPAllowListRuntime added in v0.1.1

type IPAllowListRuntime struct {
	Enabled            bool
	SourceRange        *collectionlist.List[string]
	TrustForwardHeader bool
}

type MetricsRecorder

type MetricsRecorder interface {
	Observe(route *CompiledRoute, endpoint *EndpointRuntime, status int, duration time.Duration)
	Handler() http.Handler
}

func NewNoopMetrics

func NewNoopMetrics() MetricsRecorder

func NewObservabilityMetrics

func NewObservabilityMetrics(enabled bool, obs observabilityx.Observability, handler http.Handler) MetricsRecorder

type MiddlewareFactory

type MiddlewareFactory func(http.Handler, MiddlewareRuntime) http.Handler

type MiddlewareRecord

type MiddlewareRecord struct {
	Name string
	Type string
}

type MiddlewareRegistry

type MiddlewareRegistry struct {
	// contains filtered or unexported fields
}

func DefaultMiddlewareRegistry

func DefaultMiddlewareRegistry() *MiddlewareRegistry

func NewMiddlewareRegistry

func NewMiddlewareRegistry() *MiddlewareRegistry

func (*MiddlewareRegistry) Clone

func (*MiddlewareRegistry) Factory

func (r *MiddlewareRegistry) Factory(middlewareType string) (MiddlewareFactory, bool)

func (*MiddlewareRegistry) Names

func (*MiddlewareRegistry) Register

func (r *MiddlewareRegistry) Register(middlewareType string, factory MiddlewareFactory) error

type MiddlewareRuntime

type MiddlewareRuntime struct {
	Name                   string
	Type                   string
	StripPrefix            string
	StripPrefixes          *collectionlist.List[string]
	AddPrefix              string
	ReplacePath            string
	ReplacePathRegex       string
	ReplacePathReplacement string
	RedirectScheme         string
	RedirectPort           string
	RedirectRegex          string
	RedirectReplacement    string
	RedirectPermanent      bool
	RequestHeaders         *mapping.Map[string, string]
	ResponseHeaders        *mapping.Map[string, string]
	MaxBodyBytes           int64
	Chain                  *collectionlist.List[string]
	Secure                 SecureMiddlewareRuntime
	CORS                   CORSMiddlewareRuntime
	RateLimit              RateLimitRuntime
	CircuitBreaker         CircuitBreakerRuntime
	BasicAuth              BasicAuthRuntime
	Compress               CompressRuntime
	IPAllowList            IPAllowListRuntime
	ForwardAuth            ForwardAuthRuntime
}

func NewMiddleware

func NewMiddleware(name string) MiddlewareRuntime

type RaftApplyMetricsRecorder added in v0.1.4

type RaftApplyMetricsRecorder interface {
	ObserveRaftApply(group string, duration time.Duration, result string)
}

type RateLimitRuntime

type RateLimitRuntime struct {
	Enabled bool
	Rate    float64
	Burst   int
}

type ReloadDebounceMetricsRecorder added in v0.1.4

type ReloadDebounceMetricsRecorder interface {
	ObserveReloadDebounce(delay time.Duration, sourceCount int)
}

type ReloadDurationMetricsRecorder added in v0.1.4

type ReloadDurationMetricsRecorder interface {
	ObserveReloadDuration(result string, duration time.Duration)
}

type ReloadMetricsRecorder

type ReloadMetricsRecorder interface {
	ObserveReload(result string)
}

type ResourceDiff added in v0.1.1

type ResourceDiff struct {
	Added   *collectionlist.List[string] `json:"added"`
	Removed *collectionlist.List[string] `json:"removed"`
	Changed *collectionlist.List[string] `json:"changed"`
}

func EmptyResourceDiff added in v0.1.1

func EmptyResourceDiff() ResourceDiff

func (ResourceDiff) HasChanges added in v0.1.1

func (d ResourceDiff) HasChanges() bool

type RouteCacheMetricsRecorder added in v0.1.1

type RouteCacheMetricsRecorder interface {
	ObserveRouteCache(hit bool)
}

type RouteFilter

type RouteFilter struct {
	Entrypoint string
	Service    string
	Host       string
	PathPrefix string
}

type RouteRecord

type RouteRecord struct {
	Name       string
	Entrypoint string
	Host       string
	PathPrefix string
	Method     string
	Service    string
}

type RouteView

type RouteView struct {
	Name       string `json:"name"`
	Entrypoint string `json:"entrypoint"`
	Host       string `json:"host,omitempty"`
	PathPrefix string `json:"path_prefix,omitempty"`
	Method     string `json:"method,omitempty"`
	Service    string `json:"service"`
}

type SecureMiddlewareRuntime

type SecureMiddlewareRuntime struct {
	Enabled                         bool
	AllowedHosts                    *collectionlist.List[string]
	AllowedHostsAreRegex            bool
	SSLRedirect                     bool
	SSLHost                         string
	SSLTemporaryRedirect            bool
	STSSeconds                      int64
	STSIncludeSubdomains            bool
	STSPreload                      bool
	FrameDeny                       bool
	ContentTypeNosniff              bool
	BrowserXSSFilter                bool
	ContentSecurityPolicy           string
	ContentSecurityPolicyReportOnly string
	ReferrerPolicy                  string
	PermissionsPolicy               string
}

type SecurityRuntime

type SecurityRuntime struct {
	ReadHeaderTimeout string
	ReadTimeout       string
	WriteTimeout      string
	IdleTimeout       string
	MaxHeaderBytes    int
	MaxBodyBytes      int64
}

type ServiceRecord

type ServiceRecord struct {
	Name     string
	Strategy string
}

type ServiceRuntime

type ServiceRuntime struct {
	Name      string
	Strategy  string
	Endpoints *collectionlist.List[*EndpointRuntime]
	// contains filtered or unexported fields
}

func NewService

func NewService(name, strategy string, endpoints ...*EndpointRuntime) *ServiceRuntime

func (*ServiceRuntime) BuildSlots

func (s *ServiceRuntime) BuildSlots()

func (*ServiceRuntime) Pick

func (s *ServiceRuntime) Pick() (*EndpointRuntime, error)

type ServiceView

type ServiceView struct {
	Name      string                             `json:"name"`
	Strategy  string                             `json:"strategy"`
	Endpoints *collectionlist.List[EndpointView] `json:"endpoints"`
}

type SnapshotDiff added in v0.1.1

type SnapshotDiff struct {
	Routes    ResourceDiff `json:"routes"`
	Services  ResourceDiff `json:"services"`
	Endpoints ResourceDiff `json:"endpoints"`
}

func DiffSnapshots added in v0.1.1

func DiffSnapshots(current, next *CompiledSnapshot) SnapshotDiff

func (SnapshotDiff) HasChanges added in v0.1.1

func (d SnapshotDiff) HasChanges() bool

type SnapshotMetricsRecorder

type SnapshotMetricsRecorder interface {
	ObserveSnapshot(snapshot *CompiledSnapshot)
}

type TLSRuntime

type TLSRuntime struct {
	Enabled  bool
	CertFile string
	KeyFile  string
	ACME     ACMERuntime
}

Jump to

Keyboard shortcuts

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