Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no way to set background context of rpc server #172

Closed
stevvooe opened this issue Apr 21, 2015 · 10 comments
Closed

no way to set background context of rpc server #172

stevvooe opened this issue Apr 21, 2015 · 10 comments

Comments

@stevvooe
Copy link
Contributor

There doesn't seem to be a way to control the root context of the server. For example, it would be nice to have at least something like the following:

ctx := MySpecialContext()
s := NewServer(WithContext(ctx))

Such that the context passed to an implemented rpc method descends from the above:

func (mg *myGreeter) SayHello(context.Context, *HelloRequest) (*HelloReply, error) {}

Following from that, there doesn't seem to be a way to "prepare" a context using headers. One could imagine a per-request context preparation that incorporates header metadata (ie spans, auth, metrics, etc.) at the entire sever-level.

I may be missing API details in the documentation.

@dsymonds
Copy link
Contributor

Contexts are request-scoped (see http://blog.golang.org/context). What would it mean for a server-wide context? If something is server-wide, it doesn't belong in a context.

@stevvooe
Copy link
Contributor Author

@dsymonds The blog post outlines other uses as "cancelation signals, and deadlines across API boundaries to all the goroutines involved in handling a request". Not all of these boundaries may be exclusive to the request scope.

Imagine an API that could be used as part of a request-scoped server or client-side:

func GetStuff(ctx context.Context) (Stuff, error) { ... }

This method may use something in context but it doesn't care whether or not the context is request-scoped. Subsequently, we may configure the default resources used by GetStuff in a background context that then may be replaced at request time with something more specific to the context.

There may also be information setup in the background context that is service-wide but doesn't make sense to pass explicitly, such as loggers and tracing facilities.

@dsymonds
Copy link
Contributor

Either you misread, or the blog post needs clarifying. That "API boundaries" reference does not suddenly let contexts escape from being request scoped. @Sajmani

@stevvooe
Copy link
Contributor Author

@dsymonds That is a blog post I have read several times. I understand that the values are request scoped. There are cases where one wants to control the instantiation of the background context. Here is an example, written by you.

@dsymonds
Copy link
Contributor

internal.BackgroundContext is not a good counter example. It is an edge case that is somewhat unfortunate and largely dictated by how App Engine works. It is not a model for how to otherwise write good Go APIs. If I were to redo everything in the entire Go App Engine ecosystem then that function may not even exist.

@stevvooe
Copy link
Contributor Author

@dsymonds How would you accomplish the same task now?

@iamqizhao
Copy link
Contributor

I have not seen a strong reason to have a server-scoped context so far. Close this now.

@stevvooe
Copy link
Contributor Author

stevvooe commented May 1, 2015

@iamqizhao @dsymonds Any insight implementing similar functionality to internal.BackgroundContext? A little more advice than "don't do that" would be helpful.

Allowing one to set the server context provides a mechanism to control the background context used in requests. The main reasoning behind requiring a server-scoped context is to avoid having different ways of transmitting contextual values depending on the use case.

@ilius
Copy link

ilius commented Apr 18, 2018

I would say adding a context.SetBackground(ctx) feature would allow a process-scoped default context (not server-scoped or client-coped, since there is no boundary between server and client in microservice infra, most of our servers are also clients for other servers)

The same way that you can set http.DefaultClient or http.DefaultTransport in your own program (typically init func, or main), and that becomes the default for that process / program.

@menghanl
Copy link
Contributor

@ilius You can append content to context using interceptor: https://godoc.org/google.golang.org/grpc#UnaryInterceptor

@lock lock bot locked as resolved and limited conversation to collaborators Oct 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants