Skip to content

Commit

Permalink
Fix shutdown behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
pcallewaert committed Mar 21, 2024
1 parent addac74 commit 64276a1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions aproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ func main() {
listenFlag := flag.String("listen", ":8443", "the address and port on which the server will listen")
flag.Parse()
listenAddr := *listenFlag
ctx := context.Background()
signal.NotifyContext(ctx, os.Interrupt)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
listenConfig := new(net.ListenConfig)
listener, err := listenConfig.Listen(ctx, "tcp", listenAddr)
if err != nil {
Expand All @@ -390,12 +390,17 @@ func main() {
log.Fatalf("no upstearm proxy specified")
}
slog.Info(fmt.Sprintf("start forwarding to proxy %s", proxy))
for {
conn, err := listener.Accept()
if err != nil {
slog.Error("failed to accept connection", "error", err)
continue
go func() {
for {
conn, err := listener.Accept()
if err != nil {
slog.Error("failed to accept connection", "error", err)
continue
}
go HandleConn(conn, proxy)
}
go HandleConn(conn, proxy)
}
}()
<-ctx.Done()
stop()
slog.Info("server gracefully shutdown")
}

0 comments on commit 64276a1

Please sign in to comment.