Coder Social home page Coder Social logo

Comments (10)

stevvooe avatar stevvooe commented on June 19, 2024 1

@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.

from grpc-go.

dsymonds avatar dsymonds commented on June 19, 2024

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.

from grpc-go.

stevvooe avatar stevvooe commented on June 19, 2024

@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.

from grpc-go.

dsymonds avatar dsymonds commented on June 19, 2024

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

from grpc-go.

stevvooe avatar stevvooe commented on June 19, 2024

@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.

from grpc-go.

dsymonds avatar dsymonds commented on June 19, 2024

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.

from grpc-go.

stevvooe avatar stevvooe commented on June 19, 2024

@dsymonds How would you accomplish the same task now?

from grpc-go.

iamqizhao avatar iamqizhao commented on June 19, 2024

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

from grpc-go.

ilius avatar ilius commented on June 19, 2024

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.

from grpc-go.

menghanl avatar menghanl commented on June 19, 2024

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

from grpc-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.