minirpc

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: MIT Imports: 21 Imported by: 0

README

minirpc

一个简单的rpc

之前写了一个rpc框架,由于是第一次写,所以当时想把限流,熔断,服务注册与发现等功能都加进去,当时并没有太关注性能,而且代码很多很乱

所以新写一个minirpc,此项目不会有太多的功能,只有最基础的功能,参考 go-kratos 可以知道,我们完全可以基于一个简单的rpc框架扩展成一个很好用的框架

压测

既然此框架注重性能,那么要做好压测,至于怎么做压测,可以看我之前写的文章:压测

至于此项目如何做压测,我们直接fork鸟窝老师的压测仓库,模仿他的代码,为自己的rpc框架增加上压测代码

常用优化

我们基于tcp协议,可以很简单的实现一个支持长连接的rpc。

  1. 尽可能的使用对象池,减少内存分配,垃圾回收压力
    1. 除了对象,还有[]byte缓冲区
    2. 可以参考 fasthttp,此框架就是把对象复用做到极致了,性能才能那么好
  2. 使用goroutine pool,有挺多线程的库,复用goroutine
  3. 使用bufio,虽然会增加内存的使用,但是大部分情况下会减少系统调用,弊大于利
  4. 考虑场景: 长连接 or 短链接 or 连接多路复用。针对场景做特殊优化
    1. 比如说两个微服务之间调用,可能只需要建立少数的长连接就够用了,即便该服务被好几百个服务调用,几千条连接也够用了
    2. 如果是网关等需要建立大量长连接的场景,而且大部分都是空闲连接的话,std net就不合适了,可以使用 netpoll 代替std net
  5. pprof
    1. go tool pprof --alloc_objects your-program mem.pprof
  6. 可以但没必要
    1. 其实还可以将数据压缩后传送,然后再解压,消耗更多的cpu但是传输更快了

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrShutdown         = errors.New("connection is shut down")
	ErrUnsupportedCodec = errors.New("unsupported Codec")
)

Functions

This section is empty.

Types

type ByteCodec

type ByteCodec struct{}

ByteCodec uses raw slice pf bytes and don't encode/decode.

func (ByteCodec) Decode

func (c ByteCodec) Decode(data []byte, i interface{}) error

Decode returns raw slice of bytes.

func (ByteCodec) Encode

func (c ByteCodec) Encode(i interface{}) ([]byte, error)

Encode returns raw slice of bytes.

type Call

type Call struct {
	ServicePath   string
	ServiceMethod string
	Args          any
	Reply         any
	Error         error
	Done          chan *Call
	Seq           uint64
}

type Client

type Client interface {
	// Call 同步调用
	Call(path, method string, args any, reply any) error
	// Go 异步调用
	Go(path, method string, args any, reply any, done chan *Call) *Call
	Close() error
}

func NewClient

func NewClient(addr string, opts ...ClientOption) (Client, error)

type ClientOption

type ClientOption func(*ClientOptions)

func WithSerializeType

func WithSerializeType(serializeType protocol.SerializeType) ClientOption

type ClientOptions

type ClientOptions struct {
	SerializeType protocol.SerializeType
}

type Codec

type Codec interface {
	Encode(i interface{}) ([]byte, error)
	Decode(data []byte, i interface{}) error
}

Codec defines the interface that decode/encode payload.

type Connection

type Connection struct {
	net.Conn

	sync.Once
	// contains filtered or unexported fields
}

func (*Connection) Close added in v0.4.0

func (c *Connection) Close()

func (*Connection) ReadMessage

func (c *Connection) ReadMessage(ctx context.Context) (*protocol.Message, error)

func (*Connection) WriteMessage

func (c *Connection) WriteMessage(ctx context.Context, message *protocol.Message) error

type JSONCodec

type JSONCodec struct{}

JSONCodec uses json marshaler and unmarshaler.

func (JSONCodec) Decode

func (c JSONCodec) Decode(data []byte, i interface{}) error

Decode decodes an object from slice of bytes.

func (JSONCodec) Encode

func (c JSONCodec) Encode(i interface{}) ([]byte, error)

Encode encodes an object into slice of bytes.

type MsgpackCodec

type MsgpackCodec struct{}

MsgpackCodec uses messagepack marshaller and unmarshaler.

func (MsgpackCodec) Decode

func (c MsgpackCodec) Decode(data []byte, i interface{}) error

Decode decodes an object from slice of bytes.

func (MsgpackCodec) Encode

func (c MsgpackCodec) Encode(i interface{}) ([]byte, error)

Encode encodes an object into slice of bytes.

type PBCodec

type PBCodec struct{}

PBCodec uses protobuf marshaler and unmarshaler.

func (PBCodec) Decode

func (c PBCodec) Decode(data []byte, i interface{}) error

Decode decodes an object from slice of bytes.

func (PBCodec) Encode

func (c PBCodec) Encode(i interface{}) ([]byte, error)

Encode encodes an object into slice of bytes.

type Reset

type Reset interface {
	Reset()
}

Reset defines Reset method for pooled object.

type Server

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

func NewServer

func NewServer(addr string, opts ...ServerOption) *Server

func (*Server) Register

func (server *Server) Register(rcvr any) error

func (*Server) RegisterName

func (server *Server) RegisterName(name string, rcvr any) error

RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.

func (*Server) Start

func (server *Server) Start() error

type ServerError

type ServerError string

ServerError represents an error that has been returned from the remote side of the RPC connection.

func (ServerError) Error

func (e ServerError) Error() string

type ServerOption

type ServerOption func(*ServerOptions)

func WithTimeout

func WithTimeout(timeout time.Duration) ServerOption

type ServerOptions

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

Directories

Path Synopsis
pb_example
proto
Package proto is a generated protocol buffer package.
Package proto is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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