Provide a global context.Context #905
Unanswered
Southclaws
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a quick question about best practices around contexts with fx.
Many components in a typical app are long-running processes: HTTP servers, message queues, email clients, database clients, etc. these libraries often construct with a context.
Up until now, I've just been lazy and constructed these with
context.Background()
when fx.Providing them to the app.Now, I know there are short-lived contexts in the OnStart/OnStop hooks, and my understanding is that these are purely for timeouts during start/stop processes. I had confusion about this before and was using the OnStart context in a HTTP server.
As with most apps, I have a top-level root context hooked up to OS signals, so a sigterm gracefully shuts down the program (for redeploys, etc) my question is: would it make sense to
fx.Provide
this top-level context and then add it as a dependency to any components that may use a context for graceful shutdowns?One specific use-case I have in mind is
http.Server.BaseContext
so all request handlers'r.Context()
will inherit from this and thus facilitate graceful connection termination if the program receives a sigterm.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions