From 3eef2ce26c1060f8e3704fb7898f7b7b4129cf90 Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Tue, 15 Jun 2021 10:50:44 +0700 Subject: [PATCH] Use signal ctx as a base context only for requests Signed-off-by: Vladimir Popov --- main.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index a9c4052..903808d 100644 --- a/main.go +++ b/main.go @@ -61,17 +61,7 @@ import ( ) func main() { - // ******************************************************************************** - // Configure signal handling context - // ******************************************************************************** - ctx, cancel := signal.NotifyContext( - context.Background(), - os.Interrupt, - // More Linux signals here - syscall.SIGHUP, - syscall.SIGTERM, - syscall.SIGQUIT, - ) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // ******************************************************************************** @@ -152,11 +142,24 @@ func main() { client.WithDialOptions(dialOptions...), ) + // ******************************************************************************** + // Configure signal handling context + // ******************************************************************************** + signalCtx, cancelSignalCtx := signal.NotifyContext( + ctx, + os.Interrupt, + // More Linux signals here + syscall.SIGHUP, + syscall.SIGTERM, + syscall.SIGQUIT, + ) + defer cancelSignalCtx() + // ******************************************************************************** // Create Network Service Manager monitorClient // ******************************************************************************** - dialCtx, cancel := context.WithTimeout(ctx, c.DialTimeout) - defer cancel() + dialCtx, cancelDial := context.WithTimeout(signalCtx, c.DialTimeout) + defer cancelDial() logger.Infof("NSC: Connecting to Network Service Manager %v", c.ConnectTo.String()) cc, err := grpc.DialContext(dialCtx, grpcutils.URLToTarget(&c.ConnectTo), dialOptions...) @@ -175,7 +178,7 @@ func main() { id := fmt.Sprintf("%s-%d", c.Name, i) - monitorCtx, cancelMonitor := context.WithTimeout(ctx, c.RequestTimeout) + monitorCtx, cancelMonitor := context.WithTimeout(signalCtx, c.RequestTimeout) defer cancelMonitor() stream, err := monitorClient.MonitorConnections(monitorCtx, &networkservice.MonitorScopeSelector{ @@ -185,13 +188,11 @@ func main() { }, }, }) - if err != nil { logger.Fatal(err.Error()) } event, err := stream.Recv() - if err != nil { logger.Fatal(err.Error()) } @@ -221,14 +222,12 @@ func main() { defer cancelRequest() resp, err := nsmClient.Request(requestCtx, request) - if err != nil { logger.Fatalf("failed connect to NSMgr: %v", err.Error()) } defer func() { - closeCtx, cancelClose := context.WithTimeout(context.Background(), c.RequestTimeout) - closeCtx = log.WithFields(closeCtx, log.Fields(ctx)) + closeCtx, cancelClose := context.WithTimeout(ctx, c.RequestTimeout) defer cancelClose() _, _ = nsmClient.Close(closeCtx, resp) }() @@ -237,5 +236,5 @@ func main() { } // Wait for cancel event to terminate - <-ctx.Done() + <-signalCtx.Done() }