dogrpc

package
v1.7.20 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultConcurrency     = 8 * 1024
	DefaultRequestTimeout  = 20 * time.Second
	DefaultPendingMessages = 32 * 1024
	DefaultFlushDelay      = -1
	DefaultBufferSize      = 64 * 1024
	DefaultDialRetryTime   = 0
	DefaultConnectNumbers  = 1
)
View Source
const (
	HeaderLen = 24
	Version   = 1
	Padding   = 0
	SOH       = 0x10
	EOH       = 0x24
)
View Source
const (
	DefaultDialNetWork = "tcp"
)

Variables

View Source
var (
	TimeOutError        = derror.SetCodeType(10001, "timeout error.")
	OverflowError       = derror.SetCodeType(10002, "overflow error.")
	InternalServerError = derror.SetCodeType(10003, "interval server error.")
	InvalidParam        = derror.SetCodeType(10004, "invalid param")
)

Functions

func Return

func Return(code uint32, message string, err error, result interface{}) (resp []byte)

func Use added in v1.7.12

func Use(filters []Filter)

Types

type AsyncResult

type AsyncResult struct {
	Response Packet
	Error    error
	Done     chan struct{}
	Request  Packet
	// contains filtered or unexported fields
}

func (*AsyncResult) Cancel

func (m *AsyncResult) Cancel()

type Client

type Client struct {
	Addr            string
	Conns           int
	Dial            DialFunc
	DialRetryTime   int
	PendingRequests int
	FlushDelay      time.Duration
	RequestTimeout  time.Duration
	SendBufferSize  int
	RecvBufferSize  int

	Encoder MessageEncoderFunc
	Decoder MessageDecoderFunc
	// contains filtered or unexported fields
}

func (*Client) Call

func (c *Client) Call(req Packet) (rsp Packet, err *dogError.CodeError)

func (*Client) CallAsync

func (c *Client) CallAsync(req Packet, skipResponse bool) (*AsyncResult, *dogError.CodeError)

func (*Client) CallRetry

func (c *Client) CallRetry(req Packet, retryNum uint32) (rsp Packet, err *dogError.CodeError)

func (*Client) CallTimeout

func (c *Client) CallTimeout(req Packet, timeout time.Duration, retryNum uint32) (rsp Packet, err *dogError.CodeError)

func (*Client) SendUDP

func (c *Client) SendUDP(req Packet) (err *dogError.CodeError)

skip response

func (*Client) Start

func (c *Client) Start()

func (*Client) Stop

func (c *Client) Stop()

type Context

type Context struct {
	ClientAddr string
	Seq        uint32
	Method     string
	Handler    RpcHandlerFunc
	Req        []byte
}

type DialFunc

type DialFunc func(addr string) (conn io.ReadWriteCloser, err error)

type DogPacket

type DogPacket struct {
	Header
	Body []byte
}

func NewDogPacket

func NewDogPacket(cmd uint32, body []byte) *DogPacket

func NewDogPacketWithRet

func NewDogPacketWithRet(cmd uint32, body []byte, seq uint32, ret uint32) *DogPacket

func NewDogPacketWithSeq

func NewDogPacketWithSeq(cmd uint32, body []byte, seq uint32) *DogPacket

func (*DogPacket) ID

func (p *DogPacket) ID() uint32

func (*DogPacket) SetErrCode

func (p *DogPacket) SetErrCode(code uint32)

type DogPacketDecoder

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

func (*DogPacketDecoder) Decode

func (d *DogPacketDecoder) Decode() (Packet, error)

type DogPacketEncoder

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

func (*DogPacketEncoder) Encode

func (e *DogPacketEncoder) Encode(p Packet) error

func (*DogPacketEncoder) Flush

func (e *DogPacketEncoder) Flush() error

type Filter

type Filter interface {
	SetNext(filter Filter)
	Handle(cxt *Context) (uint32, []byte)
}

type Filters

type Filters struct {
	Filters []Filter
}

func (*Filters) Handle

func (f *Filters) Handle(ctx *Context) (uint32, []byte)

type GlFilter

type GlFilter struct {
	IfLogAll          bool
	SlowCostThreshold int
	// contains filtered or unexported fields
}

example: gl filter

func (*GlFilter) Handle

func (f *GlFilter) Handle(ctx *Context) (code uint32, rsp []byte)

func (*GlFilter) SetNext

func (f *GlFilter) SetNext(filter Filter)

type HandlerFunc

type HandlerFunc func(clientAddr string, req Packet) (rsp Packet)
type Header struct {
	PacketLen uint32
	Seq       uint32
	Cmd       uint32
	CheckSum  uint32
	ErrCode   uint32
	Version   uint8
	Padding   uint8
	SOH       uint8
	EOH       uint8
}

type Listener

type Listener interface {
	Init(addr string) error
	Accept() (conn io.ReadWriteCloser, clientAddr string, err error)
	Close() error
	ListenAddr() net.Addr
}

type LogFilter

type LogFilter struct {
	IfLogAll          bool
	SlowCostThreshold int
	// contains filtered or unexported fields
}

example: log filter

func (*LogFilter) Handle

func (f *LogFilter) Handle(ctx *Context) (code uint32, rsp []byte)

func (*LogFilter) SetNext

func (f *LogFilter) SetNext(filter Filter)

type MessageDecoder

type MessageDecoder interface {
	Decode() (Packet, error)
}

type MessageDecoderFunc

type MessageDecoderFunc func(r io.Reader, bufferSize int) (decoder MessageDecoder, err error)

type MessageEncoder

type MessageEncoder interface {
	Encode(msg Packet) error
	Flush() error
}

type MessageEncoderFunc

type MessageEncoderFunc func(w io.Writer, bufferSize int) (encoder MessageEncoder, err error)

type Packet

type Packet interface {
	ID() uint32
	SetErrCode(code uint32)
}

type RpcClient

type RpcClient struct {
	Cm map[string]*Client

	Timeout  time.Duration
	RetryNum uint32

	TlsCfg           *tls.Config
	RpcCaPemFile     string
	RpcClientKeyFile string
	RpcClientPemFile string
	// contains filtered or unexported fields
}

func NewClient

func NewClient(timeout time.Duration, retryNum uint32, useTls bool, cfg *tls.Config, ca, serverKey, serverPem string) *RpcClient

func (*RpcClient) AddAddr

func (c *RpcClient) AddAddr(addr string) *RpcClient

add server address

func (*RpcClient) Connect

func (c *RpcClient) Connect() (*Client, error)

connect

func (*RpcClient) DogConnect

func (c *RpcClient) DogConnect() (*Client, error)

dog packet establish connection

func (*RpcClient) DogInvoke

func (c *RpcClient) DogInvoke(cmd uint32, req interface{}, client ...*Client) (code uint32, rsp []byte, err *dogError.CodeError)

dog packet. Invoke rpc call

func (*RpcClient) Invoke

func (c *RpcClient) Invoke(cmd uint32, req []byte, client ...*Client) (code uint32, rsp []byte, err *dogError.CodeError)

Invoke rpc call

func (*RpcClient) Stop

func (c *RpcClient) Stop()

Stop stop client

type RpcHandlerFunc

type RpcHandlerFunc func([]byte) (uint32, []byte)

type RpcPacket

type RpcPacket struct {
	Seq       uint32
	ErrCode   uint32
	Cmd       uint32 // also be a string, for dispatch.
	PacketLen uint32
	Body      []byte
}

func NewRpcPacket

func NewRpcPacket(cmd uint32, body []byte) *RpcPacket

func NewRpcPacketWithRet

func NewRpcPacketWithRet(cmd uint32, body []byte, seq uint32, ret uint32) *RpcPacket

func NewRpcPacketWithSeq

func NewRpcPacketWithSeq(cmd uint32, body []byte, seq uint32) *RpcPacket

func (*RpcPacket) ID

func (p *RpcPacket) ID() uint32

func (*RpcPacket) SetErrCode

func (p *RpcPacket) SetErrCode(code uint32)

type RpcPacketDecoder

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

func (*RpcPacketDecoder) Decode

func (d *RpcPacketDecoder) Decode() (Packet, error)

type RpcPacketEncoder

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

func (*RpcPacketEncoder) Encode

func (e *RpcPacketEncoder) Encode(p Packet) error

func (*RpcPacketEncoder) Flush

func (e *RpcPacketEncoder) Flush() error

type RpcServer

type RpcServer struct {
	Addr int `inject:"rpcHost"`

	UseTls           bool   `inject:"rpcUseTls" canNil:"true"`
	RpcCaPemFile     string `inject:"rpcCaPemFile" canNil:"true"`
	RpcServerKeyFile string `inject:"rpcServerKeyFile" canNil:"true"`
	RpcServerPemFile string `inject:"rpcServerPemFile" canNil:"true"`
	// contains filtered or unexported fields
}

func NewDogRpcServer

func NewDogRpcServer() *RpcServer

func NewRpcServer

func NewRpcServer() *RpcServer

func (*RpcServer) AddDogHandler

func (s *RpcServer) AddDogHandler(headCmd uint32, f interface{})

func (*RpcServer) AddHandler

func (s *RpcServer) AddHandler(headCmd uint32, f RpcHandlerFunc)

func (*RpcServer) Close added in v1.7.2

func (s *RpcServer) Close()

func (*RpcServer) DogRpcRegister

func (s *RpcServer) DogRpcRegister() error

func (*RpcServer) Start added in v1.7.2

func (s *RpcServer) Start() error

type Server

type Server struct {
	Addr             string
	Handler          HandlerFunc
	Concurrency      int
	FlushDelay       time.Duration
	PendingResponses int
	SendBufferSize   int
	RecvBufferSize   int
	Listener         Listener

	Encoder MessageEncoderFunc
	Decoder MessageDecoderFunc
	// contains filtered or unexported fields
}

func (*Server) Serve

func (s *Server) Serve() *dogError.CodeError

func (*Server) Start

func (s *Server) Start() *dogError.CodeError

func (*Server) Stop

func (s *Server) Stop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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