Documentation
¶
Overview ¶
Package nanorpc provides the core types and utilities for the NanoRPC protocol.
This package contains:
- Protocol buffer definitions and generated types
- HashCache for efficient path hashing using FNV-1a
- Request/response encoding and decoding utilities
- Type aliases and helpers for working with protocol internals
- Error handling utilities
The actual client and server implementations are in separate packages:
- Client: github.com/amery/nanorpc/pkg/nanorpc/client
- Server: github.com/amery/nanorpc/pkg/nanorpc/server
Index ¶
- Variables
- func AsPathOneOfHash(p PathOneOf) (uint32, bool)
- func AsPathOneOfString(p PathOneOf) (string, bool)
- func DecodeRequestData[T proto.Message](req *NanoRPCRequest, out T) (T, bool, error)
- func DecodeResponseData[T proto.Message](res *NanoRPCResponse, out T) (T, bool, error)
- func DecodeSplit(data []byte) (prefixLen, totalLen int, err error)
- func EncodeRequest(req *NanoRPCRequest, data proto.Message) ([]byte, error)
- func EncodeRequestTo(w io.Writer, req *NanoRPCRequest, data proto.Message) (int, error)
- func EncodeResponse(res *NanoRPCResponse, data proto.Message) ([]byte, error)
- func EncodeResponseTo(w io.Writer, res *NanoRPCResponse, data proto.Message) (int, error)
- func IsNoResponse(err error) bool
- func IsNotAuthorized(err error) bool
- func IsNotFound(err error) bool
- func RegisterPath(path string)
- func ResponseAsError(res *NanoRPCResponse) error
- func Split(data []byte, atEOF bool) (advance int, msg []byte, err error)
- type HashCache
- type NanoRPCRequest
- func (*NanoRPCRequest) Descriptor() ([]byte, []int)deprecated
- func (x *NanoRPCRequest) GetData() []byte
- func (x *NanoRPCRequest) GetPath() string
- func (x *NanoRPCRequest) GetPathHash() uint32
- func (m *NanoRPCRequest) GetPathOneof() isNanoRPCRequest_PathOneof
- func (x *NanoRPCRequest) GetRequestId() int32
- func (x *NanoRPCRequest) GetRequestType() NanoRPCRequest_Type
- func (*NanoRPCRequest) ProtoMessage()
- func (x *NanoRPCRequest) ProtoReflect() protoreflect.Message
- func (x *NanoRPCRequest) Reset()
- func (x *NanoRPCRequest) String() string
- type NanoRPCRequest_Path
- type NanoRPCRequest_PathHash
- type NanoRPCRequest_Type
- func (NanoRPCRequest_Type) Descriptor() protoreflect.EnumDescriptor
- func (x NanoRPCRequest_Type) Enum() *NanoRPCRequest_Type
- func (NanoRPCRequest_Type) EnumDescriptor() ([]byte, []int)deprecated
- func (x NanoRPCRequest_Type) Number() protoreflect.EnumNumber
- func (x NanoRPCRequest_Type) String() string
- func (NanoRPCRequest_Type) Type() protoreflect.EnumType
- type NanoRPCResponse
- func (*NanoRPCResponse) Descriptor() ([]byte, []int)deprecated
- func (x *NanoRPCResponse) GetData() []byte
- func (x *NanoRPCResponse) GetRequestId() int32
- func (x *NanoRPCResponse) GetResponseMessage() string
- func (x *NanoRPCResponse) GetResponseStatus() NanoRPCResponse_Status
- func (x *NanoRPCResponse) GetResponseType() NanoRPCResponse_Type
- func (*NanoRPCResponse) ProtoMessage()
- func (x *NanoRPCResponse) ProtoReflect() protoreflect.Message
- func (x *NanoRPCResponse) Reset()
- func (x *NanoRPCResponse) String() string
- type NanoRPCResponse_Status
- func (NanoRPCResponse_Status) Descriptor() protoreflect.EnumDescriptor
- func (x NanoRPCResponse_Status) Enum() *NanoRPCResponse_Status
- func (NanoRPCResponse_Status) EnumDescriptor() ([]byte, []int)deprecated
- func (x NanoRPCResponse_Status) Number() protoreflect.EnumNumber
- func (x NanoRPCResponse_Status) String() string
- func (NanoRPCResponse_Status) Type() protoreflect.EnumType
- type NanoRPCResponse_Type
- func (NanoRPCResponse_Type) Descriptor() protoreflect.EnumDescriptor
- func (x NanoRPCResponse_Type) Enum() *NanoRPCResponse_Type
- func (NanoRPCResponse_Type) EnumDescriptor() ([]byte, []int)deprecated
- func (x NanoRPCResponse_Type) Number() protoreflect.EnumNumber
- func (x NanoRPCResponse_Type) String() string
- func (NanoRPCResponse_Type) Type() protoreflect.EnumType
- type PathOneOf
- type ResponseError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoResponse indicates the server didn't answer before disconnection ErrNoResponse = core.NewTimeoutError(errors.New("no response")) // ErrInternalServerError indicates the server reported an internal error ErrInternalServerError = errors.New("internal server error") // ErrSessionClosed indicates the session has been closed ErrSessionClosed = errors.New("session closed") // ErrHashCollision indicates two different paths hash to the same value ErrHashCollision = errors.New("hash collision detected") )
var ( NanoRPCRequest_Type_name = map[int32]string{ 0: "TYPE_UNSPECIFIED", 1: "TYPE_PING", 2: "TYPE_REQUEST", 3: "TYPE_SUBSCRIBE", } NanoRPCRequest_Type_value = map[string]int32{ "TYPE_UNSPECIFIED": 0, "TYPE_PING": 1, "TYPE_REQUEST": 2, "TYPE_SUBSCRIBE": 3, } )
Enum value maps for NanoRPCRequest_Type.
var ( NanoRPCResponse_Type_name = map[int32]string{ 0: "TYPE_UNSPECIFIED", 1: "TYPE_PONG", 2: "TYPE_RESPONSE", 3: "TYPE_UPDATE", } NanoRPCResponse_Type_value = map[string]int32{ "TYPE_UNSPECIFIED": 0, "TYPE_PONG": 1, "TYPE_RESPONSE": 2, "TYPE_UPDATE": 3, } )
Enum value maps for NanoRPCResponse_Type.
var ( NanoRPCResponse_Status_name = map[int32]string{ 0: "STATUS_UNSPECIFIED", 1: "STATUS_OK", 2: "STATUS_NOT_FOUND", 3: "STATUS_NOT_AUTHORIZED", 4: "STATUS_INTERNAL_ERROR", } NanoRPCResponse_Status_value = map[string]int32{ "STATUS_UNSPECIFIED": 0, "STATUS_OK": 1, "STATUS_NOT_FOUND": 2, "STATUS_NOT_AUTHORIZED": 3, "STATUS_INTERNAL_ERROR": 4, } )
Enum value maps for NanoRPCResponse_Status.
var File_nanorpc_proto protoreflect.FileDescriptor
Functions ¶
func AsPathOneOfHash ¶ added in v0.4.99
AsPathOneOfHash extracts the path hash from a PathOneOf if it contains one. Returns the hash value and true if the PathOneOf contains a path hash, or 0 and false if it contains a string path or is nil.
func AsPathOneOfString ¶ added in v0.4.99
AsPathOneOfString extracts the string path from a PathOneOf if it contains one. Returns the path string and true if the PathOneOf contains a string path, or an empty string and false if it contains a hash or is nil.
func DecodeRequestData ¶ added in v0.3.1
func DecodeRequestData[T proto.Message](req *NanoRPCRequest, out T) (T, bool, error)
DecodeRequestData attempts to decode the payload of a NanoRPC request.
func DecodeResponseData ¶ added in v0.3.1
func DecodeResponseData[T proto.Message](res *NanoRPCResponse, out T) (T, bool, error)
DecodeResponseData attempts to decode the payload of a NanoRPC response.
func DecodeSplit ¶
DecodeSplit identifies the size of the wrapped message and if enough data is already buffered.
func EncodeRequest ¶ added in v0.1.2
func EncodeRequest(req *NanoRPCRequest, data proto.Message) ([]byte, error)
EncodeRequest encodes a wrapped NanoRPC request. If request data is provided, it will be encoded into the NanoRPCRequest, otherwise the request will be used as-is.
func EncodeRequestTo ¶ added in v0.2.1
EncodeRequestTo encodes a wrapped NanoRPC request. If request data is provided, it will be encoded into the NanoRPCRequest, otherwise the request will be used as-is.
func EncodeResponse ¶ added in v0.1.2
func EncodeResponse(res *NanoRPCResponse, data proto.Message) ([]byte, error)
EncodeResponse encodes a wrapped NanoRPC response. If response data is provided, it will be encoded into the NanoRPCResponse, otherwise the response will be used as-is.
func EncodeResponseTo ¶ added in v0.2.1
EncodeResponseTo encodes a wrapped NanoRPC response. If response data is provided, it will be encoded into the NanoRPCResponse, otherwise the response will be used as-is.
func IsNoResponse ¶ added in v0.3.7
IsNoResponse checks if the error represents no response being received. This error is also used to notify the connection was closed.
func IsNotAuthorized ¶ added in v0.3.7
IsNotAuthorized checks if the error represents a STATUS_NOT_AUTHORIZED response.
func IsNotFound ¶ added in v0.3.7
IsNotFound checks if the error represents a STATUS_NOT_FOUND response.
func RegisterPath ¶ added in v0.1.1
func RegisterPath(path string)
RegisterPath pre-computes the path_hash for a given path into a the global cache.
func ResponseAsError ¶ added in v0.2.0
func ResponseAsError(res *NanoRPCResponse) error
ResponseAsError extracts an error from the status of a response.
Types ¶
type HashCache ¶ added in v0.1.1
type HashCache struct {
// contains filtered or unexported fields
}
HashCache stores and computes path_hash values for [NanoRPCRequest]s.
func (*HashCache) DehashRequest ¶ added in v0.1.1
func (hc *HashCache) DehashRequest(r *NanoRPCRequest) (*NanoRPCRequest, bool)
DehashRequest attempts to convert path_hash in a NanoRPCRequest into a string path.
type NanoRPCRequest ¶
type NanoRPCRequest struct {
RequestId int32 `protobuf:"varint,1,opt,name=request_id,json=requestId,proto3" json:"request_id,omitempty"`
RequestType NanoRPCRequest_Type `protobuf:"varint,2,opt,name=request_type,json=requestType,proto3,enum=NanoRPCRequest_Type" json:"request_type,omitempty"`
// Types that are assignable to PathOneof:
//
// *NanoRPCRequest_PathHash
// *NanoRPCRequest_Path
PathOneof isNanoRPCRequest_PathOneof `protobuf_oneof:"path_oneof"`
Data []byte `protobuf:"bytes,10,opt,name=data,proto3" json:"data,omitempty"`
// contains filtered or unexported fields
}
func DecodeRequest ¶
func DecodeRequest(data []byte) (*NanoRPCRequest, int, error)
DecodeRequest attempts to decode a wrapped NanoRPC request from a buffer
func DehashRequest ¶ added in v0.1.1
func DehashRequest(r *NanoRPCRequest) (*NanoRPCRequest, bool)
DehashRequest attempts to convert path_hash in a NanoRPCRequest into a string path.
func (*NanoRPCRequest) Descriptor
deprecated
func (*NanoRPCRequest) Descriptor() ([]byte, []int)
Deprecated: Use NanoRPCRequest.ProtoReflect.Descriptor instead.
func (*NanoRPCRequest) GetData ¶
func (x *NanoRPCRequest) GetData() []byte
func (*NanoRPCRequest) GetPath ¶
func (x *NanoRPCRequest) GetPath() string
func (*NanoRPCRequest) GetPathHash ¶
func (x *NanoRPCRequest) GetPathHash() uint32
func (*NanoRPCRequest) GetPathOneof ¶
func (m *NanoRPCRequest) GetPathOneof() isNanoRPCRequest_PathOneof
func (*NanoRPCRequest) GetRequestId ¶
func (x *NanoRPCRequest) GetRequestId() int32
func (*NanoRPCRequest) GetRequestType ¶
func (x *NanoRPCRequest) GetRequestType() NanoRPCRequest_Type
func (*NanoRPCRequest) ProtoMessage ¶
func (*NanoRPCRequest) ProtoMessage()
func (*NanoRPCRequest) ProtoReflect ¶
func (x *NanoRPCRequest) ProtoReflect() protoreflect.Message
func (*NanoRPCRequest) Reset ¶
func (x *NanoRPCRequest) Reset()
func (*NanoRPCRequest) String ¶
func (x *NanoRPCRequest) String() string
type NanoRPCRequest_Path ¶
type NanoRPCRequest_Path struct {
Path string `protobuf:"bytes,4,opt,name=path,proto3,oneof"`
}
type NanoRPCRequest_PathHash ¶
type NanoRPCRequest_PathHash struct {
PathHash uint32 `protobuf:"varint,3,opt,name=path_hash,json=pathHash,proto3,oneof"` // FNV-1a of path
}
type NanoRPCRequest_Type ¶
type NanoRPCRequest_Type int32
const ( NanoRPCRequest_TYPE_UNSPECIFIED NanoRPCRequest_Type = 0 NanoRPCRequest_TYPE_PING NanoRPCRequest_Type = 1 NanoRPCRequest_TYPE_REQUEST NanoRPCRequest_Type = 2 NanoRPCRequest_TYPE_SUBSCRIBE NanoRPCRequest_Type = 3 )
func (NanoRPCRequest_Type) Descriptor ¶
func (NanoRPCRequest_Type) Descriptor() protoreflect.EnumDescriptor
func (NanoRPCRequest_Type) Enum ¶
func (x NanoRPCRequest_Type) Enum() *NanoRPCRequest_Type
func (NanoRPCRequest_Type) EnumDescriptor
deprecated
func (NanoRPCRequest_Type) EnumDescriptor() ([]byte, []int)
Deprecated: Use NanoRPCRequest_Type.Descriptor instead.
func (NanoRPCRequest_Type) Number ¶
func (x NanoRPCRequest_Type) Number() protoreflect.EnumNumber
func (NanoRPCRequest_Type) String ¶
func (x NanoRPCRequest_Type) String() string
func (NanoRPCRequest_Type) Type ¶
func (NanoRPCRequest_Type) Type() protoreflect.EnumType
type NanoRPCResponse ¶
type NanoRPCResponse struct {
RequestId int32 `protobuf:"varint,1,opt,name=request_id,json=requestId,proto3" json:"request_id,omitempty"`
ResponseType NanoRPCResponse_Type `protobuf:"varint,2,opt,name=response_type,json=responseType,proto3,enum=NanoRPCResponse_Type" json:"response_type,omitempty"`
ResponseStatus NanoRPCResponse_Status `` /* 132-byte string literal not displayed */
ResponseMessage string `protobuf:"bytes,4,opt,name=response_message,json=responseMessage,proto3" json:"response_message,omitempty"`
Data []byte `protobuf:"bytes,10,opt,name=data,proto3" json:"data,omitempty"`
// contains filtered or unexported fields
}
func DecodeResponse ¶
func DecodeResponse(data []byte) (*NanoRPCResponse, int, error)
DecodeResponse attempts to decode a wrapped NanoRPC response from a buffer.
func (*NanoRPCResponse) Descriptor
deprecated
func (*NanoRPCResponse) Descriptor() ([]byte, []int)
Deprecated: Use NanoRPCResponse.ProtoReflect.Descriptor instead.
func (*NanoRPCResponse) GetData ¶
func (x *NanoRPCResponse) GetData() []byte
func (*NanoRPCResponse) GetRequestId ¶
func (x *NanoRPCResponse) GetRequestId() int32
func (*NanoRPCResponse) GetResponseMessage ¶
func (x *NanoRPCResponse) GetResponseMessage() string
func (*NanoRPCResponse) GetResponseStatus ¶
func (x *NanoRPCResponse) GetResponseStatus() NanoRPCResponse_Status
func (*NanoRPCResponse) GetResponseType ¶
func (x *NanoRPCResponse) GetResponseType() NanoRPCResponse_Type
func (*NanoRPCResponse) ProtoMessage ¶
func (*NanoRPCResponse) ProtoMessage()
func (*NanoRPCResponse) ProtoReflect ¶
func (x *NanoRPCResponse) ProtoReflect() protoreflect.Message
func (*NanoRPCResponse) Reset ¶
func (x *NanoRPCResponse) Reset()
func (*NanoRPCResponse) String ¶
func (x *NanoRPCResponse) String() string
type NanoRPCResponse_Status ¶
type NanoRPCResponse_Status int32
const ( NanoRPCResponse_STATUS_UNSPECIFIED NanoRPCResponse_Status = 0 NanoRPCResponse_STATUS_OK NanoRPCResponse_Status = 1 NanoRPCResponse_STATUS_NOT_FOUND NanoRPCResponse_Status = 2 NanoRPCResponse_STATUS_NOT_AUTHORIZED NanoRPCResponse_Status = 3 NanoRPCResponse_STATUS_INTERNAL_ERROR NanoRPCResponse_Status = 4 )
func (NanoRPCResponse_Status) Descriptor ¶
func (NanoRPCResponse_Status) Descriptor() protoreflect.EnumDescriptor
func (NanoRPCResponse_Status) Enum ¶
func (x NanoRPCResponse_Status) Enum() *NanoRPCResponse_Status
func (NanoRPCResponse_Status) EnumDescriptor
deprecated
func (NanoRPCResponse_Status) EnumDescriptor() ([]byte, []int)
Deprecated: Use NanoRPCResponse_Status.Descriptor instead.
func (NanoRPCResponse_Status) Number ¶
func (x NanoRPCResponse_Status) Number() protoreflect.EnumNumber
func (NanoRPCResponse_Status) String ¶
func (x NanoRPCResponse_Status) String() string
func (NanoRPCResponse_Status) Type ¶
func (NanoRPCResponse_Status) Type() protoreflect.EnumType
type NanoRPCResponse_Type ¶
type NanoRPCResponse_Type int32
const ( NanoRPCResponse_TYPE_UNSPECIFIED NanoRPCResponse_Type = 0 NanoRPCResponse_TYPE_PONG NanoRPCResponse_Type = 1 NanoRPCResponse_TYPE_RESPONSE NanoRPCResponse_Type = 2 NanoRPCResponse_TYPE_UPDATE NanoRPCResponse_Type = 3 )
func (NanoRPCResponse_Type) Descriptor ¶
func (NanoRPCResponse_Type) Descriptor() protoreflect.EnumDescriptor
func (NanoRPCResponse_Type) Enum ¶
func (x NanoRPCResponse_Type) Enum() *NanoRPCResponse_Type
func (NanoRPCResponse_Type) EnumDescriptor
deprecated
func (NanoRPCResponse_Type) EnumDescriptor() ([]byte, []int)
Deprecated: Use NanoRPCResponse_Type.Descriptor instead.
func (NanoRPCResponse_Type) Number ¶
func (x NanoRPCResponse_Type) Number() protoreflect.EnumNumber
func (NanoRPCResponse_Type) String ¶
func (x NanoRPCResponse_Type) String() string
func (NanoRPCResponse_Type) Type ¶
func (NanoRPCResponse_Type) Type() protoreflect.EnumType
type PathOneOf ¶ added in v0.4.99
type PathOneOf = isNanoRPCRequest_PathOneof
PathOneOf is a type alias for the internal isNanoRPCRequest_PathOneof interface. This interface represents the oneof field in NanoRPCRequest that can contain either a string path or a uint32 path hash. It is used by both client and server packages for flexible path handling.
func GetPathOneOfHash ¶ added in v0.4.99
GetPathOneOfHash creates a PathOneOf containing a path hash. This is used when only the 32-bit FNV-1a hash of the path should be sent, which is more efficient for embedded systems with limited bandwidth.
func GetPathOneOfString ¶ added in v0.4.99
GetPathOneOfString creates a PathOneOf containing a string path. This is used when the full path string should be sent in the request.
type ResponseError ¶ added in v0.3.9
type ResponseError struct {
Err error
Msg string
Status NanoRPCResponse_Status
}
ResponseError represents a NanoRPC error response.
func (ResponseError) Error ¶ added in v0.3.9
func (e ResponseError) Error() string
func (ResponseError) String ¶ added in v0.3.9
func (e ResponseError) String() string
func (ResponseError) Unwrap ¶ added in v0.3.9
func (e ResponseError) Unwrap() error
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client implements a reconnecting NanoRPC client.
|
Package client implements a reconnecting NanoRPC client. |
|
Package common provides shared constants, types, and utilities used by both the nanorpc client and server implementations.
|
Package common provides shared constants, types, and utilities used by both the nanorpc client and server implementations. |
|
testutils
Package testutils provides test utilities for nanorpc including assertion helpers and mock implementations for testing.
|
Package testutils provides test utilities for nanorpc including assertion helpers and mock implementations for testing. |
|
Package server implements the NanoRPC server for handling lightweight RPC requests in embedded systems and resource-constrained environments.
|
Package server implements the NanoRPC server for handling lightweight RPC requests in embedded systems and resource-constrained environments. |