middleware

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OrderRecover wraps outermost so it captures panics from every other
	// middleware and from the handler.
	OrderRecover = -100
	// OrderTracing runs early so the trace context it installs is visible
	// to logging, metrics, and the handler.
	OrderTracing = -50
	// OrderLogging runs after tracing so log lines can carry the trace ID.
	OrderLogging = -25
	// OrderMetrics measures the handler plus the inner middlewares.
	OrderMetrics = 0
	// OrderInbox runs innermost so the dedupe decision sits closest to the
	// handler's side effects.
	OrderInbox = 100
)

Built-in middleware ordering. Lower Order wraps further out (runs earlier on the way in, later on the way out); equal Order preserves registration order. Custom middleware can position itself relative to these reference points.

Variables

This section is empty.

Functions

func IncomingTraceIDFromContext added in v0.28.0

func IncomingTraceIDFromContext(ctx context.Context) string

IncomingTraceIDFromContext returns the trace ID supplied by the cross-process producer when strict mode is in effect; in the default (trusting) mode it returns "" because the producer-supplied value is already used as the active TraceIDFromContext.

func TraceIDFromContext added in v0.28.0

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext returns the active trace ID stamped onto ctx by the Tracing consume middleware. In the default (trusting) mode this is the value the producer sent; in strict mode it is a freshly generated, non-spoofable ID.

func WithIncomingTraceID added in v0.28.0

func WithIncomingTraceID(ctx context.Context, traceID string) context.Context

WithIncomingTraceID parks the producer-supplied (untrusted) trace ID on ctx. Strict-mode tracing uses this so audit pipelines can distinguish a forged incoming ID from the framework-generated active ID.

func WithTraceID added in v0.28.0

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID stores the active trace ID on ctx. The Tracing consume middleware sets this; handlers read it via TraceIDFromContext.

Types

type ConsumeHandler

type ConsumeHandler func(ctx context.Context, d transport.Delivery, env event.Envelope) error

ConsumeHandler is the inner function a ConsumeMiddleware wraps. The Delivery is exposed so middleware can inspect Attempt counts and Ack/Nack the message directly (e.g. inbox dedupe).

func ChainConsume

ChainConsume composes consume middlewares filtered by transport capabilities, by ascending Order so the lowest Order becomes the outermost wrapper; equal Order keeps registration order. Nil and non-applicable entries are skipped (see ChainPublish).

type ConsumeMiddleware

type ConsumeMiddleware interface {
	// Name identifies the middleware for diagnostics.
	Name() string
	// Order sets the middleware's chain position. Lower values wrap
	// further out; equal values keep registration order. See the Order*
	// constants.
	Order() int
	// Applies reports whether the middleware should attach to a
	// transport with the given capabilities. For example, Inbox
	// returns false on non-AtLeastOnce transports to avoid pointless
	// database writes.
	Applies(caps transport.Capabilities) bool
	// WrapConsume returns a new handler that wraps next.
	WrapConsume(next ConsumeHandler) ConsumeHandler
}

ConsumeMiddleware augments the consume pipeline. Implementations run before the user-registered handler and may transform context, log, dedupe, capture metrics, recover panics, etc.

type PublishHandler

type PublishHandler func(ctx context.Context, env *event.Envelope) error

PublishHandler is the inner function a PublishMiddleware wraps.

func ChainPublish

func ChainPublish(mws []PublishMiddleware, base PublishHandler) PublishHandler

ChainPublish composes middlewares by ascending Order so the lowest Order becomes the outermost wrapper; equal Order keeps registration order. Nil entries are skipped so opt-out constructors (returning nil under disabled feature flags) compose safely with fx groups.

type PublishMiddleware

type PublishMiddleware interface {
	// Name identifies the middleware for diagnostics.
	Name() string
	// Order sets the middleware's chain position. Lower values wrap
	// further out; equal values keep registration order. See the Order*
	// constants.
	Order() int
	// WrapPublish returns a new handler that wraps next with the
	// middleware's behavior.
	WrapPublish(next PublishHandler) PublishHandler
}

PublishMiddleware augments the publish pipeline. Implementations can mutate the Envelope (e.g. inject tracing headers) before invoking next, or short-circuit by returning without calling next.

Jump to

Keyboard shortcuts

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