Skip to main content
The Transport API defines the core interfaces for transport layer implementations in Kratos, supporting both HTTP and gRPC protocols.

Interfaces

Server

Transport server interface that all transport implementations must satisfy.
type Server interface {
    Start(context.Context) error
    Stop(context.Context) error
}
Start
func(context.Context) error
Starts the transport server. Blocks until the server stops or context is canceled.
Stop
func(context.Context) error
Gracefully stops the transport server within the context deadline.

Endpointer

Interface for services that can provide their endpoint URL.
type Endpointer interface {
    Endpoint() (*url.URL, error)
}
Endpoint
func() (*url.URL, error)
Returns the endpoint URL of the service (e.g., http://127.0.0.1:8000 or grpc://127.0.0.1:9000)
Storage interface for transport headers.
type Header interface {
    Get(key string) string
    Set(key string, value string)
    Add(key string, value string)
    Keys() []string
    Values(key string) []string
}
Get
func(key string) string
Returns the first value associated with the key
Set
func(key string, value string)
Sets the header entry, replacing any existing values
Add
func(key string, value string)
Adds a value to the header entry
Keys
func() []string
Returns all header keys
Values
func(key string) []string
Returns all values associated with the key

Transporter

Transport context value interface providing access to transport information.
type Transporter interface {
    Kind() Kind
    Endpoint() string
    Operation() string
    RequestHeader() Header
    ReplyHeader() Header
}
Kind
func() Kind
Returns the transport kind (“grpc” or “http”)
Endpoint
func() string
Returns the server or client endpoint.
  • Server: grpc://127.0.0.1:9000 or http://127.0.0.1:8000
  • Client: discovery:///provider-demo
Operation
func() string
Returns the service full method selector generated by protobuf. Example: /helloworld.Greeter/SayHello
RequestHeader
func() Header
Returns the transport request header (http.Header for HTTP, metadata.MD for gRPC)
ReplyHeader
func() Header
Returns the transport reply/response header. Only valid for server transport. (http.Header for HTTP, metadata.MD for gRPC)

Types

Kind

Defines the type of transport.
type Kind string

const (
    KindGRPC Kind = "grpc"
    KindHTTP Kind = "http"
)
KindGRPC
Kind
gRPC transport kind
KindHTTP
Kind
HTTP transport kind
Methods:
func (k Kind) String() string

Context Functions

NewServerContext

Returns a new Context that carries the server Transporter value.
func NewServerContext(ctx context.Context, tr Transporter) context.Context
ctx
context.Context
The parent context
tr
Transporter
The Transporter to attach
context
context.Context
Context with server Transporter attached

FromServerContext

Returns the server Transporter value stored in context.
func FromServerContext(ctx context.Context) (tr Transporter, ok bool)
ctx
context.Context
Context to retrieve Transporter from
tr
Transporter
The retrieved Transporter instance
ok
bool
Whether the Transporter was found
Example:
if tr, ok := transport.FromServerContext(ctx); ok {
    fmt.Println("Transport kind:", tr.Kind())
    fmt.Println("Operation:", tr.Operation())
}

NewClientContext

Returns a new Context that carries the client Transporter value.
func NewClientContext(ctx context.Context, tr Transporter) context.Context
ctx
context.Context
The parent context
tr
Transporter
The Transporter to attach
context
context.Context
Context with client Transporter attached

FromClientContext

Returns the client Transporter value stored in context.
func FromClientContext(ctx context.Context) (tr Transporter, ok bool)
ctx
context.Context
Context to retrieve Transporter from
tr
Transporter
The retrieved Transporter instance
ok
bool
Whether the Transporter was found

Usage Example

package main

import (
    "context"
    "fmt"
    "github.com/go-kratos/kratos/v2/transport"
)

func middleware(next Handler) Handler {
    return func(ctx context.Context, req interface{}) (interface{}, error) {
        if tr, ok := transport.FromServerContext(ctx); ok {
            // Access transport information
            fmt.Printf("Kind: %s\n", tr.Kind())
            fmt.Printf("Operation: %s\n", tr.Operation())
            fmt.Printf("Endpoint: %s\n", tr.Endpoint())
            
            // Access headers
            header := tr.RequestHeader()
            authToken := header.Get("authorization")
            fmt.Printf("Auth: %s\n", authToken)
        }
        return next(ctx, req)
    }
}

Build docs developers (and LLMs) love