hydra

package module
v0.0.0-...-1afb86a Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: MIT Imports: 14 Imported by: 0

README

Hydra

同一个端口,支持多种协议。
采用协议头字段识别来支持多协议。

使用demo 见此

比如 HTTP 协议识别:protocols/xhttp/protocol.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunGrace

func RunGrace(ser Server, l net.Listener, timeout time.Duration) error

Types

type Hydra

type Hydra struct {
	// Opts 配置选项,可选
	Opts *Options

	// DefaultListener 未识别的协议,可选
	// 若未设置,在运行时会自动初始化
	DefaultListener Listener
	// contains filtered or unexported fields
}

Hydra 识别多协议的 fixedListenerServer

func (*Hydra) BindListener

func (h *Hydra) BindListener(p Protocol, ln Listener)

BindListener 绑定协议和 Listener 解析成功的 Conn 会发送到该 Listener

func (*Hydra) BindServer

func (h *Hydra) BindServer(p Protocol, ser Server)

BindServer 绑定协议和 Server

func (*Hydra) GetDefaultListener

func (h *Hydra) GetDefaultListener() net.Listener

GetDefaultListener 未知协议的连接

func (*Hydra) Listen

func (h *Hydra) Listen(p Protocol) net.Listener

Listen 绑定一个协议头,并返回对应的 Listener

func (*Hydra) Serve

func (h *Hydra) Serve(listener net.Listener) error

Serve 监听服务 注意,必须在所有 Listen 完成之后

func (*Hydra) SetDefaultServer

func (h *Hydra) SetDefaultServer(ser Server)

SetDefaultServer 绑定未知协议对应的 Server

func (*Hydra) Shutdown

func (h *Hydra) Shutdown(ctx context.Context) error

Shutdown 停止服务

type Listener

type Listener interface {
	net.Listener
	Dispatch(conn net.Conn)
	SetLocalAddr(addr net.Addr)
}

Listener 具备接收 Conn 的 Listener

func NewListener

func NewListener(size int) Listener

NewListener 创建一个指定 chan size 的 Listener

type Logger

type Logger interface {
	Println(v ...any)
	Printf(format string, v ...any)
}

Logger 日志接口定义

type Options

type Options struct {
	ListerChanSize int
	AcceptTimeout  time.Duration

	OnAcceptError func(err error)
}

func (*Options) GetAcceptTimeout

func (o *Options) GetAcceptTimeout() time.Duration

func (*Options) GetListerChanSize

func (o *Options) GetListerChanSize() int

type Protocol

type Protocol interface {
	// Name 协议名字
	Name() string

	// MinLen 为最小长度,用于做非判断( MustNot )
	MinLen() int

	// MustNot 通过 MinLen 长度的 header 判断,一定不是该协议
	// 	当确定不是该协议的时候,返回 true
	// 	若不确定,应该返回 false
	MustNot(header []byte) bool

	// MaxLen 最大长度,用于明确的判断不是该协议
	// 应确保 MaxLen >= MinLen > 0
	MaxLen() int

	// Is 通过 MaxLen 长度的 header 判断是否该协议
	Is(header []byte) bool
}

Protocol 协议接口定义

type Server

type Server interface {
	// Serve 启动 server
	Serve(net.Listener) error

	// Shutdown 优雅关闭
	Shutdown(context.Context) error
}

Server 具备优雅关闭的 server

type Starter

type Starter struct {
	Server   Server
	Listener net.Listener

	Timeout time.Duration
	Signals []os.Signal
	Logger  Logger
}

Starter 优雅关闭 server

func (*Starter) RunGrace

func (sd *Starter) RunGrace() error

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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