rpc

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RpcRaw = iota
	RpcCall
	RpcAsyncCall

	RpcTimeout = time.Second * 5
)
View Source
const (
	TypeNone = iota
	TypeRunning
	TypeStopped
)

Client state enum

View Source
const (
	TTypeNone = iota
	TTypeRunning
	TTypeStopped

	MaxCheck = 20
)
View Source
const (
	BucketNum = 32
)

Variables

View Source
var (
	ErrTimeout    = errors.New("timeout")
	ErrDisconnect = errors.New("disconnect")
)

Functions

func AcquireTimer

func AcquireTimer(d time.Duration) *time.Timer

func Logger added in v1.0.4

func Logger() *zap.Logger

func NewConn

func NewConn(conn transport.IServerConn)

func NewDefaultLogger added in v1.0.4

func NewDefaultLogger() *zap.Logger

func NewRpcError

func NewRpcError(err error) error

func ReleaseTimer

func ReleaseTimer(t *time.Timer)

func Serve

func Serve(host string, rh RequestHandle) error

func SetInvokeCB

func SetInvokeCB(cb func(ctx any, in []byte, err error) error)

SetInvokeCB Set global asynchronous callback

func SugarLogger added in v1.0.4

func SugarLogger() *zap.SugaredLogger

Types

type AsyncInvoker

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

func (*AsyncInvoker) Invoke

func (i *AsyncInvoker) Invoke(in []byte, err error) error

type Call

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

func (*Call) Done

func (r *Call) Done() <-chan IReply

func (*Call) Id

func (r *Call) Id() uint32

func (*Call) Invoke

func (r *Call) Invoke(in []byte, err error) error

func (*Call) IsAsyncInvoker

func (r *Call) IsAsyncInvoker() bool

func (*Call) Reply

func (r *Call) Reply(in []byte, err error)

func (*Call) Return

func (r *Call) Return()

type Client

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

func (*Client) AsyncCall

func (c *Client) AsyncCall(out []byte, ctx any) error

func (*Client) AsyncCallC

func (c *Client) AsyncCallC(out []byte, ctx any, cb func(ctx any, in []byte, err error) error) error

func (*Client) Call

func (c *Client) Call(ctx context.Context, out []byte) ([]byte, error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Shoot

func (c *Client) Shoot(out []byte) error

type Codec

type Codec struct {
}

type Conn

type Conn struct {
	Codec
	// contains filtered or unexported fields
}

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) Send

func (c *Conn) Send(seq uint32, category int8, out []byte) error

type Decoder

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

func NewDecoder

func NewDecoder() *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(in packet.IPacket) error

func (*Decoder) Return

func (d *Decoder) Return()

type DialOption added in v1.0.2

type DialOption struct {
	DisconnectHandler func(nodeId string)
}

type IAsyncInvoker

type IAsyncInvoker interface {
	Invoke(in []byte, err error) error
}

func NewAsyncInvoker

func NewAsyncInvoker(ctx any, cb func(ctx any, in []byte, err error) error) IAsyncInvoker

type ICall

type ICall interface {
	Id() uint32
	Return()
	Reply(in []byte, err error)
	IsAsyncInvoker() bool
	Invoke(in []byte, err error) error
	Done() <-chan IReply
}

func NewCall

func NewCall(seq uint32) ICall

func NewCallWithInvoker

func NewCallWithInvoker(seq uint32, _invoker IAsyncInvoker) ICall

type IClient

type IClient interface {
	//Shoot is a one-way communication, the sender does not pay attention to the receiver's reply
	Shoot(out []byte) error

	// Call performs a unary RPC and returns after the response is received
	// into replyMsg.
	// Support users to use context to cancel blocking status or perform timeout operations
	Call(ctx context.Context, out []byte) ([]byte, error)

	// AsyncCall the safe way to handle asynchronous callbacks is to package the context, replyMsg, and err
	// into a message task and send it to the working goroutine to process the message linearly
	AsyncCall(out []byte, ctx any) error

	// AsyncCallC the functionality and precautions of AsyncCallC are similar to AsyncCall.
	// The difference is that AsyncCallC no need to set a global asynchronous callback.
	AsyncCallC(out []byte, ctx any, cb func(ctx any, in []byte, err error) error) error

	// Close will close all transport links, causes all subsequent requests to fail,
	// All Call or asynchronous Call requests in the waiting queue will return and receive the error ErrDisconnect 'rpc err: disconnect'
	// Close can be called repeatedly
	Close()
}

IClient defines the functions clients need to perform unary and streaming RPCs Support two modes

	1: Call
 	2: Asynchronous Call
	3: Shoot

When the transport connection is disconnected, all Call or asynchronous Call requests in the waiting queue will return and receive the error 'rpc err: disconnect'

func Dial added in v1.0.2

func Dial(id, remoteId, addr string, ops ...*DialOption) (IClient, error)

type IReply

type IReply interface {
	In() ([]byte, error)
	Return()
}

type IRequest

type IRequest interface {
	NewReader() packet.IPacket
	Data() []byte
	Category() int8
	Response(out []byte) error
	IgnoreRsp()

	// Return To prevent request resource memory leaks,
	// you need to explicitly call the Return method to release it.
	// cannot call Response again later
	Return()
}

IRequest To avoid IRequest resource leakage, IRequest requires the receiver to call Return() to return it to the pool

func NewRequest

func NewRequest(session ISession, in packet.IPacket) (IRequest, error)

type ISession

type ISession interface {
	Send(seq uint32, category int8, out []byte) error
}

type Item

type Item[K comparable, V any] struct {
	// contains filtered or unexported fields
}

type Pending

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

func (*Pending) Init

func (p *Pending) Init(cli *Client, _timeout time.Duration)

func (*Pending) OnClose

func (p *Pending) OnClose()

func (*Pending) Pop

func (p *Pending) Pop(id uint32) (ICall, bool)

func (*Pending) Push

func (p *Pending) Push(call ICall)

func (*Pending) RangeAll

func (p *Pending) RangeAll(iter func(id uint32))

type Reply

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

func (*Reply) In

func (r *Reply) In() ([]byte, error)

func (*Reply) Return

func (r *Reply) Return()

type Request

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

func (*Request) Category

func (r *Request) Category() int8

func (*Request) Data added in v1.0.4

func (r *Request) Data() []byte

func (*Request) IgnoreRsp

func (r *Request) IgnoreRsp()

func (*Request) NewReader

func (r *Request) NewReader() packet.IPacket

func (*Request) Response

func (r *Request) Response(out []byte) error

func (*Request) Return

func (r *Request) Return()

type RequestHandle

type RequestHandle func(req IRequest) error

RequestHandle To avoid IRequest resource leakage, IRequest requires the receiver to actively call Return to return it to the pool

type RingBuffer

type RingBuffer[v any] struct {
}

type Timeout

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

func NewTimeoutMgr

func NewTimeoutMgr(cb func([]uint32)) *Timeout

func (*Timeout) OnClose

func (t *Timeout) OnClose()

func (*Timeout) Push

func (t *Timeout) Push(id uint32, ttl time.Duration)

func (*Timeout) Remove

func (t *Timeout) Remove(id uint32)

Jump to

Keyboard shortcuts

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