You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/options.go

86 lines
2.0 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package nnet
import (
"git.noahlan.cn/noahlan/nnet/conn"
"git.noahlan.cn/noahlan/nnet/event"
"git.noahlan.cn/noahlan/nnet/packet"
rt "git.noahlan.cn/noahlan/nnet/router"
"git.noahlan.cn/noahlan/ntool/ndef"
"time"
)
type (
// RunOption defines the method to customize an Engine.
RunOption func(ngin *Engine)
)
// EventManager returns EventManager.
func (ngin *Engine) EventManager() event.EventManager {
return ngin.evtMgr
}
// ConnManager returns connection manager
func (ngin *Engine) ConnManager() *conn.ConnManager {
return ngin.connMgr
}
//////////////////////// Options
func WithSendSize(sendSize int) RunOption {
return func(ngin *Engine) {
ngin.SendChannelSize = sendSize
}
}
func WithWriteSize(size int) RunOption {
return func(ngin *Engine) {
ngin.WriteChannelSize = size
}
}
func WithMiddleware(middleware ...rt.Middleware) RunOption {
return func(ngin *Engine) {
ngin.Use(middleware...)
}
}
// WithRouter 设置消息路由
func WithRouter(router rt.Router) RunOption {
return func(ngin *Engine) {
ngin.router = router
}
}
// WithNotFoundHandler returns a RunOption with not found handler set to given handler.
func WithNotFoundHandler(handler rt.Handler) RunOption {
return func(ngin *Engine) {
ngin.router.SetNotFoundHandler(rt.NotFoundHandler(handler))
}
}
// WithTimerPrecision 设置Timer精度需在 Start 或 Dial 之前执行
// 注精度需大于1ms, 并且不能在运行时更改
// 默认精度是 time.Second
func WithTimerPrecision(precision time.Duration) RunOption {
if precision < time.Millisecond {
panic("time precision can not less than a Millisecond")
}
return func(ngin *Engine) {
ngin.TaskTimerPrecision = precision
}
}
// WithPackerBuilder 设置 消息的封包/解包构造器
func WithPackerBuilder(fn packet.PackerBuilder) RunOption {
return func(ngin *Engine) {
ngin.packerBuilder = fn
}
}
// WithSerializer 设置消息的 序列化/反序列化 方式
func WithSerializer(s ndef.Serializer) RunOption {
return func(ngin *Engine) {
ngin.serializer = s
}
}