diff --git a/network/engine.go b/network/engine.go index 2f552ed3..093754de 100644 --- a/network/engine.go +++ b/network/engine.go @@ -11,25 +11,8 @@ import ( "github.com/rs/zerolog" ) -type Option struct { - EnableTicker bool -} - -type Action int - -const ( - None Action = iota - Close - Shutdown -) - -type TCPSocketOpt int - -const ( - TCPNoDelay TCPSocketOpt = iota - TCPDelay -) - +// Engine is the network engine. +// TODO: Move this to the Server struct. type Engine struct { listener net.Listener host string @@ -41,19 +24,22 @@ type Engine struct { mu *sync.RWMutex } +// CountConnections returns the current number of connections. func (engine *Engine) CountConnections() int { engine.mu.RLock() defer engine.mu.RUnlock() return int(engine.connections) } +// Stop stops the engine. func (engine *Engine) Stop(ctx context.Context) error { _, cancel := context.WithDeadline(ctx, time.Now().Add(config.DefaultEngineStopTimeout)) defer cancel() + var err error engine.running.Store(false) if engine.listener != nil { - if err := engine.listener.Close(); err != nil { + if err = engine.listener.Close(); err != nil { engine.logger.Error().Err(err).Msg("Failed to close listener") } } else { @@ -61,5 +47,16 @@ func (engine *Engine) Stop(ctx context.Context) error { } engine.stopServer <- struct{}{} close(engine.stopServer) - return nil + return err +} + +// NewEngine creates a new engine. +func NewEngine(logger zerolog.Logger) Engine { + return Engine{ + connections: 0, + logger: logger, + running: &atomic.Bool{}, + stopServer: make(chan struct{}), + mu: &sync.RWMutex{}, + } } diff --git a/network/server.go b/network/server.go index ae9fd48a..14ed58ed 100644 --- a/network/server.go +++ b/network/server.go @@ -8,7 +8,6 @@ import ( "os" "strconv" "sync" - "sync/atomic" "time" v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" @@ -21,6 +20,18 @@ import ( "go.opentelemetry.io/otel/attribute" ) +type Option struct { + EnableTicker bool +} + +type Action int + +const ( + None Action = iota + Close + Shutdown +) + type Server struct { engine Engine proxy IProxy @@ -382,15 +393,6 @@ func (s *Server) Run() *gerr.GatewayDError { } } - // Start the server. - s.engine = Engine{ - connections: 0, - logger: s.logger, - stopServer: make(chan struct{}), - mu: &sync.RWMutex{}, - running: &atomic.Bool{}, - } - if action := s.OnBoot(s.engine); action != None { return nil } @@ -564,6 +566,7 @@ func NewServer( pluginRegistry: pluginRegistry, pluginTimeout: pluginTimeout, mu: &sync.RWMutex{}, + engine: NewEngine(logger), } // Try to resolve the address and log an error if it can't be resolved.