Skip to content

Commit ac8d101

Browse files
committed
fix: cancel error group properly
1 parent 341df75 commit ac8d101

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

internal/auth/server.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"github.com/hashicorp/go-bexpr"
@@ -22,7 +23,7 @@ import (
2223

2324
const authCachePrefix = "a_"
2425

25-
func StartServer(config *config.Config) error {
26+
func StartServer(ctx context.Context, config *config.Config) error {
2627
v, r := version.GetReleaseInfo()
2728
logrus.Infof("Starting brink auth server. Version %s - %s", v, r)
2829

@@ -44,7 +45,7 @@ func StartServer(config *config.Config) error {
4445
}
4546
authServer.RegisterRoutes(e, true)
4647

47-
return server.Start(config, e)
48+
return server.Start(ctx, config, e)
4849
}
4950

5051
func NewServer(config config.Auth, cache cache.Cache) (*Server, error) {

internal/cmd/server.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"github.com/jsiebens/brink/internal/auth"
56
"github.com/jsiebens/brink/internal/config"
67
"github.com/jsiebens/brink/internal/proxy"
@@ -27,7 +28,7 @@ func serverProxyCommand() *coral.Command {
2728
return createServerCommand("proxy", "Start a proxy server with a configuration file.", proxy.StartServer)
2829
}
2930

30-
func createServerCommand(use, short string, start func(*config.Config) error) *coral.Command {
31+
func createServerCommand(use, short string, start func(context.Context, *config.Config) error) *coral.Command {
3132
command := &coral.Command{
3233
Use: use,
3334
Short: short,
@@ -43,7 +44,7 @@ func createServerCommand(use, short string, start func(*config.Config) error) *c
4344
if err != nil {
4445
return err
4546
}
46-
return start(c)
47+
return start(command.Context(), c)
4748
}
4849

4950
return command

internal/proxy/server.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package proxy
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/hashicorp/yamux"
67
"github.com/jsiebens/brink/internal/api"
@@ -25,7 +26,7 @@ import (
2526
const authCachePrefix = "pa_"
2627
const proxyCachePrefix = "pp_"
2728

28-
func StartServer(config *config.Config) error {
29+
func StartServer(ctx context.Context, config *config.Config) error {
2930
v, r := version.GetReleaseInfo()
3031
logrus.Infof("Starting brink proxy server. Version %s - %s", v, r)
3132

@@ -70,7 +71,7 @@ func StartServer(config *config.Config) error {
7071
}
7172
proxyServer.RegisterRoutes(e)
7273

73-
return server.Start(config, e)
74+
return server.Start(ctx, config, e)
7475
}
7576

7677
func NewServer(config config.Proxy, cache cache.Cache, registry auth.SessionRegistry) (*Server, error) {

internal/server/server.go

+32-15
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,26 @@ import (
1515
"time"
1616
)
1717

18-
func Start(config *config.Config, e *echo.Echo) error {
18+
func Start(ctx context.Context, config *config.Config, e *echo.Echo) error {
1919
m := mon.Echo()
2020

2121
registerDefaultRoutes(e)
2222
e.Use(mon.Middleware())
2323

24-
done := make(chan os.Signal, 1)
25-
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
24+
return serve(contextWithSigterm(ctx), e, m, config)
25+
}
26+
27+
func serve(ctx context.Context, e *echo.Echo, p *echo.Echo, config *config.Config) error {
28+
g, gCtx := errgroup.WithContext(ctx)
2629

2730
go func() {
28-
<-done
29-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
31+
<-gCtx.Done()
32+
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
3033
defer cancel()
31-
_ = e.Shutdown(ctx)
32-
_ = m.Shutdown(ctx)
34+
_ = e.Shutdown(shutdownCtx)
35+
_ = p.Shutdown(shutdownCtx)
3336
}()
3437

35-
logrus.Infof("server listening on %s", config.ListenAddr)
36-
logrus.Infof("metrics listening on %s", config.Metrics.ListenAddr)
37-
38-
return serve(e, m, config)
39-
}
40-
41-
func serve(e *echo.Echo, p *echo.Echo, config *config.Config) error {
42-
g := new(errgroup.Group)
4338
g.Go(func() error {
4439
if config.Tls.Disable {
4540
if err := e.Start(config.ListenAddr); err != nil && !errors.Is(err, http.ErrServerClosed) {
@@ -52,12 +47,17 @@ func serve(e *echo.Echo, p *echo.Echo, config *config.Config) error {
5247
}
5348
return nil
5449
})
50+
5551
g.Go(func() error {
5652
if err := p.Start(config.Metrics.ListenAddr); err != nil && !errors.Is(err, http.ErrServerClosed) {
5753
return err
5854
}
5955
return nil
6056
})
57+
58+
logrus.Infof("server listening on %s", config.ListenAddr)
59+
logrus.Infof("metrics listening on %s", config.Metrics.ListenAddr)
60+
6161
return g.Wait()
6262
}
6363

@@ -66,3 +66,20 @@ func registerDefaultRoutes(e *echo.Echo) {
6666
return c.Render(http.StatusOK, "index.html", nil)
6767
})
6868
}
69+
70+
func contextWithSigterm(ctx context.Context) context.Context {
71+
ctxWithCancel, cancel := context.WithCancel(ctx)
72+
go func() {
73+
defer cancel()
74+
75+
signalCh := make(chan os.Signal, 1)
76+
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
77+
78+
select {
79+
case <-signalCh:
80+
case <-ctx.Done():
81+
}
82+
}()
83+
84+
return ctxWithCancel
85+
}

0 commit comments

Comments
 (0)