Skip to main content
The transport layer provides a unified interface for different communication protocols. It defines common interfaces that both HTTP and gRPC implementations follow.

Core Interfaces

Server Interface

All transport servers implement the Server interface:
transport/transport.go
type Server interface {
    Start(context.Context) error
    Stop(context.Context) error
}
Start
func(context.Context) error
Starts the server and begins accepting requests
Stop
func(context.Context) error
Gracefully stops the server, respecting context timeout

Endpointer Interface

Servers can expose their endpoint information:
transport/transport.go
type Endpointer interface {
    Endpoint() (*url.URL, error)
}
Returns the server’s listening address in URL format (e.g., http://127.0.0.1:8000 or grpc://127.0.0.1:9000).

Header Interface

Abstraction for headers across different transports:
transport/transport.go
type Header interface {
    Get(key string) string
    Set(key string, value string)
    Add(key string, value string)
    Keys() []string
    Values(key string) []string
}
Provides a unified API for working with HTTP headers and gRPC metadata.

Transporter Interface

The Transporter interface provides access to transport-specific information:
transport/transport.go
type Transporter interface {
    // Kind returns transport type: "http" or "grpc"
    Kind() Kind
    
    // Endpoint returns the server or client endpoint
    // Server: grpc://127.0.0.1:9000
    // Client: discovery:///provider-demo
    Endpoint() string
    
    // Operation returns the full method path
    // Example: /helloworld.Greeter/SayHello
    Operation() string
    
    // RequestHeader returns transport request header
    RequestHeader() Header
    
    // ReplyHeader returns transport reply header (server only)
    ReplyHeader() Header
}

Transport Kinds

transport/transport.go
type Kind string

const (
    KindGRPC Kind = "grpc"
    KindHTTP Kind = "http"
)

Context Integration

Server Context

Store transport information in server handlers:
import "github.com/go-kratos/kratos/v2/transport"

// Store transporter in context
ctx = transport.NewServerContext(ctx, transporter)

// Retrieve transporter from context
if tr, ok := transport.FromServerContext(ctx); ok {
    kind := tr.Kind()           // "http" or "grpc"
    endpoint := tr.Endpoint()   // server address
    operation := tr.Operation() // method path
}

Client Context

Store transport information in client requests:
import "github.com/go-kratos/kratos/v2/transport"

// Store transporter in context
ctx = transport.NewClientContext(ctx, transporter)

// Retrieve transporter from context
if tr, ok := transport.FromClientContext(ctx); ok {
    endpoint := tr.Endpoint()   // target endpoint
    operation := tr.Operation() // method being called
}

Usage Example

Access transport information in middleware or handlers:
func MyMiddleware() middleware.Middleware {
    return func(handler middleware.Handler) middleware.Handler {
        return func(ctx context.Context, req interface{}) (interface{}, error) {
            if tr, ok := transport.FromServerContext(ctx); ok {
                // Log transport info
                log.Infof("Transport: %s, Operation: %s", 
                    tr.Kind(), tr.Operation())
                
                // Access headers
                userAgent := tr.RequestHeader().Get("User-Agent")
                
                // Set response headers (server side)
                tr.ReplyHeader().Set("X-Custom-Header", "value")
            }
            
            return handler(ctx, req)
        }
    }
}

Best Practices

Always access transport information through context rather than storing it globally.
Always check the boolean return value when extracting transport from context.
Write business logic that works regardless of transport type when possible.

HTTP Transport

HTTP server and client implementation

gRPC Transport

gRPC server and client implementation

Middleware

Middleware system for request processing

Metadata

Request/response metadata handling

Build docs developers (and LLMs) love