From 265cb66b572acfc721c2d47c380b2fb5ebf76f6c Mon Sep 17 00:00:00 2001 From: Sanskarzz Date: Thu, 14 Mar 2024 14:19:48 +0530 Subject: [PATCH] pref: used single context instead of chan Signed-off-by: Sanskarzz --- main.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 44e8ea83..5344af74 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "log" "net" "net/http" - "os" "os/signal" "syscall" "time" @@ -73,18 +72,17 @@ func (s *Servers) startGRPCServer(ctx context.Context) { } func main() { - httpCtx, httpCancel := context.WithCancel(context.Background()) - grpcCtx, grpcCancel := context.WithCancel(context.Background()) + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer cancel() srv := NewServers() - go srv.startHTTPServer(httpCtx) - go srv.startGRPCServer(grpcCtx) - done := make(chan os.Signal, 1) - signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) - <-done - httpCancel() - grpcCancel() - time.Sleep(5 * time.Second) + go srv.startHTTPServer(ctx) + go srv.startGRPCServer(ctx) + go func() { + <-ctx.Done() + cancel() + }() + select {} }