msrpc

package module
v0.0.0-...-d99319c Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

README

用法

服务器初始化

s := msrpc.New("tcp://127.0.0.1:10000")
defer s.Stop()

err = s.Start()

客户端初始化

cli := client.New("tcp://127.0.0.1:10000")
defer cli.Stop()

err = cli.Start()

用法一: Request/Response

server

s.Route("/hi", func(c *msrpc.Context) {
	c.Write([]byte("reply"))
})

client

resp,_ := cli.Request("/hi", []byte("hi"))

fmt.Println(string(resp.Body)) // reply

用法二: Send/Receive

server

s.OnMessage(func(conn gnet.Conn, m *proto.Message) {
    fmt.Println(string(msg.Body)) // hi
})

client

err = cli.Send(&proto.Message{
	MsgType: 1,
	Content: []byte("hi"),
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnIsNil = errors.New("conn is nil")
)

Functions

func GetUidFromContext

func GetUidFromContext(conn gnet.Conn) string

func ParseProtoAddr

func ParseProtoAddr(protoAddr string) (string, string, error)

Types

type ConnManager

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

func NewConnManager

func NewConnManager() *ConnManager

func (*ConnManager) AddConn

func (c *ConnManager) AddConn(uid string, conn gnet.Conn)

func (*ConnManager) GetConn

func (c *ConnManager) GetConn(uid string) gnet.Conn

func (*ConnManager) RemoveConn

func (c *ConnManager) RemoveConn(uid string)

type Context

type Context struct {
	mslog.Log
	// contains filtered or unexported fields
}

func NewContext

func NewContext(conn gnet.Conn) *Context

func (*Context) Body

func (c *Context) Body() []byte

func (*Context) Conn

func (c *Context) Conn() gnet.Conn

func (*Context) ConnReq

func (c *Context) ConnReq() *proto.Connect

func (*Context) Uid

func (c *Context) Uid() string

func (*Context) Write

func (c *Context) Write(data []byte)

func (*Context) WriteConnack

func (c *Context) WriteConnack(connack *proto.Connack)

func (*Context) WriteErr

func (c *Context) WriteErr(err error)

func (*Context) WriteErrorAndStatus

func (c *Context) WriteErrorAndStatus(err error, status proto.Status)

func (*Context) WriteMessage

func (c *Context) WriteMessage(m gproto.Message)

func (*Context) WriteOk

func (c *Context) WriteOk()

func (*Context) WriteStatus

func (c *Context) WriteStatus(status proto.Status)

type Handler

type Handler func(c *Context)

type Option

type Option func(*Options)

func WithAddr

func WithAddr(addr string) Option

func WithClosePath

func WithClosePath(path string) Option

func WithConnPath

func WithConnPath(path string) Option

func WithLogDetailOn

func WithLogDetailOn(on bool) Option

func WithMaxIdle

func WithMaxIdle(maxIdle time.Duration) Option

func WithMessagePoolOn

func WithMessagePoolOn(on bool) Option

func WithMessagePoolSize

func WithMessagePoolSize(size int) Option

func WithOnMessage

func WithOnMessage(onMessage func(conn gnet.Conn, msg *proto.Message)) Option

func WithOnRequest

func WithOnRequest(onRequest func(conn gnet.Conn, req *proto.Request)) Option

func WithOnResponse

func WithOnResponse(onResponse func(conn gnet.Conn, resp *proto.Response)) Option

func WithRequestPoolSize

func WithRequestPoolSize(size int) Option

func WithRequestTimeout

func WithRequestTimeout(timeout time.Duration) Option

func WithTimingWheelSize

func WithTimingWheelSize(size int64) Option

func WithTimingWheelTick

func WithTimingWheelTick(tick time.Duration) Option

type Options

type Options struct {
	Addr            string
	RequestPoolSize int  // The size of the request pool, the default is 10000
	MessagePoolSize int  // The size of the message pool, the default is 10000
	MessagePoolOn   bool // Whether to open the message pool, the default is true
	ConnPath        string
	ClosePath       string
	RequestTimeout  time.Duration
	OnMessage       func(conn gnet.Conn, msg *proto.Message)
	MaxIdle         time.Duration

	TimingWheelTick time.Duration // The time-round training interval must be 1ms or more
	TimingWheelSize int64         // Time wheel size
	OnRequest       func(conn gnet.Conn, req *proto.Request)
	OnResponse      func(conn gnet.Conn, resp *proto.Response)
	LogDetailOn     bool // 是否开启详细日志

}

func NewOptions

func NewOptions() *Options

type RateLimiter

type RateLimiter struct {
	mslog.Log
	// contains filtered or unexported fields
}

RateLimiter is the struct used to keep tracking consumed memory size.

func NewRateLimiter

func NewRateLimiter(max uint64) *RateLimiter

NewRateLimiter creates and returns a rate limiter instance.

func (*RateLimiter) Decrease

func (r *RateLimiter) Decrease(sz uint64)

Decrease decreases the recorded in memory log size by sz bytes.

func (*RateLimiter) Enabled

func (r *RateLimiter) Enabled() bool

Enabled returns a boolean flag indicating whether the rate limiter is enabled.

func (*RateLimiter) Get

func (r *RateLimiter) Get() uint64

Get returns the recorded in memory log size.

func (*RateLimiter) Increase

func (r *RateLimiter) Increase(sz uint64)

Increase increases the recorded in memory log size by sz bytes.

func (*RateLimiter) RateLimited

func (r *RateLimiter) RateLimited() bool

RateLimited returns a boolean flag indicating whether the node is rate limited.

func (*RateLimiter) Set

func (r *RateLimiter) Set(sz uint64)

Set sets the recorded in memory log size to sz bytes.

type Server

type Server struct {
	gnet.BuiltinEventEngine

	mslog.Log

	ConnManager *ConnManager
	// contains filtered or unexported fields
}

func New

func New(addr string, ops ...Option) *Server

func (*Server) MessagePoolRunning

func (s *Server) MessagePoolRunning() int

func (*Server) OnBoot

func (s *Server) OnBoot(eng gnet.Engine) (action gnet.Action)

func (*Server) OnClose

func (s *Server) OnClose(conn gnet.Conn, err error) (action gnet.Action)

func (*Server) OnMessage

func (s *Server) OnMessage(h func(conn gnet.Conn, msg *proto.Message))

func (*Server) OnTick

func (s *Server) OnTick() (delay time.Duration, action gnet.Action)

func (*Server) OnTraffic

func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action)

func (*Server) Options

func (s *Server) Options() *Options

func (*Server) Request

func (s *Server) Request(uid string, p string, body []byte) (*proto.Response, error)

func (*Server) RequestAsync

func (s *Server) RequestAsync(uid string, p string, body []byte) error

func (*Server) RequestPoolRunning

func (s *Server) RequestPoolRunning() int

func (*Server) RequestWithContext

func (s *Server) RequestWithContext(ctx context.Context, uid string, p string, body []byte) (*proto.Response, error)

func (*Server) Route

func (s *Server) Route(p string, h Handler)

func (*Server) Schedule

func (s *Server) Schedule(interval time.Duration, f func()) *timingwheel.Timer

Schedule 延迟任务

func (*Server) Send

func (s *Server) Send(uid string, msg *proto.Message) error

func (*Server) Start

func (s *Server) Start() error

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