package server import ( "context" "sync" "git.noahlan.cn/noahlan/nnet/v2/internal/logger" "git.noahlan.cn/noahlan/nnet/v2/pkg/config" "git.noahlan.cn/noahlan/nnet/v2/pkg/executor" ) // handlerExecutor owns the optional asynchronous handler executor. An // injected executor is deliberately not owned by the server. type handlerExecutor struct { exec executor.Executor owned bool shutdownOnce sync.Once shutdownErr error } func newHandlerExecutor(cfg *config.Config, log logger.Logger) *handlerExecutor { if cfg == nil || cfg.HandlerExecutionMode != config.HandlerExecutionPerConnection { return nil } if cfg.HandlerExecutor != nil { return &handlerExecutor{exec: cfg.HandlerExecutor} } return &handlerExecutor{ exec: executor.NewPerKey(executor.Config{ Workers: cfg.HandlerWorkers, QueueSize: cfg.HandlerQueueSize, OnPanic: func(value interface{}) { log.Error("Handler panic recovered: %v", value) }, }), owned: true, } } func (h *handlerExecutor) submit(key string, task executor.Task) error { if h == nil || h.exec == nil { return nil } return h.exec.Submit(key, task) } func (h *handlerExecutor) shutdown(ctx context.Context) error { if h == nil || !h.owned || h.exec == nil { return nil } h.shutdownOnce.Do(func() { h.shutdownErr = h.exec.Shutdown(ctx) }) return h.shutdownErr }