srpc

package
v0.31.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 24 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnimplemented is returned if the RPC method was not implemented.
	ErrUnimplemented = errors.New("unimplemented")
	// ErrCompleted is returned if a message is received after the rpc was completed.
	ErrCompleted = errors.New("unexpected packet after rpc was completed")
	// ErrUnrecognizedPacket is returned if the packet type was not recognized.
	ErrUnrecognizedPacket = errors.New("unrecognized packet type")
	// ErrEmptyPacket is returned if nothing is specified in a packet.
	ErrEmptyPacket = errors.New("invalid empty packet")
	// ErrInvalidMessage indicates the message failed to parse.
	ErrInvalidMessage = errors.New("invalid message")
	// ErrEmptyMethodID is returned if the method id was empty.
	ErrEmptyMethodID = errors.New("method id empty")
	// ErrEmptyServiceID is returned if the service id was empty.
	ErrEmptyServiceID = errors.New("service id empty")
	// ErrNoAvailableClients is returned if no clients were available.
	ErrNoAvailableClients = errors.New("no available rpc clients")
)

Functions

func AcceptMuxedListener added in v0.8.3

func AcceptMuxedListener(ctx context.Context, lis net.Listener, srv *Server, yamuxConf *yamux.Config) error

AcceptMuxedListener accepts incoming connections from a net.Listener.

Uses the default yamux muxer. If yamux conf is nil, uses the defaults.

func CheckStripPrefix added in v0.12.0

func CheckStripPrefix(id string, matchPrefixes []string) (strippedID string, matchedPrefix string)

CheckStripPrefix checks if the string has any of the given prefixes and strips the matched prefix if any.

if len(matchPrefixes) == 0 returns the ID without changing it.

func Listen added in v0.18.1

func Listen(ctx context.Context, addr string, srv *Server, errCh <-chan error) error

Listen listens for incoming connections with TCP on the given address with the default muxed conn type. Returns on any fatal error or if ctx was canceled. errCh is an optional error channel (can be nil)

func NewMuxedConn added in v0.8.2

func NewMuxedConn(conn net.Conn, outbound bool, yamuxConf *yamux.Config) (network.MuxedConn, error)

NewMuxedConn constructs a new MuxedConn from a net.Conn.

If yamuxConf is nil, uses defaults.

func NewMuxedConnWithRwc added in v0.10.8

func NewMuxedConnWithRwc(
	ctx context.Context,
	rwc io.ReadWriteCloser,
	outbound bool,
	yamuxConf *yamux.Config,
) (network.MuxedConn, error)

NewMuxedConnWithRwc builds a new MuxedConn with a io.ReadWriteCloser.

If yamuxConf is nil, uses defaults.

func NewPipeStream

func NewPipeStream(ctx context.Context) (Stream, Stream)

NewPipeStream constructs a new in-memory stream.

func NewWebSocketConn

func NewWebSocketConn(
	ctx context.Context,
	conn *websocket.Conn,
	isServer bool,
	yamuxConf *yamux.Config,
) (network.MuxedConn, error)

NewWebSocketConn wraps a websocket into a MuxedConn. if yamuxConf is unset, uses the defaults.

func NewYamuxConfig added in v0.13.0

func NewYamuxConfig() *yamux.Config

NewYamuxConfig builds the default yamux configuration.

Types

type CallData

type CallData struct {

	// Data contains the packet in the sequence.
	Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
	// DataIsZero indicates Data is set with an empty message.
	DataIsZero bool `protobuf:"varint,2,opt,name=data_is_zero,json=dataIsZero,proto3" json:"dataIsZero,omitempty"`
	// Complete indicates the RPC call is completed.
	Complete bool `protobuf:"varint,3,opt,name=complete,proto3" json:"complete,omitempty"`
	// Error contains any error that caused the RPC to fail.
	// If set, implies complete=true.
	Error string `protobuf:"bytes,4,opt,name=error,proto3" json:"error,omitempty"`
	// contains filtered or unexported fields
}

CallData contains a message in a streaming RPC sequence.

func (*CallData) CloneMessageVT added in v0.18.3

func (m *CallData) CloneMessageVT() protobuf_go_lite.CloneMessage

func (*CallData) CloneVT added in v0.12.3

func (m *CallData) CloneVT() *CallData

func (*CallData) EqualMessageVT added in v0.18.3

func (this *CallData) EqualMessageVT(thatMsg any) bool

func (*CallData) EqualVT

func (this *CallData) EqualVT(that *CallData) bool

func (*CallData) GetComplete

func (x *CallData) GetComplete() bool

func (*CallData) GetData

func (x *CallData) GetData() []byte

func (*CallData) GetDataIsZero added in v0.1.8

func (x *CallData) GetDataIsZero() bool

func (*CallData) GetError

func (x *CallData) GetError() string

func (*CallData) MarshalJSON added in v0.30.0

func (x *CallData) MarshalJSON() ([]byte, error)

MarshalJSON marshals the CallData to JSON.

func (*CallData) MarshalProtoJSON added in v0.30.0

func (x *CallData) MarshalProtoJSON(s *json.MarshalState)

MarshalProtoJSON marshals the CallData message to JSON.

func (*CallData) MarshalProtoText added in v0.31.4

func (x *CallData) MarshalProtoText() string

func (*CallData) MarshalToSizedBufferVT

func (m *CallData) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*CallData) MarshalToVT

func (m *CallData) MarshalToVT(dAtA []byte) (int, error)

func (*CallData) MarshalVT

func (m *CallData) MarshalVT() (dAtA []byte, err error)

func (*CallData) ProtoMessage

func (*CallData) ProtoMessage()

func (*CallData) Reset

func (x *CallData) Reset()

func (*CallData) SizeVT

func (m *CallData) SizeVT() (n int)

func (*CallData) String

func (x *CallData) String() string

func (*CallData) UnmarshalJSON added in v0.30.1

func (x *CallData) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the CallData from JSON.

func (*CallData) UnmarshalProtoJSON added in v0.30.1

func (x *CallData) UnmarshalProtoJSON(s *json.UnmarshalState)

UnmarshalProtoJSON unmarshals the CallData message from JSON.

func (*CallData) UnmarshalVT

func (m *CallData) UnmarshalVT(dAtA []byte) error

func (*CallData) Validate

func (p *CallData) Validate() error

Validate performs cursory validation of the packet.

type CallStart

type CallStart struct {

	// RpcService is the service to contact.
	// Must be set.
	RpcService string `protobuf:"bytes,1,opt,name=rpc_service,json=rpcService,proto3" json:"rpcService,omitempty"`
	// RpcMethod is the RPC method to call.
	// Must be set.
	RpcMethod string `protobuf:"bytes,2,opt,name=rpc_method,json=rpcMethod,proto3" json:"rpcMethod,omitempty"`
	// Data contains the request or the first message in the stream.
	// Optional if streaming.
	Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
	// DataIsZero indicates Data is set with an empty message.
	DataIsZero bool `protobuf:"varint,4,opt,name=data_is_zero,json=dataIsZero,proto3" json:"dataIsZero,omitempty"`
	// contains filtered or unexported fields
}

CallStart requests starting a new RPC call.

func (*CallStart) CloneMessageVT added in v0.18.3

func (m *CallStart) CloneMessageVT() protobuf_go_lite.CloneMessage

func (*CallStart) CloneVT added in v0.12.3

func (m *CallStart) CloneVT() *CallStart

func (*CallStart) EqualMessageVT added in v0.18.3

func (this *CallStart) EqualMessageVT(thatMsg any) bool

func (*CallStart) EqualVT

func (this *CallStart) EqualVT(that *CallStart) bool

func (*CallStart) GetData

func (x *CallStart) GetData() []byte

func (*CallStart) GetDataIsZero added in v0.1.8

func (x *CallStart) GetDataIsZero() bool

func (*CallStart) GetRpcMethod

func (x *CallStart) GetRpcMethod() string

func (*CallStart) GetRpcService

func (x *CallStart) GetRpcService() string

func (*CallStart) MarshalJSON added in v0.30.0

func (x *CallStart) MarshalJSON() ([]byte, error)

MarshalJSON marshals the CallStart to JSON.

func (*CallStart) MarshalProtoJSON added in v0.30.0

func (x *CallStart) MarshalProtoJSON(s *json.MarshalState)

MarshalProtoJSON marshals the CallStart message to JSON.

func (*CallStart) MarshalProtoText added in v0.31.4

func (x *CallStart) MarshalProtoText() string

func (*CallStart) MarshalToSizedBufferVT

func (m *CallStart) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*CallStart) MarshalToVT

func (m *CallStart) MarshalToVT(dAtA []byte) (int, error)

func (*CallStart) MarshalVT

func (m *CallStart) MarshalVT() (dAtA []byte, err error)

func (*CallStart) ProtoMessage

func (*CallStart) ProtoMessage()

func (*CallStart) Reset

func (x *CallStart) Reset()

func (*CallStart) SizeVT

func (m *CallStart) SizeVT() (n int)

func (*CallStart) String

func (x *CallStart) String() string

func (*CallStart) UnmarshalJSON added in v0.30.1

func (x *CallStart) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the CallStart from JSON.

func (*CallStart) UnmarshalProtoJSON added in v0.30.1

func (x *CallStart) UnmarshalProtoJSON(s *json.UnmarshalState)

UnmarshalProtoJSON unmarshals the CallStart message from JSON.

func (*CallStart) UnmarshalVT

func (m *CallStart) UnmarshalVT(dAtA []byte) error

func (*CallStart) Validate

func (p *CallStart) Validate() error

Validate performs cursory validation of the packet.

type Client

type Client interface {
	// ExecCall executes a request/reply RPC with the remote.
	ExecCall(ctx context.Context, service, method string, in, out Message) error

	// NewStream starts a streaming RPC with the remote & returns the stream.
	// firstMsg is optional.
	NewStream(ctx context.Context, service, method string, firstMsg Message) (Stream, error)
}

Client implements a SRPC client which can initiate RPC streams.

func Dial added in v0.18.1

func Dial(addr string) (Client, error)

Dial dials a remote server using TCP with the default muxed conn type.

func NewClient

func NewClient(openStream OpenStreamFunc) Client

NewClient constructs a client with a OpenStreamFunc.

func NewClientWithConn added in v0.8.2

func NewClientWithConn(conn net.Conn, outbound bool, yamuxConf *yamux.Config) (Client, error)

NewClientWithConn constructs the muxer and the client.

if yamuxConf is nil, uses defaults.

func NewClientWithMuxedConn added in v0.3.6

func NewClientWithMuxedConn(conn network.MuxedConn) Client

NewClientWithMuxedConn constructs a new client with a MuxedConn.

type ClientRPC

type ClientRPC struct {
	// contains filtered or unexported fields
}

ClientRPC represents the client side of an on-going RPC call message stream.

func NewClientRPC

func NewClientRPC(ctx context.Context, service, method string) *ClientRPC

NewClientRPC constructs a new ClientRPC session and writes CallStart. the writer will be closed when the ClientRPC completes. service and method must be specified. must call Start after creating the RPC object.

func (*ClientRPC) Close

func (r *ClientRPC) Close()

Close releases any resources held by the ClientRPC.

func (*ClientRPC) Context

func (c *ClientRPC) Context() context.Context

Context is canceled when the rpc has finished.

func (*ClientRPC) HandleCallCancel added in v0.15.0

func (c *ClientRPC) HandleCallCancel() error

HandleCallCancel handles the call cancel packet.

func (*ClientRPC) HandleCallData

func (c *ClientRPC) HandleCallData(pkt *CallData) error

HandleCallData handles the call data packet.

func (*ClientRPC) HandleCallStart

func (r *ClientRPC) HandleCallStart(pkt *CallStart) error

HandleCallStart handles the call start packet.

func (*ClientRPC) HandlePacket

func (r *ClientRPC) HandlePacket(msg *Packet) error

HandlePacket handles an incoming parsed message packet.

func (*ClientRPC) HandlePacketData

func (r *ClientRPC) HandlePacketData(data []byte) error

HandlePacketData handles an incoming unparsed message packet.

func (*ClientRPC) HandleStreamClose added in v0.8.0

func (r *ClientRPC) HandleStreamClose(closeErr error)

HandleStreamClose handles the stream closing optionally w/ an error.

func (*ClientRPC) ReadOne added in v0.8.5

func (c *ClientRPC) ReadOne() ([]byte, error)

ReadOne reads a single message and returns.

returns io.EOF if the stream ended without a packet.

func (*ClientRPC) Start

func (r *ClientRPC) Start(writer PacketWriter, writeFirstMsg bool, firstMsg []byte) error

Start sets the writer and writes the MsgSend message. must only be called once!

func (*ClientRPC) Wait added in v0.15.0

func (c *ClientRPC) Wait(ctx context.Context) error

Wait waits for the RPC to finish.

func (*ClientRPC) WriteCallData added in v0.15.0

func (c *ClientRPC) WriteCallData(data []byte, complete bool, err error) error

WriteCallData writes a call data packet.

func (*ClientRPC) WriteCancel added in v0.15.3

func (c *ClientRPC) WriteCancel() error

WriteCancel writes a call cancel packet.

type ClientSet added in v0.12.2

type ClientSet struct {
	// contains filtered or unexported fields
}

ClientSet wraps a list of clients into one Client.

func NewClientSet added in v0.12.2

func NewClientSet(clients []Client) *ClientSet

NewClientSet constructs a new client set.

func (*ClientSet) ExecCall added in v0.12.2

func (c *ClientSet) ExecCall(
	ctx context.Context,
	service, method string,
	in, out Message,
) error

ExecCall executes a request/reply RPC with the remote.

func (*ClientSet) NewStream added in v0.12.2

func (c *ClientSet) NewStream(
	ctx context.Context,
	service, method string,
	firstMsg Message,
) (Stream, error)

NewStream starts a streaming RPC with the remote & returns the stream. firstMsg is optional.

type CloseHandler added in v0.8.0

type CloseHandler = func(closeErr error)

CloseHandler handles the stream closing with an optional error.

type HTTPServer

type HTTPServer struct {
	// contains filtered or unexported fields
}

HTTPServer implements the SRPC server.

func NewHTTPServer

func NewHTTPServer(mux Mux, path string) (*HTTPServer, error)

NewHTTPServer builds a http server / handler. if path is empty, serves on all routes.

func (*HTTPServer) ServeHTTP

func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Handler

type Handler interface {
	// Invoker invokes the methods.
	Invoker

	// GetServiceID returns the ID of the service.
	GetServiceID() string
	// GetMethodIDs returns the list of methods for the service.
	GetMethodIDs() []string
}

Handler describes a SRPC call handler implementation.

type Invoker added in v0.10.7

type Invoker interface {
	// InvokeMethod invokes the method matching the service & method ID.
	// Returns false, nil if not found.
	// If service string is empty, ignore it.
	InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)
}

Invoker is a function for invoking SRPC service methods.

type InvokerFunc added in v0.22.5

type InvokerFunc func(serviceID, methodID string, strm Stream) (bool, error)

InvokerFunc is a function implementing InvokeMethod.

func (InvokerFunc) InvokeMethod added in v0.22.5

func (f InvokerFunc) InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)

InvokeMethod invokes the method matching the service & method ID. Returns false, nil if not found. If service string is empty, ignore it.

type InvokerSlice added in v0.15.2

type InvokerSlice []Invoker

InvokerSlice is a list of invokers.

func (InvokerSlice) InvokeMethod added in v0.15.2

func (s InvokerSlice) InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)

InvokeMethod invokes the method matching the service & method ID. Returns false, nil if not found. If service string is empty, ignore it.

type Message

type Message = protobuf_go_lite.Message

Message is the vtprotobuf message interface. TODO use VTMessage interface

type MsgStream added in v0.4.0

type MsgStream struct {
	// contains filtered or unexported fields
}

MsgStream implements the stream interface passed to implementations.

func NewMsgStream added in v0.4.0

func NewMsgStream(
	ctx context.Context,
	rw MsgStreamRw,
	closeCb func(),
) *MsgStream

NewMsgStream constructs a new Stream with a ClientRPC. dataCh should be closed when no more messages will arrive.

func (*MsgStream) Close added in v0.4.0

func (r *MsgStream) Close() error

Close closes the stream.

func (*MsgStream) CloseSend added in v0.4.0

func (r *MsgStream) CloseSend() error

CloseSend signals to the remote that we will no longer send any messages.

func (*MsgStream) Context added in v0.4.0

func (r *MsgStream) Context() context.Context

Context is canceled when the Stream is no longer valid.

func (*MsgStream) MsgRecv added in v0.4.0

func (r *MsgStream) MsgRecv(msg Message) error

MsgRecv receives an incoming message from the remote. Parses the message into the object at msg.

func (*MsgStream) MsgSend added in v0.4.0

func (r *MsgStream) MsgSend(msg Message) error

MsgSend sends the message to the remote.

type MsgStreamRw added in v0.15.0

type MsgStreamRw interface {
	// ReadOne reads a single message and returns.
	//
	// returns io.EOF if the stream ended.
	ReadOne() ([]byte, error)

	// WriteCallData writes a call data packet.
	WriteCallData(data []byte, complete bool, err error) error
}

MsgStreamRw is the read-write interface for MsgStream.

type Mux

type Mux interface {
	// Invoker invokes the methods.
	Invoker

	// Register registers a new RPC method handler (service).
	Register(handler Handler) error
	// HasService checks if the service ID exists in the handlers.
	HasService(serviceID string) bool
	// HasServiceMethod checks if <service-id, method-id> exists in the handlers.
	HasServiceMethod(serviceID, methodID string) bool
}

Mux contains a set of <service, method> handlers.

func NewMux

func NewMux(fallbackInvokers ...Invoker) Mux

NewMux constructs a new Mux.

fallbackInvokers is the list of fallback Invokers to call in the case that the service/method is not found on this mux.

type OpenStreamFunc

type OpenStreamFunc = func(
	ctx context.Context,
	msgHandler PacketDataHandler,
	closeHandler CloseHandler,
) (PacketWriter, error)

OpenStreamFunc opens a stream with a remote. msgHandler must not be called concurrently.

func NewOpenStreamWithMuxedConn added in v0.3.6

func NewOpenStreamWithMuxedConn(conn network.MuxedConn) OpenStreamFunc

NewOpenStreamWithMuxedConn constructs a OpenStream func with a MuxedConn.

func NewServerPipe

func NewServerPipe(server *Server) OpenStreamFunc

NewServerPipe constructs a open stream func which creates an in-memory Pipe Stream with the given Server. Starts read pumps for both. Starts the HandleStream function on the server in a separate goroutine.

type Packet

type Packet struct {

	// Body is the packet body.
	//
	// Types that are assignable to Body:
	//
	//	*Packet_CallStart
	//	*Packet_CallData
	//	*Packet_CallCancel
	Body isPacket_Body `protobuf_oneof:"body"`
	// contains filtered or unexported fields
}

Packet is a message sent over a srpc packet connection.

func NewCallCancelPacket added in v0.14.0

func NewCallCancelPacket() *Packet

NewCallCancelPacket constructs a new CallCancel packet with cancel.

func NewCallDataPacket

func NewCallDataPacket(data []byte, dataIsZero bool, complete bool, err error) *Packet

NewCallDataPacket constructs a new CallData packet.

func NewCallStartPacket

func NewCallStartPacket(service, method string, data []byte, dataIsZero bool) *Packet

NewCallStartPacket constructs a new CallStart packet.

func (*Packet) CloneMessageVT added in v0.18.3

func (m *Packet) CloneMessageVT() protobuf_go_lite.CloneMessage

func (*Packet) CloneVT added in v0.12.3

func (m *Packet) CloneVT() *Packet

func (*Packet) EqualMessageVT added in v0.18.3

func (this *Packet) EqualMessageVT(thatMsg any) bool

func (*Packet) EqualVT

func (this *Packet) EqualVT(that *Packet) bool

func (*Packet) GetBody

func (m *Packet) GetBody() isPacket_Body

func (*Packet) GetCallCancel added in v0.14.0

func (x *Packet) GetCallCancel() bool

func (*Packet) GetCallData

func (x *Packet) GetCallData() *CallData

func (*Packet) GetCallStart

func (x *Packet) GetCallStart() *CallStart

func (*Packet) MarshalJSON added in v0.30.0

func (x *Packet) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Packet to JSON.

func (*Packet) MarshalProtoJSON added in v0.30.0

func (x *Packet) MarshalProtoJSON(s *json.MarshalState)

MarshalProtoJSON marshals the Packet message to JSON.

func (*Packet) MarshalProtoText added in v0.31.4

func (x *Packet) MarshalProtoText() string

func (*Packet) MarshalToSizedBufferVT

func (m *Packet) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*Packet) MarshalToVT

func (m *Packet) MarshalToVT(dAtA []byte) (int, error)

func (*Packet) MarshalVT

func (m *Packet) MarshalVT() (dAtA []byte, err error)

func (*Packet) ProtoMessage

func (*Packet) ProtoMessage()

func (*Packet) Reset

func (x *Packet) Reset()

func (*Packet) SizeVT

func (m *Packet) SizeVT() (n int)

func (*Packet) String

func (x *Packet) String() string

func (*Packet) UnmarshalJSON added in v0.30.1

func (x *Packet) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the Packet from JSON.

func (*Packet) UnmarshalProtoJSON added in v0.30.1

func (x *Packet) UnmarshalProtoJSON(s *json.UnmarshalState)

UnmarshalProtoJSON unmarshals the Packet message from JSON.

func (*Packet) UnmarshalVT

func (m *Packet) UnmarshalVT(dAtA []byte) error

func (*Packet) Validate

func (p *Packet) Validate() error

Validate performs cursory validation of the packet.

type PacketDataHandler added in v0.19.0

type PacketDataHandler = func(data []byte) error

PacketDataHandler handles a packet before it is parsed.

func NewPacketDataHandler added in v0.19.0

func NewPacketDataHandler(handler PacketHandler) PacketDataHandler

NewPacketDataHandler wraps a PacketHandler with a decoding step.

type PacketHandler

type PacketHandler = func(pkt *Packet) error

PacketHandler handles a packet.

pkt is optional (can be nil) if closeErr is set, the stream is closed after pkt.

type PacketReadWriter added in v0.19.0

type PacketReadWriter struct {
	// contains filtered or unexported fields
}

PacketReadWriter reads and writes packets from a io.ReadWriter. Uses a LittleEndian uint32 length prefix.

func NewPacketReadWriter

func NewPacketReadWriter(rw io.ReadWriteCloser) *PacketReadWriter

NewPacketReadWriter constructs a new read/writer.

func (*PacketReadWriter) Close added in v0.19.0

func (r *PacketReadWriter) Close() error

Close closes the packet rw.

func (*PacketReadWriter) ReadPump added in v0.19.0

func (r *PacketReadWriter) ReadPump(cb PacketDataHandler, closed CloseHandler)

ReadPump executes the read pump in a goroutine.

calls the handler when closed or returning an error

func (*PacketReadWriter) ReadToHandler added in v0.19.0

func (r *PacketReadWriter) ReadToHandler(cb PacketDataHandler) error

ReadToHandler reads data to the given handler. Does not handle closing the stream, use ReadPump instead.

func (*PacketReadWriter) Write added in v0.19.0

func (r *PacketReadWriter) Write(p []byte) (n int, err error)

Write writes raw data to the remote.

func (*PacketReadWriter) WritePacket added in v0.19.0

func (r *PacketReadWriter) WritePacket(p *Packet) error

WritePacket writes a packet to the writer.

type PacketWriter added in v0.26.0

type PacketWriter interface {
	// WritePacket writes a packet to the remote.
	WritePacket(p *Packet) error
	// Close closes the writer.
	Close() error
}

PacketWriter is the interface used to write messages to a PacketStream.

func NewPacketWriterWithClose added in v0.27.2

func NewPacketWriterWithClose(prw PacketWriter, close func() error) PacketWriter

NewPacketWriterWithClose wraps a PacketWriter with a close function to call when Close is called.

type Packet_CallCancel added in v0.14.0

type Packet_CallCancel struct {
	// CallCancel cancels the call.
	CallCancel bool `protobuf:"varint,3,opt,name=call_cancel,json=callCancel,proto3,oneof"`
}

func (*Packet_CallCancel) CloneVT added in v0.14.0

func (m *Packet_CallCancel) CloneVT() *Packet_CallCancel

func (*Packet_CallCancel) EqualVT added in v0.14.0

func (this *Packet_CallCancel) EqualVT(thatIface isPacket_Body) bool

func (*Packet_CallCancel) MarshalToSizedBufferVT added in v0.14.0

func (m *Packet_CallCancel) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*Packet_CallCancel) MarshalToVT added in v0.14.0

func (m *Packet_CallCancel) MarshalToVT(dAtA []byte) (int, error)

func (*Packet_CallCancel) SizeVT added in v0.14.0

func (m *Packet_CallCancel) SizeVT() (n int)

type Packet_CallData

type Packet_CallData struct {
	// CallData is a message in a streaming RPC sequence.
	CallData *CallData `protobuf:"bytes,2,opt,name=call_data,json=callData,proto3,oneof"`
}

func (*Packet_CallData) CloneVT added in v0.12.3

func (m *Packet_CallData) CloneVT() *Packet_CallData

func (*Packet_CallData) EqualVT added in v0.10.0

func (this *Packet_CallData) EqualVT(thatIface isPacket_Body) bool

func (*Packet_CallData) MarshalToSizedBufferVT

func (m *Packet_CallData) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*Packet_CallData) MarshalToVT

func (m *Packet_CallData) MarshalToVT(dAtA []byte) (int, error)

func (*Packet_CallData) SizeVT

func (m *Packet_CallData) SizeVT() (n int)

type Packet_CallStart

type Packet_CallStart struct {
	// CallStart initiates a new call.
	CallStart *CallStart `protobuf:"bytes,1,opt,name=call_start,json=callStart,proto3,oneof"`
}

func (*Packet_CallStart) CloneVT added in v0.12.3

func (m *Packet_CallStart) CloneVT() *Packet_CallStart

func (*Packet_CallStart) EqualVT added in v0.10.0

func (this *Packet_CallStart) EqualVT(thatIface isPacket_Body) bool

func (*Packet_CallStart) MarshalToSizedBufferVT

func (m *Packet_CallStart) MarshalToSizedBufferVT(dAtA []byte) (int, error)

func (*Packet_CallStart) MarshalToVT

func (m *Packet_CallStart) MarshalToVT(dAtA []byte) (int, error)

func (*Packet_CallStart) SizeVT

func (m *Packet_CallStart) SizeVT() (n int)

type PrefixClient added in v0.12.0

type PrefixClient struct {
	// contains filtered or unexported fields
}

PrefixClient checks for and strips a set of prefixes from a Client.

func NewPrefixClient added in v0.12.0

func NewPrefixClient(client Client, serviceIDPrefixes []string) *PrefixClient

NewPrefixClient constructs a new PrefixClient.

serviceIDPrefixes is the list of service id prefixes to match. strips the prefix before calling the underlying Invoke function. if none of the prefixes match, returns unimplemented. if empty: forwards all services w/o stripping any prefix.

func (*PrefixClient) ExecCall added in v0.12.0

func (i *PrefixClient) ExecCall(ctx context.Context, service, method string, in, out Message) error

ExecCall executes a request/reply RPC with the remote.

func (*PrefixClient) NewStream added in v0.12.0

func (i *PrefixClient) NewStream(ctx context.Context, service, method string, firstMsg Message) (Stream, error)

NewStream starts a streaming RPC with the remote & returns the stream. firstMsg is optional.

type PrefixInvoker added in v0.11.3

type PrefixInvoker struct {
	// contains filtered or unexported fields
}

PrefixInvoker checks for and strips a set of prefixes from a Invoker.

func NewPrefixInvoker added in v0.11.3

func NewPrefixInvoker(inv Invoker, serviceIDPrefixes []string) *PrefixInvoker

NewPrefixInvoker constructs a new PrefixInvoker.

serviceIDPrefixes is the list of service id prefixes to match. strips the prefix before calling the underlying Invoke function. if none of the prefixes match, returns unimplemented. if empty: forwards all services w/o stripping any prefix.

func (*PrefixInvoker) InvokeMethod added in v0.11.3

func (i *PrefixInvoker) InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)

InvokeMethod invokes the method matching the service & method ID. Returns false, nil if not found. If service string is empty, ignore it.

type RawMessage added in v0.15.2

type RawMessage struct {
	// contains filtered or unexported fields
}

RawMessage is a raw protobuf message container.

The empty value is valid with copy=false.

func NewRawMessage added in v0.15.2

func NewRawMessage(data []byte, copy bool) *RawMessage

NewRawMessage constructs a new raw message. If copy=true, copies data in MarshalVT. Note: the data buffer will be retained and used. The data buffer will be written to and/or replaced in UnmarshalVT.

func (*RawMessage) Clear added in v0.19.0

func (m *RawMessage) Clear()

Clear sets the length of the data buffer to 0 without releasing it.

func (*RawMessage) GetData added in v0.15.2

func (m *RawMessage) GetData() []byte

GetData returns the data buffer without copying.

func (*RawMessage) MarshalVT added in v0.15.2

func (m *RawMessage) MarshalVT() ([]byte, error)

func (*RawMessage) Reset added in v0.19.0

func (m *RawMessage) Reset()

Reset releases the data buffer.

func (*RawMessage) SetData added in v0.15.2

func (m *RawMessage) SetData(data []byte)

SetData sets the data buffer. if copy=true, copies the data to the internal slice. otherwise retains the buffer.

func (*RawMessage) UnmarshalVT added in v0.15.2

func (m *RawMessage) UnmarshalVT(data []byte) error

type RwcConn added in v0.10.8

type RwcConn struct {
	// contains filtered or unexported fields
}

RwcConn implements a Conn with a buffered ReadWriteCloser.

func NewRwcConn added in v0.10.8

func NewRwcConn(
	ctx context.Context,
	rwc io.ReadWriteCloser,
	laddr, raddr net.Addr,
	bufferPacketN int,
) *RwcConn

NewRwcConn constructs a new packet conn and starts the rx pump.

func (*RwcConn) Close added in v0.10.8

func (p *RwcConn) Close() error

Close closes the connection. Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.

func (*RwcConn) LocalAddr added in v0.10.8

func (p *RwcConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*RwcConn) Read added in v0.10.8

func (p *RwcConn) Read(b []byte) (n int, err error)

Read reads data from the connection. Read can be made to time out and return an error after a fixed time limit; see SetDeadline and SetReadDeadline.

func (*RwcConn) RemoteAddr added in v0.10.8

func (p *RwcConn) RemoteAddr() net.Addr

RemoteAddr returns the bound remote network address.

func (*RwcConn) SetDeadline added in v0.10.8

func (p *RwcConn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

A deadline is an absolute time after which I/O operations fail instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to Read or Write. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.

If the deadline is exceeded a call to Read or Write or to other I/O methods will return an error that wraps os.ErrDeadlineExceeded. This can be tested using errors.Is(err, os.ErrDeadlineExceeded). The error's Timeout method will return true, but note that there are other possible errors for which the Timeout method will return true even if the deadline has not been exceeded.

An idle timeout can be implemented by repeatedly extending the deadline after successful ReadFrom or WriteTo calls.

A zero value for t means I/O operations will not time out.

func (*RwcConn) SetReadDeadline added in v0.10.8

func (p *RwcConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future ReadFrom calls and any currently-blocked ReadFrom call. A zero value for t means ReadFrom will not time out.

func (*RwcConn) SetWriteDeadline added in v0.10.8

func (p *RwcConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future WriteTo calls and any currently-blocked WriteTo call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means WriteTo will not time out.

func (*RwcConn) Write added in v0.10.8

func (p *RwcConn) Write(pkt []byte) (n int, err error)

Write writes data to the connection.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server handles incoming RPC streams with a mux.

func NewServer

func NewServer(invoker Invoker) *Server

NewServer constructs a new SRPC server.

func (*Server) AcceptMuxedConn added in v0.3.5

func (s *Server) AcceptMuxedConn(ctx context.Context, mc network.MuxedConn) error

AcceptMuxedConn runs a loop which calls Accept on a muxer to handle streams.

Starts HandleStream in a separate goroutine to handle the stream. Returns context.Canceled or io.EOF when the loop is complete / closed.

func (*Server) GetInvoker added in v0.10.7

func (s *Server) GetInvoker() Invoker

GetInvoker returns the invoker.

func (*Server) HandleStream added in v0.3.5

func (s *Server) HandleStream(ctx context.Context, rwc io.ReadWriteCloser)

HandleStream handles an incoming stream and runs the read loop. Uses length-prefixed packets.

type ServerRPC

type ServerRPC struct {
	// contains filtered or unexported fields
}

ServerRPC represents the server side of an on-going RPC call message stream.

func NewServerRPC

func NewServerRPC(ctx context.Context, invoker Invoker, writer PacketWriter) *ServerRPC

NewServerRPC constructs a new ServerRPC session. note: call SetWriter before handling any incoming messages.

func (*ServerRPC) Context

func (c *ServerRPC) Context() context.Context

Context is canceled when the rpc has finished.

func (*ServerRPC) HandleCallCancel added in v0.15.0

func (c *ServerRPC) HandleCallCancel() error

HandleCallCancel handles the call cancel packet.

func (*ServerRPC) HandleCallData

func (c *ServerRPC) HandleCallData(pkt *CallData) error

HandleCallData handles the call data packet.

func (*ServerRPC) HandleCallStart

func (r *ServerRPC) HandleCallStart(pkt *CallStart) error

HandleCallStart handles the call start packet.

func (*ServerRPC) HandlePacket

func (r *ServerRPC) HandlePacket(msg *Packet) error

HandlePacket handles an incoming parsed message packet.

func (*ServerRPC) HandlePacketData added in v0.19.0

func (r *ServerRPC) HandlePacketData(data []byte) error

HandlePacketData handles an incoming unparsed message packet.

func (*ServerRPC) HandleStreamClose added in v0.8.0

func (c *ServerRPC) HandleStreamClose(closeErr error)

HandleStreamClose handles the incoming stream closing w/ optional error.

func (*ServerRPC) ReadOne added in v0.15.0

func (c *ServerRPC) ReadOne() ([]byte, error)

ReadOne reads a single message and returns.

returns io.EOF if the stream ended without a packet.

func (*ServerRPC) Wait added in v0.8.0

func (c *ServerRPC) Wait(ctx context.Context) error

Wait waits for the RPC to finish.

func (*ServerRPC) WriteCallData added in v0.15.0

func (c *ServerRPC) WriteCallData(data []byte, complete bool, err error) error

WriteCallData writes a call data packet.

func (*ServerRPC) WriteCancel added in v0.15.3

func (c *ServerRPC) WriteCancel() error

WriteCancel writes a call cancel packet.

type Stream

type Stream interface {
	// Context is canceled when the Stream is no longer valid.
	Context() context.Context

	// MsgSend sends the message to the remote.
	MsgSend(msg Message) error

	// MsgRecv receives an incoming message from the remote.
	// Parses the message into the object at msg.
	MsgRecv(msg Message) error

	// CloseSend signals to the remote that we will no longer send any messages.
	CloseSend() error

	// Close closes the stream for reading and writing.
	Close() error
}

Stream is a handle to an on-going bi-directional or one-directional stream RPC handle.

func NewStreamWithClose added in v0.27.1

func NewStreamWithClose(strm Stream, close func() error) Stream

NewStreamWithClose wraps a Stream with a close function to call when Close is called.

type StreamRecv added in v0.26.2

type StreamRecv[T any] interface {
	Stream
	Recv() (T, error)
	RecvTo(T) error
}

StreamRecv is a stream that can receive typed messages.

T is the response type.

type StreamRwc added in v0.19.0

type StreamRwc struct {
	// Stream is the base stream interface.
	Stream
	// contains filtered or unexported fields
}

StreamRwc implements an io.ReadWriteCloser with a srpc.Stream.

func NewStreamRwc added in v0.19.0

func NewStreamRwc(strm Stream) *StreamRwc

NewStreamRwc constructs a new stream read write closer.

func (*StreamRwc) Read added in v0.19.0

func (s *StreamRwc) Read(p []byte) (n int, err error)

Read reads data from the stream to p. Implements io.Reader.

func (*StreamRwc) Write added in v0.19.0

func (s *StreamRwc) Write(p []byte) (n int, err error)

Write writes data to the stream.

type StreamSend added in v0.26.2

type StreamSend[T any] interface {
	Stream
	Send(T) error
}

StreamSend is a stream that can send typed messages.

T is the outgoing type.

type StreamSendAndClose added in v0.26.2

type StreamSendAndClose[T any] interface {
	StreamSend[T]
	SendAndClose(T) error
}

StreamSendAndClose is a stream that can send typed messages, closing after.

T is the outgoing type.

type VClient added in v0.21.0

type VClient struct {
	// contains filtered or unexported fields
}

VClient implements a verbose SRPC client which can log RPC streams.

func NewVClient added in v0.21.0

func NewVClient(c Client, le *logrus.Entry) *VClient

NewVClient constructs a new verbose client wrapper.

func (*VClient) ExecCall added in v0.21.0

func (c *VClient) ExecCall(ctx context.Context, service, method string, in, out Message) (err error)

ExecCall executes a request/reply RPC with the remote.

func (*VClient) NewStream added in v0.21.0

func (c *VClient) NewStream(ctx context.Context, service, method string, firstMsg Message) (stream Stream, err error)

NewStream starts a streaming RPC with the remote & returns the stream. firstMsg is optional.

type VMux added in v0.21.0

type VMux struct {
	// contains filtered or unexported fields
}

VMux implements a verbose logging wrapper for a Mux.

func NewVMux added in v0.21.0

func NewVMux(mux Mux, le *logrus.Entry, veryVerbose bool) *VMux

NewVMux constructs a verbose logging wrapper for a Mux.

if veryVerbose is set, we also log very chatty logs: HasService, HasServiceMethod, Register

func (*VMux) HasService added in v0.21.0

func (v *VMux) HasService(serviceID string) (has bool)

HasService checks if the service ID exists in the handlers.

func (*VMux) HasServiceMethod added in v0.21.0

func (v *VMux) HasServiceMethod(serviceID, methodID string) (has bool)

HasServiceMethod checks if <service-id, method-id> exists in the handlers.

func (*VMux) InvokeMethod added in v0.21.0

func (v *VMux) InvokeMethod(serviceID, methodID string, strm Stream) (done bool, err error)

InvokeMethod invokes the method matching the service & method ID. Returns false, nil if not found. If service string is empty, ignore it.

func (*VMux) Register added in v0.21.0

func (v *VMux) Register(handler Handler) (err error)

Register registers a new RPC method handler (service).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL