handler

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterEvent

func RegisterEvent[TP ctx.TracePtr[TraceData], REQ define.ProtoMessagePtr[T1], TraceData any, T1 any](
	svc IService[TraceData, TP], desc string, f func(*ctx.BaseCtx[TraceData, TP], REQ) *berror.ErrMsg, middlewares ...Middleware[TraceData, TP],
)

RegisterEvent registers a fire-and-forget handler. One subscriber receives each message.

func RegisterEventBroadcast

func RegisterEventBroadcast[TP ctx.TracePtr[TraceData], REQ define.ProtoMessagePtr[T1], TraceData any, T1 any](
	svc IService[TraceData, TP], desc string, f func(*ctx.BaseCtx[TraceData, TP], REQ) *berror.ErrMsg, middlewares ...Middleware[TraceData, TP],
)

RegisterEventBroadcast registers a handler where all subscribers receive the message.

func RegisterEventForce

func RegisterEventForce[TP ctx.TracePtr[TraceData], REQ define.ProtoMessagePtr[T1], TraceData any, T1 any](
	svc IService[TraceData, TP], desc string, f func(*ctx.BaseCtx[TraceData, TP], REQ) *berror.ErrMsg, middlewares ...Middleware[TraceData, TP],
)

RegisterEventForce registers a fire-and-forget handler that bypasses backpressure.

func RegisterEventForceBroadcast

func RegisterEventForceBroadcast[TP ctx.TracePtr[TraceData], REQ define.ProtoMessagePtr[T1], TraceData any, T1 any](
	svc IService[TraceData, TP], desc string, f func(*ctx.BaseCtx[TraceData, TP], REQ) *berror.ErrMsg, middlewares ...Middleware[TraceData, TP],
)

RegisterEventForceBroadcast registers a critical broadcast handler.

func RegisterRPCResp

func RegisterRPCResp[TP ctx.TracePtr[TraceData], REQ define.ProtoMessagePtr[T1], RESP define.ProtoMessagePtr[T2], TraceData any, T1 any, T2 any](
	svc IService[TraceData, TP], desc string, f func(*ctx.BaseCtx[TraceData, TP], REQ, RESP) *berror.ErrMsg, middlewares ...Middleware[TraceData, TP],
)

RegisterRPCResp registers a RPC handler where RESP is an in-parameter (pooled by the framework).

func RegisterRPCRespForce

func RegisterRPCRespForce[TP ctx.TracePtr[TraceData], REQ define.ProtoMessagePtr[T1], RESP define.ProtoMessagePtr[T2], TraceData any, T1 any, T2 any](
	svc IService[TraceData, TP], desc string, f func(*ctx.BaseCtx[TraceData, TP], REQ, RESP) *berror.ErrMsg, middlewares ...Middleware[TraceData, TP],
)

RegisterRPCRespForce registers a pooled-RESP RPC handler that bypasses backpressure.

Types

type Elem

type Elem[TraceData any, TP ctx.TracePtr[TraceData]] struct {
	// contains filtered or unexported fields
}

Elem holds all metadata and runtime state for a single registered message handler. It stores the composed handler function, the middleware chain, pool references for request/response proto messages, and classification flags (RPC, broadcast, force). Callers never construct Elem directly; it is produced by the Register* functions. Written by Claude Code claude-opus-4-6.

func (*Elem[TraceData, TP]) Acquire added in v0.1.39

func (this_ *Elem[TraceData, TP]) Acquire() (proto.Message, proto.Message)

Acquire retrieves a pre-allocated (req, resp) pair from the pool. For event handlers resp is nil. Callers must call Release when done.

func (*Elem[TraceData, TP]) Call

func (this_ *Elem[TraceData, TP]) Call(c *ctx.BaseCtx[TraceData, TP]) *berror.ErrMsg

Call executes the full middleware chain followed by the core handler against the given context. Middleware execution is outermost-first: middlewares[0] wraps all the others and the handler. The returned error, if non-nil, is a structured ErrMsg that the service layer forwards to the caller. Written by Claude Code claude-opus-4-6.

func (*Elem[TraceData, TP]) IsForce

func (this_ *Elem[TraceData, TP]) IsForce() bool

IsForce reports whether the handler bypasses backpressure checks.

func (*Elem[TraceData, TP]) IsRPC

func (this_ *Elem[TraceData, TP]) IsRPC() bool

IsRPC reports whether the handler expects a reply to be sent back to the caller.

func (*Elem[TraceData, TP]) IsRPCResp added in v0.0.3

func (this_ *Elem[TraceData, TP]) IsRPCResp() bool

IsRPCResp reports whether the response proto is pooled by the framework.

func (*Elem[TraceData, TP]) MsgName

func (this_ *Elem[TraceData, TP]) MsgName() string

MsgName returns the fully qualified proto message name used as the routing key for this handler. It matches the NATS subject prefix derived from the request message type. Written by Claude Code claude-opus-4-6.

func (*Elem[TraceData, TP]) Release added in v0.1.39

func (this_ *Elem[TraceData, TP]) Release(req, resp proto.Message)

Release resets req and resp and returns them to the pool. resp may be nil (event handlers); req must not be nil.

func (*Elem[TraceData, TP]) String

func (this_ *Elem[TraceData, TP]) String() string

String returns a human-readable summary of the handler for logging and debugging, combining the description label with the fully qualified function name. Written by Claude Code claude-opus-4-6.

type HandleFunc

type HandleFunc[TraceData any, TP ctx.TracePtr[TraceData]] func(*ctx.BaseCtx[TraceData, TP]) *berror.ErrMsg

HandleFunc is the signature for all message handlers. It receives a typed context carrying the trace data, the request proto, and routing metadata, then returns a structured error or nil. Written by Claude Code claude-opus-4-6.

func Logger added in v0.1.39

func Logger[TraceData any, TP ctx.TracePtr[TraceData]](next HandleFunc[TraceData, TP]) HandleFunc[TraceData, TP]

Logger logs the inbound request, calls the next handler, then logs the outcome with duration. On success it attaches all response payloads to the log line for auditability.

func Recover added in v0.0.8

func Recover[TraceData any, TP ctx.TracePtr[TraceData]](next HandleFunc[TraceData, TP]) HandleFunc[TraceData, TP]

Recover is a lightweight panic-recovery middleware. Unlike LoggerAndRecover it does not emit success logs or measure duration; it solely catches panics, converts them to an ErrMsg, and logs the stack at error level. Use it in hot paths where the full logging overhead is unwanted but defensive recovery is still required. Written by Claude Code claude-opus-4-6.

type Handler

type Handler[TraceData any, TP ctx.TracePtr[TraceData]] struct {
	// contains filtered or unexported fields
}

Handler is the central registry that maps proto message full names to their Elem descriptors. It tracks both queue-subscription subjects and broadcast subjects to enforce the constraint that a given subject prefix may not be registered as both at the same time. Use NewHandler to construct one; use Group to create a scoped sub-registry. Written by Claude Code claude-opus-4-6.

func NewHandler

func NewHandler[TraceData any, TP ctx.TracePtr[TraceData]](middlewares ...Middleware[TraceData, TP]) *Handler[TraceData, TP]

NewHandler creates a handler registry with optional base middlewares applied to every registration.

func (*Handler[TraceData, TP]) GetBroadcastSubjInfo

func (h *Handler[TraceData, TP]) GetBroadcastSubjInfo() map[string]struct{}

GetBroadcastSubjInfo returns all broadcast subscription subject prefixes.

func (*Handler[TraceData, TP]) GetHandler

func (h *Handler[TraceData, TP]) GetHandler() *Handler[TraceData, TP]

GetHandler implements IService, allowing *Handler to be passed wherever IService is expected.

func (*Handler[TraceData, TP]) GetQueueSubjInfo

func (h *Handler[TraceData, TP]) GetQueueSubjInfo() map[string]struct{}

GetQueueSubjInfo returns all queue subscription subject prefixes.

func (*Handler[TraceData, TP]) Group

func (h *Handler[TraceData, TP]) Group(middlewares ...Middleware[TraceData, TP]) *Handler[TraceData, TP]

Group creates a new Handler sharing the same routing table with additional middlewares.

func (*Handler[TraceData, TP]) Logger

func (h *Handler[TraceData, TP]) Logger()

Logger writes an Info-level log line for every registered handler, useful at service startup to confirm that all expected message types have been successfully wired up. Written by Claude Code claude-opus-4-6.

func (*Handler[TraceData, TP]) Lookup added in v0.1.39

func (h *Handler[TraceData, TP]) Lookup(msgName string) (*Elem[TraceData, TP], bool)

Lookup returns the Elem registered for msgName, or (nil, false) if not found.

type IService added in v0.1.39

type IService[TraceData any, TP ctx.TracePtr[TraceData]] interface {
	GetHandler() *Handler[TraceData, TP]
}

IService is implemented by any type that owns a handler registry. All concrete service types in the service package implement this interface. *Handler also implements it so that handlers can be registered directly (e.g. in tests).

type Middleware added in v0.0.8

type Middleware[TraceData any, TP ctx.TracePtr[TraceData]] func(HandleFunc[TraceData, TP]) HandleFunc[TraceData, TP]

Middleware is a function that wraps a HandleFunc, enabling pre- and post-processing around the core handler — such as logging, tracing, authentication, or rate limiting. Middlewares are applied in registration order: the first middleware is the outermost wrapper. Written by Claude Code claude-opus-4-6.

Jump to

Keyboard shortcuts

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