Documentation
¶
Overview ¶
Package transport provides a protocol-agnostic abstraction that unifies HTTP and gRPC request/response metadata. Middleware and business logic depend only on the Transporter interface, never on the concrete protocol, so the same code serves both transports.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewServerContext ¶
func NewServerContext(ctx context.Context, tr Transporter) context.Context
NewServerContext returns a new context with tr attached.
func Release ¶
func Release(t Transporter)
Release returns a Transporter obtained from NewTransporter to the pool, clearing its fields so pooled instances do not retain request-scoped references. It must only be called after all readers of the Transporter (for example, middlewares that extract it from the context) have finished.
Types ¶
type Header ¶
type Header interface {
// Get returns the first value for key, or "" when absent.
Get(key string) string
// Set replaces the value for key with a single value.
Set(key, value string)
// Add appends value to key.
Add(key, value string)
// Keys returns all present header keys.
Keys() []string
// Values returns all values for key.
Values(key string) []string
}
Header is a protocol-agnostic view over request/response headers. It is satisfied by both http.Header and grpc metadata.MD through the adapters below, so middleware can read and write headers without knowing the transport.
func NewHTTPHeader ¶
NewHTTPHeader wraps an http.Header as a Header.
func NewMetadataHeader ¶
NewMetadataHeader wraps a grpc metadata.MD as a Header.
type Transporter ¶
type Transporter interface {
// Kind returns the transport protocol of this request.
Kind() Kind
// Operation returns the fully-qualified operation name. For gRPC this is
// the full method name (e.g. "/edu.course.student-api.StudentService/Create");
// for HTTP it is "METHOD /path" (e.g. "POST /v1/students").
Operation() string
// Endpoint returns the address of the server handling this request.
Endpoint() string
// RequestHeader returns the incoming request headers.
RequestHeader() Header
// ReplyHeader returns the outgoing response headers.
ReplyHeader() Header
}
Transporter carries the protocol-agnostic metadata of an in-flight request. It flattens http.Header and grpc metadata.MD behind a single Header interface, following the kratos transport abstraction.
func FromServerContext ¶
func FromServerContext(ctx context.Context) (Transporter, bool)
FromServerContext extracts a Transporter previously attached via NewServerContext. The bool is false when no Transporter is present.
func NewTransporter ¶
func NewTransporter(kind Kind, operation, endpoint string, reqHeader, replyHeader Header) Transporter
NewTransporter builds a Transporter from its component parts, obtaining the backing struct from a pool. Callers that take the hot request path should defer transport.Release(t) once the chain has finished reading it.