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" "git.noahlan.cn/noahlan/ntool/npool" "github.com/panjf2000/ants/v2" "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 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 } } // WithPool 设置使用自定义的工作池 func WithPool(pl *ants.Pool) RunOption { return func(ngin *Engine) { ngin.pool = pl } } // WithPoolCfg 设置工作池配置 func WithPoolCfg(cfg npool.Config) RunOption { return func(ngin *Engine) { ngin.pool, _ = ants.NewPool(cfg.PoolSize, ants.WithOptions(cfg.Options())) } }