Skip to content

Commit 7cc3022

Browse files
committed
Add shutdown handler
1 parent a19ec3f commit 7cc3022

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package main
33
import (
44
"log"
55
"os"
6+
"os/signal"
67
"strings"
8+
"syscall"
79

810
"github.com/hashicorp/go-hclog"
911
"github.com/netauth/netauth/pkg/netauth"
@@ -60,8 +62,21 @@ func main() {
6062
os.Exit(1)
6163
}
6264

63-
if err := srvr.Serve(); err != nil {
64-
appLogger.Error("Error serving", "error", err)
65-
os.Exit(1)
65+
go func() {
66+
if err := srvr.Serve(); err != nil {
67+
appLogger.Error("Error serving", "error", err)
68+
os.Exit(1)
69+
}
70+
}()
71+
72+
// Sit here and wait for a signal to shutdown.
73+
ch := make(chan os.Signal, 5)
74+
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
75+
<-ch
76+
appLogger.Info("Shutting down...")
77+
if err := srvr.Shutdown(); err != nil {
78+
appLogger.Error("Error during shutdown", "error", err)
79+
os.Exit(2)
6680
}
81+
appLogger.Info("Goodbye!")
6782
}

radius/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package radius
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/hashicorp/go-hclog"
@@ -43,7 +44,13 @@ func (s *Server) Serve() error {
4344
Handler: radius.HandlerFunc(s.handler),
4445
SecretSource: radius.StaticSecretSource([]byte("secret")),
4546
}
47+
s.radsrv = server
4648

4749
s.log.Info("Serving Radius on :1812")
4850
return server.ListenAndServe()
4951
}
52+
53+
// Shutdown requests the underlying packetserver to shutdown smoothly.
54+
func (s *Server) Shutdown() error {
55+
return s.radsrv.Shutdown(context.Background())
56+
}

radius/type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"context"
55

66
"github.com/hashicorp/go-hclog"
7+
"layeh.com/radius"
78
)
89

910
// Server owns the radius server itself and its handlers.
1011
type Server struct {
1112
log hclog.Logger
1213
n netauth
14+
15+
radsrv radius.PacketServer
1316
}
1417

1518
// Option enables passing of various options to the server on startup.

0 commit comments

Comments
 (0)