Documentation ¶
Index ¶
- Constants
- Variables
- func Encode(v interface{}) (ret []byte, err error)
- func RegisterStructType(v ...StructValuer)
- func TryRegisterStructType(v ...StructValuer) error
- type Client
- type Error
- type ErrorKind
- type Header
- type IncompatibleTypeError
- type MapValue
- type Middleware
- type OneOfValue
- type Option
- type RPCRequest
- type Request
- type Response
- type Server
- type StructValuer
- type Structure
- type Type
- type UnknownField
Constants ¶
const ( // ErrorKindInternalError indicates that an internal error prevented the // server from performing the requested operation. ErrorKindInternalError = 0 // ErrorKindManagedError indicates that the server returned a user-defined // error. See Identifier and UserData fields, and consult the service's // documentation for further information. ErrorKindManagedError = 1 // ErrorKindRequestTimeout indicates that the server reached a timeout while // waiting for the client to transmit headers. ErrorKindRequestTimeout = 2 // ErrorKindUnimplementedMethod indicates that the server does not implement // the requested method. ErrorKindUnimplementedMethod = 3 // ErrorKindTypeMismatch indicates that the contract between the server and // client is out-of-sync, since they could not agree on a type for either // a request or response. ErrorKindTypeMismatch = 4 // operation due to lack of authorization. See Identifier and UserData // fields, along with the service's documentation for further information. ErrorKindUnauthorized = 5 // ErrorKindBadRequest indicates that the server refused to continue the // operation due to a problem with the incoming request. See Identifier and // UserData fields, along with the service's documentation for further // information. ErrorKindBadRequest = 6 )
Variables ¶
var ErrCorruptStream = fmt.Errorf("corrupt stream")
ErrCorruptStream indicates that the stream being processed is corrupt.
var ErrDuplicatedFieldIndex = fmt.Errorf("duplicated field index")
ErrDuplicatedFieldIndex indicates that a given structure contains one or more fields sharing the same index tag.
var ErrFieldGap = fmt.Errorf("structs must have no gaps between field indexes")
ErrFieldGap indicates that a given structure contains one or more gaps between fields indexes. The field list indexes must be contiguous.
var ErrIncompatibleStruct = fmt.Errorf("incompatible structure to encode; structures must implement StructValuer")
ErrIncompatibleStruct indicates that a struct that does not implement the StructValuer interface was provided.
var ErrIncompleteStruct = fmt.Errorf("incomplete structure definition; missing *Structure field")
ErrIncompleteStruct indicates that a struct lacking a *Structure field was provided.
var ErrInvalidTag = fmt.Errorf("invalid index tag")
ErrInvalidTag indicates that a structure tag contains an invalid value that could not be parsed as a number.
var ErrInvalidType = fmt.Errorf("invalid type in stream")
ErrInvalidType indicates that an invalid type was encountered in the stream, possibly indicating that it is corrupted.
var ErrMinFieldNotZero = fmt.Errorf("minimum field index should be zero")
ErrMinFieldNotZero indicates that a given structure does not have a field beginning at zero.
var ErrNonHomogeneousArray = fmt.Errorf("only homogeneous arrays are supported")
ErrNonHomogeneousArray indicates that an operation was attempted against a non-homogeneous array or slice. Only homogeneous arrays are supported by YARP.
var ErrServerClosed = fmt.Errorf("server closed")
ErrServerClosed indicates that the server was closed. This error is returned by Server's Start and StartListener methods when Shutdown is called.
var ErrSizeTooLarge = fmt.Errorf("size is too large")
ErrSizeTooLarge indicates that a message is either too large, or its stream is corrupted. The size limitation of 2GB was arbitrarily imposed to detect faulty messages.
var ErrUnknownStructType = fmt.Errorf("unknown struct type")
ErrUnknownStructType indicates that the message being parsed refers to an unknown struct type.
var ErrWantsStreamed = fmt.Errorf("method requires a streamed response")
ErrWantsStreamed indicates that the client and server implementation is possibly out-of-sync, since the server intends to return a streamed response whilst the client wants a single one.
Functions ¶
func RegisterStructType ¶
func RegisterStructType(v ...StructValuer)
RegisterStructType works like TryRegisterStructType, but panics instead of returning an error.
func TryRegisterStructType ¶
func TryRegisterStructType(v ...StructValuer) error
TryRegisterStructType takes an arbitrary number of StructValuer instances, validates them, and registers them to be able to decode streams into their respective types. Returns an error in case a struct is invalid.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
type Error ¶
Error represents a handled error from the server or an underlying component. Error contains special fields that may be used to diagnose and/or handle the error, such as Kind (see ErrorKind documentation), an optional set of Headers, an optional Identifier provided by the service implementation, and an optional list of UserData key-values with potentially relevant data regarding the error. For further information, refer to the service's documentation.
func IsManagedError ¶
IsManagedError indicates whether a given error value can be converted to an Error instance, and returns it, in case conversion is possible.
type ErrorKind ¶
type ErrorKind uint
ErrorKind indicates one of the possible errors returned in a YARP stream.
type Header ¶
Header represents a list of headers present in requests and responses. It may be important to notice that headers manipulated by Get, Set, and Del have their keys automatically standardized using MIME Header standard. For further information regarding that conversion, see textproto.CanonicalMIMEHeaderKey.
type IncompatibleTypeError ¶
IncompatibleTypeError indicates that the server returned an unexpected type as a response for a method.
func (IncompatibleTypeError) Error ¶
func (i IncompatibleTypeError) Error() string
type MapValue ¶
type MapValue struct { Keys []interface{} Values []interface{} }
MapValue represents a map that has not been transformed into a map[T]U. For each Keys[n], the associated value can be obtained through Values[n].
type Middleware ¶
type Middleware func(req *RPCRequest) (*RPCRequest, error)
Middleware is a simple function that takes an RPCRequest, and either returns the same request and no error, in case the server should continue processing it, or an error, in case the server should stop processing it. Middlewares may update fields and the context present in the RPCRequest object, and return an updated value to be passed to the rest or the responder chain.
type OneOfValue ¶
type OneOfValue struct { Index int Data interface{} }
OneOfValue represents an oneof value that has not been applied to a struct field. The value includes both the Index of the oneof field that should be set, and a Data value including the value that should be applied to such field.
type Option ¶
type Option func(c *options)
Option represents an arbitrary option to be set on a Client or Server instance. See WithTimeout and WithTLS.
func WithTimeout ¶
WithTimeout determines a timeout value for a given Client or Server, and has different meanings depending on where it is used: For Server, indicates how long the server may wait for client to provide headers for a request, disconnecting the client in case it fails to provide headers under that duration. For Client, the value is used as a Dial timeout for the underlying net.Dialer.
type RPCRequest ¶
type RPCRequest struct { Method string Identifier uint64 MethodFQN string Headers Header // contains filtered or unexported fields }
RPCRequest represents an incoming RPC request. Although fields like Method, and Identifier are available, changing them have no effect other than passing them down to other middlewares. Changing the context using WithContext, however, causes the new context to be provided to the target Handler. The same applies to Headers.
func (RPCRequest) Context ¶
func (r RPCRequest) Context() context.Context
Context returns the context for the current RPCRequest. To update the context, use WithContext to obtain a new RPCRequest instance that must be returned from the Middleware function.
func (RPCRequest) WithContext ¶
func (r RPCRequest) WithContext(ctx context.Context) *RPCRequest
WithContext returns a new RPCRequest instance using the given context as its context.
type Request ¶
Request represents an internal representation of an incoming request through a stream. Method indicates which handler should be called, and Headers contains any metadata sent by a client.
type Response ¶
Response indicates the beginning of a response in a YARP stream. The response contains a set of arbitrary headers, followed by a boolean value indicating whether the server will begin to provide a stream response comprised of potentially multiple objects.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a server object capable of routing incoming connections and requests.
func NewServer ¶
NewServer creates a new YARP Server with a given bind address and options. When using Unix Domain Sockets, bind must begin with `unix://`, followed by the socket's path. Otherwise, an IP:PORT pair is required. For options, see WithTimeout and WithTLS. Returns a Server instance ready to listen for connections.
func (*Server) RegisterHandler ¶
RegisterHandler registers a given handler identified by k, and named by n, having a given handler function. This function is not intended to be used directly by users, but rather for autogenerated code responsible for registering a given service on a Server instance.
func (*Server) Shutdown ¶
Shutdown prevents the current Server from accepting new connections, and waits until all current clients disconnects, or the provided ctx expires. In case ctx expires before all clients are finished, remaining clients will be forcefully disconnected. Passing a context without a timeout waits indefinitely for clients to finish.
func (*Server) Start ¶
Start creates a new net.Listener for the address provided to NewServer, and invokes StartListener with it. This function always returns an error, that may occur during the net.Listen (bind) operation, during the server execution, or an ErrServerClosed in case the server is shutdown.
func (*Server) StartListener ¶
StartListener starts the select loop for a given net.Listener and delegates incoming connections to goroutines, invoking all required Middlewares and handler functions. This function always returns an error, be during the server execution, or when Shutdown is called.
func (*Server) Use ¶
func (s *Server) Use(mid Middleware)
Use registers a given Middleware to be executed on new requests.
type StructValuer ¶
StructValuer represents a struct that can be encoded into a YARP stream.
type Structure ¶
type Structure struct {
UnknownFields []UnknownField
}
Structure contains a list of UnknownFields obtained during the decoding process.
type Type ¶
type Type int
Type represents the types present in a YARP stream.
const ( // Invalid represents an unknown or corrupt type. Invalid Type = iota // Void represents a void (empty) type. Void // Scalar represents all signed and unsigned integer values, along with // booleans. Scalar // Float represents both 32 and 64-bit float values. Float // Array represents a list of a single type. Array // Struct represents a user-defined structure. Struct // String represents a UTF-8 character array String // Map represents an associative array between two types. Map // OneOf represents a field containing one of several possible types. OneOf )
type UnknownField ¶
UnknownField represents a field present in a stream, but not handled by the known structure type.