Skip to content

Commit a3548b9

Browse files
feat: Simplify signal handling using signal.NotifyContext (#103)
Simplify signal handling using signal.NotifyContext Replace manual signal handling with signal.NotifyContext which automatically cancels the context when SIGTERM or SIGINT is received. This simplifies the code by removing the HandleSignals function and using context.Canceled for error checking instead of string matching.
1 parent 10eec3b commit a3548b9

File tree

1 file changed

+8
-42
lines changed

1 file changed

+8
-42
lines changed

cmd/root.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,6 @@ import (
3131

3232
const name = "crowdsec-spoa-bouncer"
3333

34-
func HandleSignals(ctx context.Context) error {
35-
signalChan := make(chan os.Signal, 1)
36-
signal.Notify(signalChan, syscall.SIGTERM, os.Interrupt)
37-
38-
select {
39-
case s := <-signalChan:
40-
switch s {
41-
case syscall.SIGTERM:
42-
return errors.New("received SIGTERM")
43-
case os.Interrupt: // cross-platform SIGINT
44-
return errors.New("received interrupt")
45-
}
46-
case <-ctx.Done():
47-
return ctx.Err()
48-
}
49-
50-
return nil
51-
}
52-
5334
func Execute() error {
5435
// Parent pflags
5536
configPath := pflag.StringP("config", "c", "/etc/crowdsec/bouncers/crowdsec-spoa-bouncer.yaml", "path to crowdsec-spoa-bouncer.yaml")
@@ -106,7 +87,10 @@ func Execute() error {
10687
return fmt.Errorf("unable to configure bouncer: %w", err)
10788
}
10889

109-
g, ctx := errgroup.WithContext(context.Background())
90+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
91+
defer stop()
92+
93+
g, ctx := errgroup.WithContext(ctx)
11094

11195
config.Geo.Init(ctx)
11296

@@ -119,10 +103,6 @@ func Execute() error {
119103
log.Debugf("InsecureSkipVerify is set to %t", *bouncer.InsecureSkipVerify)
120104
}
121105

122-
g.Go(func() error {
123-
return HandleSignals(ctx)
124-
})
125-
126106
g.Go(func() error {
127107
err := bouncer.Run(ctx)
128108
return fmt.Errorf("bouncer run halted: %w", err)
@@ -229,18 +209,9 @@ func Execute() error {
229209
_ = csdaemon.Notify(csdaemon.Ready, log.StandardLogger())
230210

231211
err = g.Wait()
232-
233-
// Determine if this was an expected shutdown signal
234-
isExpectedShutdown := false
235-
if err != nil {
236-
switch err.Error() {
237-
case "received SIGTERM":
238-
log.Info("Received SIGTERM, shutting down")
239-
isExpectedShutdown = true
240-
case "received interrupt":
241-
log.Info("Received interrupt, shutting down")
242-
isExpectedShutdown = true
243-
}
212+
if errors.Is(err, context.Canceled) {
213+
log.Info("Context canceled, shutting down")
214+
err = nil
244215
}
245216

246217
// Shutdown SPOA server gracefully after all goroutines finish
@@ -252,10 +223,5 @@ func Execute() error {
252223
log.Errorf("Failed to shutdown SPOA: %v", shutdownErr)
253224
}
254225

255-
// Return error only if it was unexpected
256-
if err != nil && !isExpectedShutdown {
257-
return err
258-
}
259-
260-
return nil
226+
return err
261227
}

0 commit comments

Comments
 (0)