gateway

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package gateway provides a transport-agnostic server and client wrapper for model completion. It exposes composable middleware for unary and streaming requests and can be paired with any RPC layer by supplying provider and caller functions that operate on the runtime model types.

Index

Constants

This section is empty.

Variables

View Source
var ErrProviderRequired = errors.New("model gateway: provider is required")

ErrProviderRequired indicates that a provider model.Client must be supplied.

Functions

This section is empty.

Types

type Option

type Option func(*serverConfig)

Option configures a Server during construction. Options are applied in the order they are passed to NewServer. Use WithProvider to set the underlying model client, and WithUnary or WithStream to register middleware chains.

func WithProvider

func WithProvider(p model.Client) Option

WithProvider returns an Option that sets the underlying model client used by the Server to fulfill completion requests. This option is required; NewServer will return ErrProviderRequired if no provider is configured. The provider's Complete and Stream methods form the innermost layer of the middleware chain.

func WithStream

func WithStream(mw ...StreamMiddleware) Option

WithStream returns an Option that appends one or more StreamMiddleware to the Server's streaming completion chain. Middleware are applied in the order they are registered across all WithStream calls, with the first middleware forming the outermost layer. Each middleware can intercept chunks, add telemetry, implement backpressure, or handle errors.

func WithUnary

func WithUnary(mw ...UnaryMiddleware) Option

WithUnary returns an Option that appends one or more UnaryMiddleware to the Server's unary completion chain. Middleware are applied in the order they are registered across all WithUnary calls, with the first middleware forming the outermost layer. Each middleware wraps the next, allowing pre-processing, post-processing, and conditional delegation.

type RemoteClient

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

RemoteClient implements model.Client using caller-supplied RPC functions that operate on normalized runtime model types. This keeps the adapter agnostic of the concrete transport (HTTP/GRPC) and generated packages.

func NewRemoteClient

func NewRemoteClient(
	complete func(ctx context.Context, req *model.Request) (*model.Response, error),
	stream func(ctx context.Context, req *model.Request) (model.Streamer, error),
) *RemoteClient

NewRemoteClient constructs a model.Client from normalized RPC functions.

func (*RemoteClient) Complete

func (c *RemoteClient) Complete(ctx context.Context, req *model.Request) (*model.Response, error)

func (*RemoteClient) Stream

func (c *RemoteClient) Stream(ctx context.Context, req *model.Request) (model.Streamer, error)

type Server

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

Server adapts a model.Client into a composable request handler with middleware support for both unary and streaming completions.

Applications typically instantiate a Server with NewServer, configure it with a provider client (WithProvider), and optionally add middleware chains (WithUnary, WithStream) for cross-cutting concerns such as logging, metrics, rate limiting, or request transformation. The resulting Server exposes Complete and Stream methods that Goa service implementations can call.

Middleware is applied in registration order: the first middleware registered wraps all subsequent ones, forming an onion structure where the innermost layer invokes the provider client.

func NewServer

func NewServer(opts ...Option) (*Server, error)

NewServer constructs a Server with the provided options. The resulting Server has no built-in policy; all behavior is composed via middleware registered through WithUnary and WithStream. A provider client must be configured via WithProvider or NewServer returns ErrProviderRequired.

Middleware chains are built during construction and applied in registration order: the first registered middleware becomes the outermost layer, wrapping all subsequent middleware and eventually the base provider handler. This allows early middleware to observe and transform both requests and responses while later middleware operate closer to the provider.

func (*Server) Complete

func (s *Server) Complete(ctx context.Context, req *model.Request) (*model.Response, error)

Complete processes a unary model completion request through the configured middleware chain and returns the complete response. The request flows through all registered UnaryMiddleware in order before reaching the provider client. The context is propagated through the chain and can be used for cancellation, timeouts, and request-scoped values.

func (*Server) Stream

func (s *Server) Stream(ctx context.Context, req *model.Request, send func(model.Chunk) error) error

Stream processes a streaming model completion request through the configured middleware chain, invoking send for each chunk produced. The send callback must be called sequentially; returning an error from send or from any middleware aborts the stream. The context is propagated through the chain and controls the lifetime of the stream.

type StreamHandler

type StreamHandler func(ctx context.Context, req *model.Request, send func(model.Chunk) error) error

StreamHandler processes a streaming model completion request by invoking the provided send callback for each chunk produced by the model. The send function must be called sequentially for each chunk; returning an error from send will abort the stream. Implementations are responsible for managing the underlying stream lifecycle, including cleanup on errors.

type StreamMiddleware

type StreamMiddleware func(next StreamHandler) StreamHandler

StreamMiddleware wraps a StreamHandler to add behavior around streaming completions. Middleware receives the next handler and returns a new handler that can intercept or transform chunks via the send callback, add logging or telemetry, implement backpressure, or handle errors. The middleware must preserve the sequential semantics of the send function.

type UnaryHandler

type UnaryHandler func(ctx context.Context, req *model.Request) (*model.Response, error)

UnaryHandler processes a single unary model completion request and returns the complete response. Implementations receive the request context and a *model.Request, and must return a *model.Response or an error. This signature is used both by the base provider handler and by middleware that compose additional behavior around it.

type UnaryMiddleware

type UnaryMiddleware func(next UnaryHandler) UnaryHandler

UnaryMiddleware wraps a UnaryHandler to add behavior before, after, or around the handler invocation. Middleware receives the next handler in the chain and returns a new handler that typically calls next after performing setup, or delegates to next conditionally. Common uses include logging, metrics, retries, request validation, and response transformation.

Jump to

Keyboard shortcuts

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