feat: manage background workers in the server lifecycle#96
Merged
Conversation
Add a Worker type and a Workers field on Config so callers can register long-running background tasks (e.g. a Pub/Sub listener) that share the server lifecycle. Workers run in their own goroutines, receive the shutdown-aware context, recover from panics, and log-and-continue on error. On shutdown they are drained within the graceful-shutdown budget.
A worker following the documented contract returns ctx.Err() on shutdown, which logged at ERROR level on every graceful shutdown. Skip context.Canceled/DeadlineExceeded, mirroring the existing http.ErrServerClosed handling in serve(). Also strengthen worker tests: table-driven TestRunWorker (incl. a context-cancellation case), goleak.VerifyNone on the serve tests, and deterministic testing/synctest for the drain timing tests.
AdrianCio
reviewed
Jun 15, 2026
Comment on lines
+397
to
+400
| err := s.Echo.Shutdown(shutdownCtx) | ||
|
|
||
| // Wait for workers to unwind, bounded by the same shutdown budget. | ||
| s.drainWorkers(shutdownCtx, &workers) |
Collaborator
There was a problem hiding this comment.
is this the right order or they should be the other way around? 🤔 I legit don't know 😬
Contributor
Author
There was a problem hiding this comment.
Yes (most likely) is. It first shuts down the possibility of new requests to this server, and then closes the workers. If we do it the other way around, new requests (that the worker might depend on) could come in while the workers are already disabled.
thzois
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a
Workertype and aWorkersfield onConfigso callers can register long-running background tasks (such as a Pub/Sub listener) that share the server lifecycle. Each worker runs in its own goroutine, receives the shutdown-aware context, recovers from panics, and logs-and-continues on error. On shutdown, workers are drained within the existing graceful-shutdown budget.Types of Changes
Review