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 RegisterCodec(codec Codec)
- type Client
- type ClientOption
- type Codec
- 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" )
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 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.
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.
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
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 the following signature:
Method(context.Context, *Request) (*Response, error).
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.