Documentation
¶
Index ¶
- Constants
- Variables
- func Call[T any](c *Client, ctx context.Context, service, method string, req any) (*T, error)
- func CallFor[S any, T any](c *Client, ctx context.Context, method string, req any) (*T, error)
- func ClientStream[T any](c *Client, ctx context.Context, service, method string, req any, src io.Reader) (*T, error)
- func ClientStreamFor[S any, T any](c *Client, ctx context.Context, method string, req any, src io.Reader) (*T, error)
- func RegisterCodec(codec Codec)
- func ServerStream[T any](c *Client, ctx context.Context, service, method string, req any, dst io.Writer) (*T, error)
- func ServerStreamFor[S any, T any](c *Client, ctx context.Context, method string, req any, dst io.Writer) (*T, error)
- type Client
- func (c *Client) Beacon(ctx context.Context, service, method string, request any) error
- func (c *Client) Call(ctx context.Context, service, method string, request any, response any) error
- func (c *Client) ClientStream(ctx context.Context, service, method string, request any, src io.Reader, ...) error
- func (c *Client) Notify(ctx context.Context, service, method string, request any) error
- func (c *Client) ServerStream(ctx context.Context, service, method string, request any, dst io.Writer, ...) error
- type ClientOption
- type Codec
- type Decoder
- type Encoder
- type Error
- type Handler
- func Def[T any](impl any) *Handler
- func DefService(service string, impl any) *Handler
- func NewHandler(service string, impl any) (*Handler, error)
- func NewHandlerFor[T any](impl any) (*Handler, error)
- func NewHandlerOf(impl any) (*Handler, error)
- func NewStrictHandlerFor[T any](impl any) (*Handler, error)
- type Mode
- type Mux
Constants ¶
const ( // ErrorHeader is the HTTP header key used to report error messages from the server. ErrorHeader = "X-Vrpc-Err" // ProtoHeader is the HTTP header key used to specify the RPC call mode. ProtoHeader = "X-Vrpc" // StatusEncodingError is returned by server when it failed to encode the response. StatusEncodingError = 567 )
Variables ¶
var ( ErrNotFound = &Error{"not found"} ErrNoCodec = &Error{"codec not supported"} )
Functions ¶
func Call ¶
Call is a generic helper function that invokes a method on the Client and returns result as *T.
func CallFor ¶
CallFor is a generic helper function that invokes a method on the Client using S type as a service name and returns result as *T.
func ClientStream ¶ added in v0.2.2
func ClientStream[T any](c *Client, ctx context.Context, service, method string, req any, src io.Reader) (*T, error)
ClientStream is a generic helper function that invokes a client streaming method on the Client and returns result as *T.
func ClientStreamFor ¶ added in v0.2.2
func ClientStreamFor[S any, T any](c *Client, ctx context.Context, method string, req any, src io.Reader) (*T, error)
ClientStreamFor is a generic helper function that invokes a client streaming method on the Client using S type as a service name and returns result as *T.
func RegisterCodec ¶
func RegisterCodec(codec Codec)
RegisterCodec adds a codec to the global list of server codecs. It is not safe for concurrent use and must be called before any Handler starts serving requests.
func ServerStream ¶ added in v0.2.2
func ServerStream[T any](c *Client, ctx context.Context, service, method string, req any, dst io.Writer) (*T, error)
ServerStream is a generic helper function that invokes a server streaming method on the Client and returns result as *T.
func ServerStreamFor ¶ added in v0.2.2
func ServerStreamFor[S any, T any](c *Client, ctx context.Context, method string, req any, dst io.Writer) (*T, error)
ServerStreamFor is a generic helper function that invokes a server streaming method on the Client using S type as a service name and returns result as *T.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an RPC client.
func NewClient ¶
func NewClient(options ...ClientOption) (*Client, error)
NewClient constructs a Client using the provided options. It validates the resulting configuration and returns an error if it is not valid.
func (*Client) Beacon ¶
Beacon sends the request, but does not check the result or whether the request reached the server. Errors are returned only if encoding fails.
func (*Client) Call ¶
Call executes a synchronous RPC method. It sends the request, waits for the server to process it, and decodes the body into the response.
func (*Client) ClientStream ¶ added in v0.2.0
func (c *Client) ClientStream(ctx context.Context, service, method string, request any, src io.Reader, response any) error
ClientStream calls an RPC method that streams data from the client to the server.
Server implementation receives an io.Reader and may read the stream until EOF, then returns a regular unary response which is decoded into response.
End-of-stream is signaled by src returning io.EOF. If you want to stream data incrementally (e.g. transfer objects), use an io.Pipe (or a custom io.Reader) and close the writer side to signal EOF.
Context cancellation aborts the request. The upload goroutine will eventually stop when the request is torn down; src may observe read errors.
The upload path is buffered; explicit flushing is not exposed.
Transport and protocol errors are returned as *vrpc.Error. Service-level failures are returned as a regular error (not wrapped). On upload errors, the returned error reflects the upload failure even if the server already replied (best-effort error propagation).
func (*Client) Notify ¶
Notify sends the request, but does not wait for the server to process it. Errors are returned if encoding failed or if the server was unable to decode the request.
func (*Client) ServerStream ¶ added in v0.2.0
func (c *Client) ServerStream(ctx context.Context, service, method string, request any, dst io.Writer, response any) error
ServerStream calls an RPC method that streams data from the server to the client.
The server implementation receives an io.Writer and may write the streamed payload to it. On the client side, all streamed bytes are forwarded into dst.
Context cancellation aborts the request. dst may observe a write error originating from request cancellation.
dst receives bytes as they arrive, but actual network delivery depends on server flushing. The server-side writer supports optional explicit Flush to prioritize latency; otherwise buffering may increase throughput.
Transport and protocol errors are returned as *vrpc.Error. Service-level failures are returned as a regular error (not wrapped).
If an error occurs, dst may have already received a prefix of the stream.
type ClientOption ¶
type ClientOption func(*clientConfig)
ClientOption configures a Client in NewClient.
func WithClient ¶
func WithClient(hc *http.Client) ClientOption
WithClient sets the underlying HTTP client (transport/TLS/etc.). If not set, a default client is created with MaxIdleConnsPerHost set to 100.
func WithCodec ¶
func WithCodec(codec Codec) ClientOption
WithCodec sets the RPC codec. If not set, msgpack is used.
func WithEndpoint ¶
func WithEndpoint(endpoint string) ClientOption
WithEndpoint sets the fixed endpoint dial target (scheme://host[:port][/prefix]). Required for default mode and for ServiceToHeader; forbidden for ServiceToURL.
func WithMode ¶
func WithMode(mode Mode) ClientOption
WithMode sets routing mode. If not set, ModeDefault is used.
func WithPrefix ¶
func WithPrefix(prefix string) ClientOption
WithPrefix sets a path prefix (e.g. "/prefix"). If Endpoint has a path component and WithPrefix is set, WithPrefix overrides the endpoint path.
func WithScheme ¶
func WithScheme(scheme string) ClientOption
WithScheme sets scheme used only in ServiceToURL mode (default: http).
type Codec ¶
type Codec interface {
Encode(w io.Writer, v any) error
Decode(r io.Reader, v any) error
NewEncoder(w io.Writer) Encoder
NewDecoder(r io.Reader) Decoder
ContentType() string
}
Codec defines the interface for encoding and decoding RPC messages.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error type is returned for RPC errors (transport, encoding, decoding). Errors returned by the service implementation are not wrapped by this type.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves RPC requests for a specific service implementation.
func DefService ¶
DefService is an alias for NewHandler that panics if an error occurs.
func NewHandler ¶
NewHandler creates a new Handler for the given implementation, using a user-provided service name. It reflects over impl to find suitable methods. A suitable method must have one of the following signatures:
Method(context.Context, *Request) (*Response, error). // unary Method(context.Context, *Request, io.Writer) (*Response, error) // download/stream Method(context.Context, *Request, io.Reader) (*Response, error) // upload/stream
func NewHandlerFor ¶
NewHandlerFor creates a new Handler for the given implementation, using type T to determine the service name.
If T is an interface, impl must implement it. However, even when T is an interface, all suitable methods found on the concrete implementation type are exposed by the handler, not only the methods declared in the interface T.
func NewHandlerOf ¶
NewHandlerOf creates a new Handler for the given implementation, inferring the service name from the implementation type.
func NewStrictHandlerFor ¶
NewStrictHandlerFor creates a new Handler for the given implementation, using type T as a service name and a contract that restricts which methods are exposed.
Only methods that are present on T and satisfy the required signature are exposed.
The implementation must fully implement RPC method subset of T: for every suitable method on the contract type, impl must have a method with the same name and a compatible signature. Otherwise, an error is returned.
If T is an interface, impl must also implement T.
type Mode ¶
type Mode uint8
Mode controls how the client routes requests.
const ( // ModeDefault means: connect to Endpoint, send Host header equal to Endpoint host. ModeDefault Mode = iota // ServiceToHeader means: connect to Endpoint, but send Host header equal to service name. // Useful for sidecars/proxies that route by Host/authority. ServiceToHeader // ServiceToURL means: connect directly to http(s)://<service>/..., and also send Host header = service. // Endpoint must not be set in this mode. ServiceToURL )
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux is a multiplexer that routes requests to specific Handlers based on the service name.
func NewMux ¶
NewMux creates a new Mux with the provided list of Handlers. It returns an error if duplicate service names are detected.