mrpc

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderLength     = 17
	MaxPayloadLength = 4 * 1024 * 1024
)
View Source
const (
	CodeOK             int32 = 0
	CodeBadRequest     int32 = 100
	CodeMethodNotFound int32 = 101
	CodeError          int32 = 102
)

Variables

This section is empty.

Functions

func EncodeHeader

func EncodeHeader(h Header) []byte

func EncodeRequest

func EncodeRequest(service, method string, req any) ([]byte, error)

client

func EncodeResponse

func EncodeResponse(code int32, message string, resp any) ([]byte, error)

func WriteFrame

func WriteFrame(w io.Writer, requestID uint64, frameType FrameType, payload []byte) error

Types

type Client

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

unary

func Dial

func Dial(addr string) (*Client, error)

func (*Client) CallUnary

func (c *Client) CallUnary(ctx context.Context, service, method string, req any, resp any) error

func (*Client) Close

func (c *Client) Close() error

func (*Client) NewClientStream

func (c *Client) NewClientStream(ctx context.Context, service string, method string, req any) (*ClientStream, error)

type ClientPool

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

func NewClientPool

func NewClientPool(cnf *ClientPoolConfig) (*ClientPool, error)

func (*ClientPool) Discard

func (c *ClientPool) Discard(client *Client) error

关闭一个连接

func (*ClientPool) Get

func (c *ClientPool) Get() (*Client, error)

func (*ClientPool) Put

func (c *ClientPool) Put(client *Client) error

放回一个连接

func (*ClientPool) Release

func (c *ClientPool) Release()

释放连接池

type ClientPoolConfig

type ClientPoolConfig struct {
	Host        string
	Port        int
	InitialCap  int
	MaxIdle     int
	MaxCap      int
	IdleTimeout time.Duration
}

type ClientStream

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

stream

func (*ClientStream) Recv

func (s *ClientStream) Recv(resp any) error

type FrameType

type FrameType uint8
const (
	FrameUnaryRequest  FrameType = 1 // 单次请求
	FrameUnaryResponse FrameType = 2 // 单次响应
	//stream
	FrameStreamOpen FrameType = 3
	FrameStreamData FrameType = 4
	FrameStreamEnd  FrameType = 5

	FrameCancel FrameType = 6
)
type Header struct {
	CRC32         uint32
	RequestID     uint64
	PayloadLength uint32
	FrameType     FrameType
}

func DecodeHeader

func DecodeHeader(data []byte) (Header, error)

func ReadFrame

func ReadFrame(r io.Reader) (Header, []byte, error)

type MethodHandler

type MethodHandler struct {
	UnaryHandler  UnaryFunc
	StreamHandler StreamFunc
}

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) LookupStream

func (r *Registry) LookupStream(service, method string) (StreamFunc, bool)

func (*Registry) LookupUnary

func (r *Registry) LookupUnary(service, method string) (UnaryFunc, bool)

func (*Registry) Register

func (r *Registry) Register(service string, method string, handler MethodHandler) error

type Request

type Request struct {
	Service string          `json:"service"`
	Method  string          `json:"method"`
	Request json.RawMessage `json:"request"`
}

func DecodeRequest

func DecodeRequest(data []byte) (*Request, error)

server

type Response

type Response struct {
	Code     int32           `json:"code"`
	Message  string          `json:"message"`
	Response json.RawMessage `json:"response"`
}

func DecodeResponse

func DecodeResponse(data []byte) (*Response, error)

type Server

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

func NewServer

func NewServer(registry *Registry) *Server

func (*Server) Listen

func (s *Server) Listen(ctx context.Context, addr string) error

type StreamFunc

type StreamFunc func(ctx context.Context, req json.RawMessage, stream *StreamServer) error

type StreamServer

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

func (*StreamServer) Send

func (stream *StreamServer) Send(resp any) error

stream send

type UnaryFunc

type UnaryFunc func(ctx context.Context, req json.RawMessage) (any, error)

Jump to

Keyboard shortcuts

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