littlerpc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2022 License: MIT Imports: 21 Imported by: 0

README

LittleRpc Go Report Card Ci codecov Go Version GitHub

高性能,跨语言的玩具级RPC实现

Project TODO

功能 支持程度 目前支持的功能 完善
代理对象代码生成器 勉强可用 生成符合定义API规范的代理对象
稳定的发布版本 API随时变动 V0.10还未发布..
完善的BenchMark 现阶段无
任务执行池 V0.10发布时添加
Java-Client 稳定版本发布之前 不支持
JavaScript-Client 稳定版本发布之前 不支持
统一可定制的日志 V0.20发布前添加 不支持
负载均衡组件 V0.20发布前添加 不支持

Quick-Start

假设有以下服务需要被使用

type Hello int

func (receiver Hello) Hello(s string) int {
	fmt.Println(s)
	return 1 << 20
}

以下代码启动一个服务器并声明可以被客户端调用的过程,需要注意的是hello之类在go中被识别为不可导出的过程,这些过程并不会被littlerpc注册。

server := littlerpc.NewServer(littlerpc.WithAddressServer(":1234"))
err := server.Elem(new(Hello))
if err != nil {
    panic(err)
}
err = server.Start()
if err != nil {
    panic(err)
}
clientInfo := new(Hello)
client := littlerpc.NewClient(littlerpc.WithAddressClient(":1234"))
_ = client.BindFunc(clientInfo)
rep, _ := client.Call("Hello", "hello world!")
fmt.Println(rep[0])

OutPut

hello world!
1048576

Examples

过程的定义

littlerpc中一个合法的过程是如下那样,必须有一个接收器,参数不能是指针类型,返回结果集允许指针/非指针类型,error可以返回或者不返回

func(receiver Type) FuncName(arg1,arg2...) (result1,result2,error/noerror...) {}
更多的例子
代码生成器

在编写每个客户端的代理对象时有很多繁琐的动作需要人工去完成,所以为了减轻这些不必要的工作,我提供了一个简易实现的代码生成器,生成一个代理对象并自动生成对应的类型断言代码和自动生成过程。

Install(安装)
go install github.com/nyan233/littlerpc/pxtor
使用

比如有以下对象需要生成

example/littlerpc/proxy/main.go

type FileServer struct {
	fileMap map[string][]byte
}

func NewFileServer() *FileServer {
	return &FileServer{fileMap: make(map[string][]byte)}
}

func (fs *FileServer) SendFile(path string, data []byte) {
	fs.fileMap[path] = data
}

func (fs *FileServer) GetFile(path string) ([]byte, bool) {
	bytes, ok := fs.fileMap[path]
	return bytes, ok
}

func (fs *FileServer) OpenSysFile(path string) ([]byte, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	return ioutil.ReadAll(file)
}
 pxtor -o test_proxy.go -r main.FileServer

生成完之后需要您手动调节一下import,因为生成器无法判断正确的import上下文

example/littlerpc/proxy/Test_proxy.go

/*
	@Generator   : littlerpc-generator
	@CreateTime  : 2022-06-08 01:56:25.797349 +0800 CST m=+0.000784176
	@Author      : littlerpc-generator
*/
package main

import (
	"github.com/nyan233/littlerpc"
)

type FileServerProxy struct {
	*littlerpc.Client
}

func NewFileServerProxy(client *littlerpc.Client) *FileServerProxy {
	proxy := &FileServerProxy{}
	err := client.BindFunc(proxy)
	if err != nil {
		panic(err)
	}
	proxy.Client = client
	return proxy
}

func (proxy FileServerProxy) SendFile(path string, data []byte) {
	_, _ = proxy.Call("SendFile", path, data)
	return
}

func (proxy FileServerProxy) GetFile(path string) ([]byte, bool) {
	inter, _ := proxy.Call("GetFile", path)
	r0 := inter[0].([]byte)
	r1 := inter[1].(bool)
	return r0, r1
}

func (proxy FileServerProxy) OpenSysFile(path string) ([]byte, error) {
	inter, err := proxy.Call("OpenSysFile", path)
	r0 := inter[0].([]byte)
	return r0, err
}

API

NewServer

...

NewClient

...

Lisence

The LittleRpc Use Mit licensed. More is See Lisence

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJsonUnMarshal      = coder.NewError("json unmarshal failed", "")
	ErrMethodNoRegister   = coder.NewError("method no register", "")
	ErrElemTypeNoRegister = coder.NewError("elem type no register : ", "")
	ErrServer             = coder.NewError("server error: ", "")
	ErrCallArgsType       = coder.NewError("call arguments type error : ", "")
	Nil                   = coder.NewError("the error is nil", "")
)

Functions

func HandleError

func HandleError(sp coder.RStackFrame, errNo coder.Error, conn *websocket.Conn, appendInfo string, more ...interface{})

func WithAddressClient

func WithAddressClient(addr string) clientOption

func WithAddressServer

func WithAddressServer(adds ...string) serverOption

func WithCallOnErr

func WithCallOnErr(fn func(err error)) clientOption

func WithCustomLogger

func WithCustomLogger(logger bilog.Logger) serverOption

func WithCustomLoggerClient

func WithCustomLoggerClient(logger bilog.Logger) clientOption

func WithDefaultClient

func WithDefaultClient() clientOption

func WithDefaultServer

func WithDefaultServer() serverOption

func WithTlsClient

func WithTlsClient(tlsC *tls.Config) clientOption

func WithTlsServer

func WithTlsServer(tlsC *tls.Config) serverOption

Types

type Client

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

func NewClient

func NewClient(opts ...clientOption) *Client

func (*Client) BindFunc

func (c *Client) BindFunc(i interface{}) error

func (*Client) Call

func (c *Client) Call(processName string, args ...interface{}) (rep []interface{}, uErr error)

func (*Client) Close

func (c *Client) Close() error

type ClientConfig

type ClientConfig struct {
	TlsConfig         *tls.Config
	ServerAddr        string
	KeepAlive         bool
	Logger            bilog.Logger
	ClientPPTimeout   time.Duration
	ClientConnTimeout time.Duration
	// 客户端Call错误处理的回调函数
	CallOnErr func(err error)
}

type CustomLogger

type CustomLogger string

func (CustomLogger) Debug

func (c CustomLogger) Debug(format string, v ...interface{})

func (CustomLogger) Error

func (c CustomLogger) Error(format string, v ...interface{})

func (CustomLogger) Info

func (c CustomLogger) Info(format string, v ...interface{})

func (CustomLogger) SetLevel

func (c CustomLogger) SetLevel(lvl int)

func (CustomLogger) Warn

func (c CustomLogger) Warn(format string, v ...interface{})

type ElemMeta

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

type Server

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

func NewServer

func NewServer(opts ...serverOption) *Server

func (*Server) Elem

func (s *Server) Elem(i interface{}) error

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop() error

type ServerConfig

type ServerConfig struct {
	TlsConfig       *tls.Config
	Address         []string
	ServerTimeout   time.Duration
	ServerKeepAlive bool
	// ping-pong timeout
	ServerPPTimeout time.Duration
	Logger          bilog.Logger
}

Directories

Path Synopsis
example
littlerpc/multi_instance command
@Generator : littlerpc-generator @CreateTime : 2022-06-10 16:24:21.8771 +0800 CST m=+0.000615892 @Author : littlerpc-generator
@Generator : littlerpc-generator @CreateTime : 2022-06-10 16:24:21.8771 +0800 CST m=+0.000615892 @Author : littlerpc-generator
littlerpc/proxy command
@Generator : littlerpc-generator @CreateTime : 2022-06-10 16:40:12.356408 +0800 CST m=+0.002272427 @Author : littlerpc-generator
@Generator : littlerpc-generator @CreateTime : 2022-06-10 16:40:12.356408 +0800 CST m=+0.002272427 @Author : littlerpc-generator
std-rpc command
internal
pool
Package pool littlerpc自带的goroutine池
Package pool littlerpc自带的goroutine池
plugins
ddio module
limiter module

Jump to

Keyboard shortcuts

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