Junior — Middle
Explain what metadata means in the gRPC protocol and what role it plays.
sobes.tech AI
Answer from AI
In gRPC, metadata is additional information sent along with the request or response, but not part of the main message body.
Role of metadata:
- Transmitting service information, such as authentication tokens, session identifiers, language settings.
- Managing call behavior (e.g., timeouts, priorities).
- Facilitating additional logic levels without changing the core message structure.
In Go, metadata is represented as key-value pairs and can be added to the call context. For example:
import (
"context"
"google.golang.org/grpc/metadata"
)
md := metadata.Pairs("authorization", "Bearer token123")
ctx := metadata.NewOutgoingContext(context.Background(), md)
// ctx is passed to gRPC call