server

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableHttpPProf

func DisableHttpPProf()

DisableHttpPProf 设置禁用 HTTP PProf

  • 当 HTTP PProf 未启用时不会产生任何效果
  • 该函数支持运行时调用且支持重复调用,重复调用不会重复禁用

func EnableHttpPProf

func EnableHttpPProf(addr, prefix string, errorHandler func(err error))

EnableHttpPProf 设置启用 HTTP PProf

  • 该函数支持运行时调用且支持重复调用,重复调用不会重复开启

Types

type Conn

type Conn interface {
	// SetQueue 设置连接使用的消息队列名称
	SetQueue(queue string)

	// DelQueue 删除连接使用的消息队列,删除后连接将在系统队列执行消息
	DelQueue()

	// GetQueue 获取连接使用的消息队列名称
	GetQueue() string

	// WritePacket 写入一个 Packet
	WritePacket(packet Packet) error

	// Write 写入数据
	Write(data []byte) (n int, err error)

	// WriteBytes 写入数据
	WriteBytes(data []byte) error

	// WriteContext 写入数据
	WriteContext(data []byte, context interface{}) error
}

type ConnWriter

type ConnWriter func(packet Packet) error

ConnWriter 用于兼容不同 Network 的连接数据写入器

type ConnectionClosedEventHandler

type ConnectionClosedEventHandler func(srv Server, conn Conn, err error)

type ConnectionOpenedEventHandler

type ConnectionOpenedEventHandler func(srv Server, conn Conn)

type ConnectionReceivePacketEventHandler

type ConnectionReceivePacketEventHandler func(srv Server, conn Conn, packet Packet)

type Controller

type Controller interface {
	// GetServer 获取服务器
	GetServer() Server
	// RegisterConnection 注册连接
	RegisterConnection(conn net.Conn, writer ConnWriter)
	// EliminateConnection 消除连接
	EliminateConnection(conn net.Conn, err error)
	// ReactPacket 反应连接数据包
	ReactPacket(conn net.Conn, packet Packet)
	// GetAnts 获取服务器异步池
	GetAnts() *ants.Pool
}

Controller 控制器是暴露 Server 对用户非公开的接口信息,适用于功能性的拓展

type Events

type Events interface {
	// RegisterLaunchedEvent 注册服务器启动事件,当服务器启动后将会触发该事件
	//  - 该事件将在系统级 Actor 中运行,该事件中阻塞会导致服务器启动延迟
	RegisterLaunchedEvent(handler LaunchedEventHandler, priority ...int)

	// RegisterShutdownEvent 注册服务器关闭事件,当服务器关闭时将会触发该事件,当该事件处理完毕后服务器将关闭
	//  - 该事件将在系统级 Actor 中运行,该事件中阻塞会导致服务器关闭延迟
	//  - 该事件未执行完毕前,服务器的一切均正常运行
	RegisterShutdownEvent(handler ShutdownEventHandler, priority ...int)

	// RegisterConnectionOpenedEvent 注册连接打开事件,当新连接创建完毕时将会触发该事件
	//  - 该事件将在系统级 Actor 中运行,不应执行阻塞操作
	RegisterConnectionOpenedEvent(handler ConnectionOpenedEventHandler, priority ...int)

	// RegisterConnectionClosedEvent 注册连接关闭事件,当连接关闭后将会触发该事件
	//  - 该事件将在系统级 Actor 中运行,不应执行阻塞操作
	RegisterConnectionClosedEvent(handler ConnectionClosedEventHandler, priority ...int)

	// RegisterConnectionReceivePacketEvent 注册连接接收数据包事件,当连接接收到数据包后将会触发该事件
	//  - 该事件将在连接的 Actor 中运行,不应执行阻塞操作
	RegisterConnectionReceivePacketEvent(handler ConnectionReceivePacketEventHandler, priority ...int)
}

type LaunchedEventHandler

type LaunchedEventHandler func(srv Server, ip string, t time.Time)

type Network

type Network interface {
	// OnSetup 装载函数,该函数将传入 Server 的上下文及一个可用于控制 Server 行为的控制器,在该函数中应该完成 Network 实现的初始化工作
	OnSetup(ctx context.Context, controller Controller) error

	// OnRun 运行函数,在该函数中通常是进行网络监听启动的阻塞行为
	OnRun() error

	// OnShutdown 停止函数,该函数在执行的时候应该完成对资源的停止及释放
	OnShutdown() error

	// Schema 获取协议标识
	Schema() string

	// Address 获取监听地址信息
	Address() string
}

Network 提供给 Server 使用的网络接口

type Options

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

func DefaultOptions

func DefaultOptions() *Options

func NewOptions

func NewOptions() *Options

func (*Options) Apply

func (opt *Options) Apply(options ...*Options)

func (*Options) GetActorMessageBufferInitialSize

func (opt *Options) GetActorMessageBufferInitialSize() int

func (*Options) GetActorMessageChannelSize

func (opt *Options) GetActorMessageChannelSize() int

func (*Options) GetAsyncLowMessageDuration

func (opt *Options) GetAsyncLowMessageDuration() time.Duration

func (*Options) GetLifeCycleLimit

func (opt *Options) GetLifeCycleLimit() time.Duration

func (*Options) GetLogger

func (opt *Options) GetLogger() *log.Logger

GetLogger 获取服务器的日志记录器

func (*Options) GetServerMessageBufferInitialSize

func (opt *Options) GetServerMessageBufferInitialSize() int

func (*Options) GetServerMessageChannelSize

func (opt *Options) GetServerMessageChannelSize() int

func (*Options) GetSyncLowMessageDuration

func (opt *Options) GetSyncLowMessageDuration() time.Duration

func (*Options) IsDebug

func (opt *Options) IsDebug() bool

IsDebug 获取当前服务器是否是 Debug 模式

func (*Options) WithActorMessageBufferInitialSize

func (opt *Options) WithActorMessageBufferInitialSize(size int) *Options

WithActorMessageBufferInitialSize 设置 Actor 消息环形缓冲区 buffer.Ring 的初始大小,适当的值可以避免频繁扩容

  • 由于扩容是按照当前大小的 2 倍进行扩容,过大的值也可能会导致内存消耗过高

func (*Options) WithActorMessageChannelSize

func (opt *Options) WithActorMessageChannelSize(size int) *Options

WithActorMessageChannelSize 设置 Actor 用于处理消息的管道大小,当管道由于逻辑阻塞而导致满载时,会导致新消息无法及时从缓冲区拿出,从而增加内存的消耗,但是并不会影响消息的写入

func (*Options) WithAsyncLowMessageMonitor

func (opt *Options) WithAsyncLowMessageMonitor(duration time.Duration) *Options

WithAsyncLowMessageMonitor 设置异步消息的慢消息监测时间

func (*Options) WithDebug

func (opt *Options) WithDebug(debug bool) *Options

WithDebug 设置 Debug 模式是否开启

  • 该函数支持运行时设置

func (*Options) WithLifeCycleEnd

func (opt *Options) WithLifeCycleEnd(end time.Time) *Options

WithLifeCycleEnd 设置服务器生命周期终点,在服务器达到该时间后将关闭服务器

  • 如果设置 end 为零值或小于当前时间的值,将不限制服务器生命周期
  • 该函数支持运行时设置

func (*Options) WithLifeCycleLimit

func (opt *Options) WithLifeCycleLimit(limit time.Duration) *Options

WithLifeCycleLimit 设置服务器生命周期上限,在服务器启动后达到生命周期上限将关闭服务器

  • 如果设置为 <= 0 的值,将不限制服务器生命周期
  • 该函数支持运行时设置

func (*Options) WithLogger

func (opt *Options) WithLogger(logger *log.Logger) *Options

WithLogger 设置服务器的日志记录器

  • 该函数支持运行时设置

func (*Options) WithServerMessageBufferInitialSize

func (opt *Options) WithServerMessageBufferInitialSize(size int) *Options

WithServerMessageBufferInitialSize 设置服务器 Actor 消息环形缓冲区 buffer.Ring 的初始大小,适当的值可以避免频繁扩容

  • 由于扩容是按照当前大小的 2 倍进行扩容,过大的值也可能会导致内存消耗过高

func (*Options) WithServerMessageChannelSize

func (opt *Options) WithServerMessageChannelSize(size int) *Options

WithServerMessageChannelSize 设置服务器 Actor 用于处理消息的管道大小,当管道由于逻辑阻塞而导致满载时,会导致新消息无法及时从缓冲区拿出,从而增加内存的消耗,但是并不会影响消息的写入

func (*Options) WithSyncLowMessageMonitor

func (opt *Options) WithSyncLowMessageMonitor(duration time.Duration) *Options

WithSyncLowMessageMonitor 设置同步消息的慢消息监测时间

type Packet

type Packet interface {
	GetBytes() []byte
	SetContext(ctx any) Packet
	GetContext() any
}

func NewPacket

func NewPacket(data []byte) Packet

type Producer

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

func (Producer) GetConn

func (p Producer) GetConn() (conn Conn, exist bool)

func (Producer) GetServer

func (p Producer) GetServer() Server

type Server

type Server interface {
	Events

	// Run 运行服务器
	Run() error

	// Shutdown 关闭服务器
	Shutdown() error

	// GetStatus 获取服务器状态
	GetStatus() *State

	// PublishSyncMessage 发布同步消息
	PublishSyncMessage(topic string, handler messageEvents.SynchronousHandler)

	// PublishAsyncMessage 发布异步消息,当包含多个 callback 时,仅首个生效
	PublishAsyncMessage(topic string, handler messageEvents.AsynchronousHandler, callback ...messageEvents.AsynchronousCallbackHandler)
}

func NewServer

func NewServer(network Network, options ...*Options) Server

type ShutdownEventHandler

type ShutdownEventHandler func(srv Server)

type State

type State struct {
	LaunchedAt time.Time // 服务器启动时间
	Ip         string    // 服务器 IP 地址
	// contains filtered or unexported fields
}

func (*State) Status

func (s *State) Status() *State

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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