99 "os"
1010 "time"
1111
12+ "github.com/steveiliop56/ding"
1213 "github.com/tinyauthapp/tinyauth/internal/controller"
1314 "github.com/tinyauthapp/tinyauth/internal/middleware"
1415 "github.com/tinyauthapp/tinyauth/internal/model"
@@ -80,9 +81,9 @@ func (app *BootstrapApp) runListeners() (chan error, error) {
8081 return nil , fmt .Errorf ("failed to get listener function: %w" , err )
8182 }
8283
83- app .wg .Go (func () {
84- lec <- listenerFunc ()
85- })
84+ app .ding .Go (func (ctx context. Context ) {
85+ lec <- listenerFunc (ctx )
86+ }, ding . RingNormal )
8687 }
8788
8889 return lec , nil
@@ -125,7 +126,7 @@ func (app *BootstrapApp) calculateListenerPolicy() []Listener {
125126 return l
126127}
127128
128- func (app * BootstrapApp ) listenerFromType (listenerType Listener ) (func () error , error ) {
129+ func (app * BootstrapApp ) listenerFromType (listenerType Listener ) (func (ctx context. Context ) error , error ) {
129130 switch listenerType {
130131 case ListenerHTTP :
131132 return app .serveHTTP , nil
@@ -138,7 +139,7 @@ func (app *BootstrapApp) listenerFromType(listenerType Listener) (func() error,
138139 }
139140}
140141
141- func (app * BootstrapApp ) serveHTTP () error {
142+ func (app * BootstrapApp ) serveHTTP (ctx context. Context ) error {
142143 address := fmt .Sprintf ("%s:%d" , app .config .Server .Address , app .config .Server .Port )
143144
144145 app .log .App .Info ().Msgf ("Starting server on %s" , address )
@@ -154,10 +155,10 @@ func (app *BootstrapApp) serveHTTP() error {
154155 Handler : app .router .Handler (),
155156 }
156157
157- return app .serve (listener , server , "http" )
158+ return app .serve (listener , server , ctx , "http" )
158159}
159160
160- func (app * BootstrapApp ) serveUnix () error {
161+ func (app * BootstrapApp ) serveUnix (ctx context. Context ) error {
161162 _ , err := os .Stat (app .config .Server .SocketPath )
162163
163164 if err == nil {
@@ -181,10 +182,10 @@ func (app *BootstrapApp) serveUnix() error {
181182 Handler : app .router .Handler (),
182183 }
183184
184- return app .serve (listener , server , "unix socket" )
185+ return app .serve (listener , server , ctx , "unix socket" )
185186}
186187
187- func (app * BootstrapApp ) serveTailscale () error {
188+ func (app * BootstrapApp ) serveTailscale (ctx context. Context ) error {
188189 app .log .App .Info ().Msgf ("Starting Tailscale server on %s" , fmt .Sprintf ("https://%s" , app .services .tailscaleService .GetHostname ()))
189190
190191 listener , err := app .services .tailscaleService .CreateListener ()
@@ -197,27 +198,23 @@ func (app *BootstrapApp) serveTailscale() error {
197198 Handler : app .router .Handler (),
198199 }
199200
200- return app .serve (listener , server , "tailscale" )
201+ return app .serve (listener , server , ctx , "tailscale" )
201202}
202203
203- func (app * BootstrapApp ) serve (listener net.Listener , server * http.Server , name string ) error {
204+ func (app * BootstrapApp ) serve (listener net.Listener , server * http.Server , ctx context. Context , name string ) error {
204205 shutdown := func () {
205- ctx , cancel := context .WithTimeout (context .Background (), model .GracefulShutdownTimeout * time .Second )
206+ // we use a new context for the shutdown since the main one is cancelled
207+ sctx , cancel := context .WithTimeout (context .Background (), model .GracefulShutdownTimeout * time .Second )
206208 defer cancel ()
207- err := server .Shutdown (ctx )
208- if err != nil &&
209- // With tailscale, the goroutine for shutting down the tailscale connection
210- // runs first and causes the connection the tailscale listener is running on to close
211- // first so, the shutdown fails
212- // TODO: add priority to the goroutine shutdowns
213- ! errors .Is (err , net .ErrClosed ) {
209+ err := server .Shutdown (sctx )
210+ if err != nil {
214211 app .log .App .Error ().Err (err ).Msgf ("Failed to shutdown %s listener gracefully" , name )
215212 }
216213 listener .Close ()
217214 }
218215
219216 go func () {
220- <- app . ctx .Done ()
217+ <- ctx .Done ()
221218 app .log .App .Debug ().Msgf ("Shutting down %s listener" , name )
222219 shutdown ()
223220 }()
0 commit comments