vex

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 13 Imported by: 2

README

⛓ Vex

Go Doc License Coverage Test

Vex 是一个使用 tcp 通信和传输数据的框架。

Read me in English

🍃 功能特性

  • 基于 tcp 自定义协议传输数据,极简 API 设计
  • 支持信号量监控机制和平滑下线
  • 支持连接数限制,并支持超时中断(敬请期待)
  • 支持客户端、服务器两种拦截器,方便监控(敬请期待)
  • 内置连接池,可以对性能进行调优(敬请期待)

历史版本的特性请查看 HISTORY.md。未来版本的新特性和计划请查看 FUTURE.md

📃 协议描述

ABNF:

PACKET = ID MAGIC FLAGS LENGTH DATA
ID = 8OCTET ; 编号,用来区分不同的数据包
MAGIC = 4OCTET ; 魔数,目前是 1997811915
FLAGS = 8OCTET ; 标志位,比如是否为错误包
LENGTH = 4OCTET ; 长度,最大 4GB
DATA = *OCTET ; 数据,需要靠 LENGTH 来确认

人话:

数据包:
id       magic     flags     length     {data}
8byte    4byte     8byte     4byte      unknown

你会发现协议没有版本号的字段,其实是我们选择将版本号融入到魔数字段中,所以每个版本可能对应的魔数不一样。

🔦 使用案例

$ go get -u github.com/FishGoddess/vex

客户端:

package main

import (
	"context"
	"fmt"

	"github.com/FishGoddess/vex"
)

func main() {
	client, err := vex.NewClient("127.0.0.1:9876")
	if err != nil {
		panic(err)
	}

	defer client.Close()

	ctx := context.Background()
	data := []byte("落得湖面月圆满,独守湖边酒哀愁")

	received, err := client.Send(ctx, data)
	if err != nil {
		panic(err)
	}

	fmt.Printf("client send: %s\n", data)
	fmt.Printf("server send: %s\n", received)
}

服务端:

package main

import (
	"context"

	"github.com/FishGoddess/vex"
)

type EchoHandler struct{}

func (EchoHandler) Handle(ctx context.Context, data []byte) ([]byte, error) {
	return data, nil
}

func main() {
	server := vex.NewServer("127.0.0.1:9876", EchoHandler{})
	defer server.Close()

	if err := server.Serve(); err != nil {
		panic(err)
	}
}

所有的使用案例都在 _examples 目录。

🛠 性能测试

$ make bench
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) CPU E5-26xx v4

BenchmarkPacket-2                  43992             26818 ns/op            4664 B/op         15 allocs/op
BenchmarkPacketPool-2              56511             20681 ns/op            4690 B/op         16 allocs/op

测试文件:_examples/packet_test.go

连接池性能测试使用的连接数是 2,单网卡已经达到瓶颈。

👥 贡献者

如果您觉得 vex 缺少您需要的功能,请不要犹豫,马上参与进来,发起一个 issue

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Send(ctx context.Context, data []byte) ([]byte, error)
	Close() error
}

Client is the interface of vex client.

func NewClient

func NewClient(address string, opts ...Option) (Client, error)

NewClient creates a client with address.

type Context added in v0.4.2

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

Context wraps a context inside and carries some attributes for using.

func (*Context) LocalAddress added in v0.5.2

func (c *Context) LocalAddress() string

LocalAddress returns the address of server.

func (*Context) RemoteAddress added in v0.5.2

func (c *Context) RemoteAddress() string

RemoteAddress returns the address of client.

type DialFunc added in v0.5.2

type DialFunc func(ctx context.Context) (Client, error)

DialFunc dials with context and returns the client. Returns an error if failed.

type Handler added in v0.5.2

type Handler interface {
	Handle(ctx *Context, data []byte) ([]byte, error)
}

Handler is for handling the data from client and returns the new data or an error if failed.

type Logger added in v0.5.2

type Logger interface {
	Debug(msg string, kvs ...any)
	Info(msg string, kvs ...any)
	Error(msg string, kvs ...any)
}

Logger is for logging some messages in different levels. You can just use log/slog package which is one implement of it.

type Option added in v0.4.2

type Option func(c *config)

Option configures the config for client or server.

func WithDialTimeout added in v0.5.2

func WithDialTimeout(timeout time.Duration) Option

WithDialTimeout sets the dial timeout to config.

func WithLogger added in v0.5.2

func WithLogger(logger Logger) Option

WithLogger sets the logger to config.

type Pool added in v0.5.2

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

Pool is a pool for reusing clients. You should always use a pool instead of using a client in production.

func NewPool added in v0.5.2

func NewPool(limit uint64, dial DialFunc, opts ...Option) *Pool

NewPool returns a pool with limit and dial function. Dial function should return a new client as your way and an error if failed.

func (*Pool) Close added in v0.5.2

func (p *Pool) Close() error

Close closes the pool and releases all clients in it.

func (*Pool) Get added in v0.5.2

func (p *Pool) Get(ctx context.Context) (Client, error)

Get gets a client from pool and returns an error if failed.

func (*Pool) Status added in v0.5.2

func (p *Pool) Status() Status

Status returns the status of pool.

type Server

type Server interface {
	Serve() error
	Close() error
}

Server is the interface of vex server.

func NewServer

func NewServer(address string, handler Handler, opts ...Option) Server

NewServer creates a server with address and handler.

type Status added in v0.4.2

type Status rego.Status

Status is the status information of pool.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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