transport

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 5 Imported by: 0

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 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

func NewHTTPHeader(h http.Header) Header

NewHTTPHeader wraps an http.Header as a Header.

func NewMetadataHeader

func NewMetadataHeader(md metadata.MD) Header

NewMetadataHeader wraps a grpc metadata.MD as a Header.

type Kind

type Kind string

Kind identifies the transport protocol of an incoming request.

const (
	// KindGRPC represents a gRPC request.
	KindGRPC Kind = "gRPC"
	// KindHTTP represents an HTTP request.
	KindHTTP Kind = "HTTP"
)

func (Kind) String

func (k Kind) String() string

String returns the string representation of the kind.

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.

Jump to

Keyboard shortcuts

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