crpc

package module
v2.0.17 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 25 Imported by: 1

README

crpc

中心服务的rpc,采用注册机制

数据类型支持
  1. coder.JSON

number,string,bool,slice, map *point 测试通过

  1. coder.MsgPack

number,string,bool,slice, map *point 测试通过

  1. coder.Msgp

当没有代码自动生成的时候(尚未实现其协议的)=>自动使用2.coder.MsgPack序列化

这里就需要注意其兼容性了,struct,只有*struct才是实现了其协议的,否则均会降级使用2.coder.MscPack mark: struct 使用2序列化.将字节码又通过3来反解就会有些不兼容

number,string,bool,slice, map *point 测试通过 map只支持string作为key

自定义解码器的时候优先要测试需要支持的类型
数据压缩

Raw,Snappy 测试通过

usage

server.go

	crpc.NewServer().Listen(":8080")

client1.go

client1 := crpc.Dial("client1", "127.0.0.1:8080", crpc.Options().SetHeartInterval(-1))
time.Sleep(2e9)                          //保证其链接上,正式使用,不需要
//one call
var r1 string
err = client.Call("client2", "func.Hello1", p, &r1, crpc.Options().SetReqCoderT(coder.Msgp).SetResCoderT(coder.Msgp))

client2.go

client = crpc.Dial("client2", "127.0.0.1:8080", crpc.Options().SetHeartInterval(-1))
//Mark: 注册方法
client2.RegisterName("crpc", new(msg))
client.RegisterFunc("Hello", Hello)
client.RegisterFunc("Hello1", Hello1)
client.RegisterFunc("Hello2", Hello2)
client.RegisterFunc("Hello3", Hello3)

方法类型支持
func (m *msg) Hello(req dto.Person) error {
	return nil
}

func (m *msg) Hello1(req dto.Person) (string, error) {
	return "World", nil
}

func (m *msg) Hello3(meta dto.Meta, req *dto.Person) error {
	return nil
}

func (m *msg) Hello2(meta dto.Meta, req *dto.Person) (*dto.Person, error) {
	return &dto.Person{Name: "World"}, nil
}

//同时支持函数
func Hello(req dto.Person) error {
	return nil
}

func Hello1(req dto.Person) (string, error) {
	return "World", nil
}

func Hello3(meta dto.Meta, req *dto.Person) error {
	return nil
}

func Hello2(meta dto.Meta, req *dto.Person) (*dto.Person, error) {
	return &dto.Person{Name: "World"}, nil
}

文件发送

维护好chunksize就可以支持断点续传 client1.go

	f, err := os.Open("./cc.mp4")
	if err != nil {
		panic(err)
	}
	now := time.Now()
	if err := client.SendFile("client2", "crpc.SaveFile", "./ccc/ccc.mp5", f); err != nil {
		fmt.Println(err)
	}

client2.go

//文件保存
func (m *msg) SaveFile(req *cdto.FileBody) error {
	return crpc.WriteFile(req)
}
Benchmark

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	VerifyError             = errors.New("Client VerifyError")
	ReadError               = errors.New("Client ReadError")
	ServerReadError         = errors.New("Server ReadError")
	UnzipError              = errors.New("Client UnzipError")
	WriteError              = errors.New("Client WriteError")
	ModuleFuncError         = errors.New("Client ModuleFunc must like rpc.func")
	ServerError             = errors.New("ServerError")
	FuncError               = errors.New("FuncError")
	ReqTimeOutError         = errors.New("ReqTimeoutError")
	ErrCoderRawBodyMustData = errors.New("CoderRawBodyMustData")
	ErrCusstomNoReceiveType = errors.New("ErrCusstomNoReceiveType")
)

Functions

func NewServer

func NewServer(opts ...*option_server) *server

func OptionServer added in v2.0.1

func OptionServer() *option_server

func RegistCustomError added in v2.0.5

func RegistCustomError(rt reflect.Type)

func WriteFile added in v2.0.4

func WriteFile(req *dto.FileBody) (err error)

Types

type Call

type Call struct {
	Service string
	Module  string
	Method  string
	Req     any
	Ret     any
	Err     error

	Done chan *Call
	// contains filtered or unexported fields
}

type Client

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

1. service - func_module -> anonymity_func 1. service - module -> func

func Dial

func Dial(name, url string, opts ...*Option) *Client

func (*Client) Call

func (this *Client) Call(server string, moduleFunc string, req, ret any, opts ...*Option) error

对外的方法 sync

func (*Client) Go

func (this *Client) Go(server string, moduleFunc string, req, ret any, opts ...*Option) *Call

async

func (*Client) Register

func (this *Client) Register(rcvr any) error

copy from official rpc

func (*Client) RegisterFunc added in v2.0.1

func (this *Client) RegisterFunc(funcname string, function any) error

func (*Client) RegisterName

func (this *Client) RegisterName(name string, rcvr any) error
	func (p *Person) Func(meta int, req int) (res int, err error)
	func (p *Person) Func1(meta int, req int) (err error)
	func (p *Person) Func2(req int) (res int, err error)
 func (p *Person) Func3(req int) (err error)

支持以上4种结构

func (*Client) Send

func (this *Client) Send(server, moduleFunc string, v any, opts ...*Option) error

send msg 就是类似于MQ

func (*Client) SendFile

func (this *Client) SendFile(server string, moduleFunc string, save_path string, reader io.Reader, opts ...*Option) (err error)

type Option added in v2.0.1

type Option struct {
	Meta any //放在header中的透传信息
	//client
	MetaCoderT    *coder.T       //meta数据的编解码器
	ReqCoderT     *coder.T       //请求数据的编解码器
	ResCoderT     *coder.T       //响应数据的编解码器,还是自定义错误的解码器,之所以请求与返回需要不同的编解码,是有文件上传的场景,上传有个自定义的编码方式
	CompressT     *compressor.T  //压缩数据的编解码器
	Timeout       *time.Duration //这个发送的超时时间,版本1是中心超市,现在做客户端超时
	CheckInterval *time.Duration //检测是否连接的间隔
	HeartInterval *time.Duration //心跳间隔,负数默认不开启心跳检测
	ChunksSize    *int           //发送文件时,每次分片文件大小
	RetErr        error          //返回一个自定义的错误
	Weight        *int           //权重 ,负数不参只保留一个链接,且绝对值越大,权重越大
	ReadDeadline  *time.Duration
	WriteDeadline *time.Duration
	MaxCacheSize  *int //sendchan 的容量
	//server
	Secret *string
}

func Options added in v2.0.2

func Options() *Option

func (*Option) Merge added in v2.0.2

func (this *Option) Merge(opts ...*Option) *Option

func (*Option) RegistRetErr added in v2.0.2

func (this *Option) RegistRetErr(r error) *Option

func (*Option) SetCheckInterval added in v2.0.2

func (this *Option) SetCheckInterval(t time.Duration) *Option

func (*Option) SetChunksMaxSize added in v2.0.2

func (this *Option) SetChunksMaxSize(t int) *Option

func (*Option) SetCoderT added in v2.0.2

func (this *Option) SetCoderT(t coder.T) *Option

func (*Option) SetCompressT added in v2.0.2

func (this *Option) SetCompressT(t compressor.T) *Option

func (*Option) SetHeartInterval added in v2.0.2

func (this *Option) SetHeartInterval(t time.Duration) *Option

< 0 将没有心跳

func (*Option) SetMaxCacheSize added in v2.0.9

func (this *Option) SetMaxCacheSize(t int) *Option

func (*Option) SetMetaCoderT added in v2.0.2

func (this *Option) SetMetaCoderT(t coder.T) *Option

func (*Option) SetMetaData added in v2.0.2

func (this *Option) SetMetaData(meta any) *Option

func (*Option) SetReadDeadline added in v2.0.9

func (this *Option) SetReadDeadline(t time.Duration) *Option

func (*Option) SetReqCoderT added in v2.0.2

func (this *Option) SetReqCoderT(t coder.T) *Option

func (*Option) SetResCoderT added in v2.0.2

func (this *Option) SetResCoderT(t coder.T) *Option

func (*Option) SetSecret added in v2.0.2

func (this *Option) SetSecret(s string) *Option

func (*Option) SetTimeout added in v2.0.2

func (this *Option) SetTimeout(t time.Duration) *Option

func (*Option) SetWeight added in v2.0.2

func (this *Option) SetWeight(t int) *Option

func (*Option) SetWriteDeadline added in v2.0.9

func (this *Option) SetWriteDeadline(t time.Duration) *Option

Directories

Path Synopsis
cmd
gencrpc command
copy
copy
目前这个性能最好,不在乎其内部结构是否缺少 msgp 是msgpack的代码生成实现 vmihailenco/msgpack 是msgpack的非代码实现,这2个玩意儿是大体兼容的,在一些特殊处理下还是有区别:var a []string = nil,就无法混用 大体上,之前遇到的不兼容的地方,都是编码使用msgpack,解码又用的是msgp
目前这个性能最好,不在乎其内部结构是否缺少 msgp 是msgpack的代码生成实现 vmihailenco/msgpack 是msgpack的非代码实现,这2个玩意儿是大体兼容的,在一些特殊处理下还是有区别:var a []string = nil,就无法混用 大体上,之前遇到的不兼容的地方,都是编码使用msgpack,解码又用的是msgp

Jump to

Keyboard shortcuts

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