crpc

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 16 Imported by: 0

README

crpc

crpc Go Report Card Go Reference license

golang rpc框架,支持以下功能:

  1. 流式传输
  2. 数据加密
  3. 数据压缩
  4. 结构序列化

分层设计

在crpc框架使用以下的多层设计,每一个层次有其相应的数据结构

+------------+-------------------+--------------------+-------+
| data frame | encrypt(optional) | compress(optional) | codec |
+------------+-------------------+--------------------+-------+
  • data frame: 数据帧,最底层数据结构,直接面向于tcp协议
  • encrypt: 数据加密层,目前已支持aes和des加密算法
  • compress: 数据压缩层,目前已支持gzip和zstd压缩算法
  • codec: 数据序列化层,目前支持[]bytehttp.Requesthttp.Response三种数据结构的序列化
数据帧(network)

数据帧为最基础数据结构,直接作用于tcp链路,其封装格式如下

+-------------+---------+----------+---------+---------+
| Sequence(4) | Size(2) | Crc32(4) | Flag(4) | Payload |
+-------------+---------+----------+---------+---------+

以上内容括号中的数字表示字节数,其中Flag字段为枚举类型,枚举值如下

+---------+------------+----------+---------+---------+---------+-----------+---------------+
| Open(1) | OpenAck(1) | Close(1) | Data(1) | Ping(1) | Pong(1) | Unused(2) | Stream ID(24) |
+---------+------------+----------+---------+---------+---------+-----------+---------------+

以上内容括号中的数字表示比特位,其中每一个比特位代表一个标志位,互相之间是互斥关系,目前仅使用了Flag字段第一字节的高6位,由于Stream ID字段仅有3字节,因此crpc中仅支持16777215个stream同时传输数据

数据加密层(encoding/encrypt)

数据加密层用于将原始数据进行加密,在数据加密前会将原始数据的crc32校验码添加到数据尾部作为解密后的校验依据,其封装格式如下:

+----------+----------+
+ Src Data | Crc32(4) |
+----------+----------+
  • aes加密算法: aes加密算法使用32字节长度密钥以及16字节的iv进行CBC算法加密
  • des加密算法: des加密算法使用24字节长度密钥以及8字节的iv进行TripleDES算法加密

当给定密钥长度不足时,底层会重复多次密钥内容以保证加密运算的进行

数据压缩层(encoding/compress)

数据压缩层用于将原始数据进行压缩,在数据压缩前会将原始数据的crc32校验码添加到数据尾部作为解压后的校验依据,其封装格式如下:

+----------+----------+
+ Src Data | Crc32(4) |
+----------+----------+
数据编码层(encoding/codec)

数据编码层用于描述原始数据类型,主要作用于数据的序列化和反序列化过程,数据结构如下:

+---------+---------+
+ Type(1) | Payload |
+---------+---------+

其中Type字段为1字节,表示当前数据类型,定义如下:

  • 0: 未知数据类型
  • 1: raw data,可反序列化到[]byte
  • 2: http request,可反序列化到http.Request
  • 3: http response,可反序列化到http.Response
http请求

grpc框架底层使用X-Crpc-Request-Id字段进行request与response的关联,因此在使用过程中请勿使用该字段。

示例

TODO

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("closed")

ErrClosed closed error

View Source
var ErrReconnecting = errors.New("reconnecting")

ErrReconnecting reconnecting error

View Source
var ErrTimeout = errors.New("transport: timeout")

ErrTimeout timeout error

Functions

This section is empty.

Types

type AcceptStreamHandlerFunc

type AcceptStreamHandlerFunc func(*Stream)

AcceptStreamHandlerFunc handler func after accept

type Client

type Client struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Client rpc client

func NewClient

func NewClient(addr string) (*Client, error)

NewClient create client

func (*Client) Call

func (cli *Client) Call(req *http.Request, timeout time.Duration) (*http.Response, error)

Call call http request

func (*Client) Close added in v1.0.1

func (cli *Client) Close() error

Close close client

func (*Client) OpenStream

func (cli *Client) OpenStream(timeout time.Duration) (*Stream, error)

OpenStream open stream

func (*Client) SetCompresser added in v1.0.4

func (cli *Client) SetCompresser(compresser encoding.Compresser)

SetCompresser set compresser

func (*Client) SetEncrypter added in v1.0.4

func (cli *Client) SetEncrypter(encrypter encoding.Encrypter)

SetEncrypter set encrypter

type RequestHandlerFunc

type RequestHandlerFunc func(*http.Request) (*http.Response, error)

RequestHandlerFunc request handler

type Server

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

Server rpc server

func NewServer

func NewServer(cfg ServerConfig) *Server

NewServer create server

func (*Server) Close added in v1.0.1

func (svr *Server) Close() error

Close close server

func (*Server) ListenAndServe

func (svr *Server) ListenAndServe(addr string) error

ListenAndServe listen and serve

type ServerConfig added in v1.0.4

type ServerConfig struct {
	Encrypter  encoding.Encrypter
	Compresser encoding.Compresser
	OnRequest  RequestHandlerFunc
	OnAccept   AcceptStreamHandlerFunc
}

ServerConfig server config

type Stream

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

Stream stream

func (*Stream) Close

func (s *Stream) Close() error

Close close stream

func (*Stream) Read

func (s *Stream) Read(p []byte) (int, error)

Read read data from stream

func (*Stream) Write

func (s *Stream) Write(p []byte) (int, error)

Write write data in stream

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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