Skip to content

Commit

Permalink
Use signal ctx as a base context only for requests (#218)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov authored Jun 15, 2021
1 parent c94b15e commit 3d1ef61
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

// ********************************************************************************
Expand Down Expand Up @@ -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...)
Expand All @@ -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{
Expand All @@ -185,13 +188,11 @@ func main() {
},
},
})

if err != nil {
logger.Fatal(err.Error())
}

event, err := stream.Recv()

if err != nil {
logger.Fatal(err.Error())
}
Expand Down Expand Up @@ -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)
}()
Expand All @@ -237,5 +236,5 @@ func main() {
}

// Wait for cancel event to terminate
<-ctx.Done()
<-signalCtx.Done()
}

0 comments on commit 3d1ef61

Please sign in to comment.