From 8ab1396129660627e1489fe0e8d4a8fd46d8fb31 Mon Sep 17 00:00:00 2001 From: kehiy Date: Sun, 15 Sep 2024 23:45:40 +0330 Subject: [PATCH 1/3] feat(relay): gracefull shutdown --- cmd/commands/help.go | 19 ++++++++++++++++++- cmd/commands/run.go | 27 ++++++++++++++++++++------- cmd/main.go | 1 + relay/relay.go | 2 +- server/server.go | 4 ++-- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/cmd/commands/help.go b/cmd/commands/help.go index c046f90..052c883 100644 --- a/cmd/commands/help.go +++ b/cmd/commands/help.go @@ -1,5 +1,22 @@ package commands +import "fmt" + +const help = ` +Available commands: +Commands: + run starts the application with the specified configuration file. + help displays this help information. + version displays the version of software. + +Usage: + [options] + +Examples: + run config.yaml run the application using config.yaml. + help display this help message. +` + func HandleHelp(_ []string) { - // TODO::: + fmt.Print(help) //nolint } diff --git a/cmd/commands/run.go b/cmd/commands/run.go index 6eac71d..180eb3e 100644 --- a/cmd/commands/run.go +++ b/cmd/commands/run.go @@ -2,6 +2,7 @@ package commands import ( "errors" + "fmt" "os" "os/signal" "syscall" @@ -25,15 +26,27 @@ func HandleRun(args []string) { ExitOnError(err) } - if err := r.Start(); err != nil { - ExitOnError(err) - } - + errCh := make(chan error) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) - <-sigChan - if err := r.Stop(); err != nil { - ExitOnError(err) + go func() { + if err := r.Start(); err != nil { + errCh <- err + } + }() + + select { + case sig := <-sigChan: + fmt.Printf("Received signal: %s\nInitiating graceful shutdown...\n", sig.String()) //nolint + if err := r.Stop(); err != nil { + ExitOnError(err) + } + + case err := <-errCh: + fmt.Printf("Unexpected error: %v\nInitiating shutdown due to the error...\n", err) //nolint + if err := r.Stop(); err != nil { + ExitOnError(err) + } } } diff --git a/cmd/main.go b/cmd/main.go index b39ab74..a963c98 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,6 +20,7 @@ func main() { case "help": commands.HandleHelp(os.Args) + os.Exit(0) case "version": fmt.Println(immortal.StringVersion()) //nolint diff --git a/relay/relay.go b/relay/relay.go index 7b74778..7b36efb 100644 --- a/relay/relay.go +++ b/relay/relay.go @@ -22,7 +22,7 @@ func New(cfg config.Config) (*Relay, error) { return &Relay{ config: cfg, - server: server.NewServer(cfg.ServerConf), + server: server.New(cfg.ServerConf), database: db, }, nil } diff --git a/server/server.go b/server/server.go index a7dab3a..bdcc364 100644 --- a/server/server.go +++ b/server/server.go @@ -23,7 +23,7 @@ type Server struct { connsLock sync.RWMutex } -func NewServer(cfg Config) *Server { +func New(cfg Config) *Server { return &Server{ config: cfg, conns: make(map[*websocket.Conn]map[string]filter.Filters), @@ -77,7 +77,7 @@ func (s *Server) readLoop(ws *websocket.Conn) { go s.handleEvent(ws, msg) case "CLOSE": - s.handleClose(ws, msg) + go s.handleClose(ws, msg) } } } From ebd141db8a81c5f2ee0e425cfb62af4bc3329d24 Mon Sep 17 00:00:00 2001 From: kehiy Date: Sun, 15 Sep 2024 23:49:53 +0330 Subject: [PATCH 2/3] help command enhance --- cmd/commands/help.go | 3 +-- cmd/main.go | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/commands/help.go b/cmd/commands/help.go index 052c883..e95d0f0 100644 --- a/cmd/commands/help.go +++ b/cmd/commands/help.go @@ -2,8 +2,7 @@ package commands import "fmt" -const help = ` -Available commands: +const help = `Available commands: Commands: run starts the application with the specified configuration file. help displays this help information. diff --git a/cmd/main.go b/cmd/main.go index a963c98..fe135ac 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,7 +11,8 @@ import ( func main() { if len(os.Args) < 2 { - commands.ExitOnError(errors.New("at least 1 arguments expected.\nuse help command for more information")) + commands.HandleHelp(os.Args) + commands.ExitOnError(errors.New("at least 1 arguments expected.")) } switch os.Args[1] { From 0c9c00dddd2ef822cdb0393c7ae1f53f3b2ef127 Mon Sep 17 00:00:00 2001 From: kehiy Date: Sun, 15 Sep 2024 23:55:09 +0330 Subject: [PATCH 3/3] help command enhance --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index fe135ac..d4bc624 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,7 +12,7 @@ import ( func main() { if len(os.Args) < 2 { commands.HandleHelp(os.Args) - commands.ExitOnError(errors.New("at least 1 arguments expected.")) + commands.ExitOnError(errors.New("at least 1 arguments expected")) } switch os.Args[1] {