Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[deployments-k8s#1758] Use signal ctx as a base context only for requests #218

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}