10 ms·
Context Control in Go
- j1elo 3y agoThis touches tangentially on a very interesting idea apart from contexts, at least for me as I've been recently learning Go: > it’s an anti-pattern for libraries to start their own goroutines. Best practices dictate that you should perform your work synchronously and let the caller decide if they want it to be asynchronous. This is something I had not discovered yet, probably because it's just a "common knowledge" thing that doesn't get explained in the Go tutorials, but regardless seems like a good idea in general. It also coincidentally threads well with something I read yesterday: The bane of my existence: Supporting both async and sync code in Rust [1]. While not being well versed at all in Rust, I constantly had precisely this same thought: why not make a synchronous library by default, then let the application choose whether it wants to use it as-is, or to put an async runtime on top of it? It only makes sense to me, and I would apply this best practice from Go if I was trying to make a Rust library. Especially given that in Rust there is no "official" standard async runtime, so I believe that authors ought to not assume which runtime end users should be forced to depend on. [1]: https://news.ycombinator.com/item?id=39061839 https://news.ycombinator.com/item?id=39061839
- jeffbee 3y agoI agree with not forcing an execution model on your callers, if you can avoid it. I also try to extend the rule to be as flexible as possible for callers. For example, in C++, I don't like to see a function that returns a container. I prefer a function that accepts an output iterator as a parameter.
- deleted 3y ago[deleted]
- Nullabillity 3y agoThere are two different questions here: 1. Should the code be async-aware? That is, should it be able to yield to other tasks? 2. Should the code launch background tasks? 1 is a cross-cutting concern, in a cooperative multitasking environment, anything that is unable to yield is, well, preventing other tasks from running. In Go, 1 is always true, and implemented by the runtime itself. In Rust, this means using async I/O, like preferring tokio over std. 2 is what the advice is about. The Rust equivalent here would be tokio::spawn. Writing async-independent Rust code requires you to tackle 1, maybe by writing a trait for all the I/O actions that you require from your executor, which could return blocking futures if you select to run synchronously. That said, blocking also prevents you from using async for structured concurrency in the library implementation, which may or may not be a big deal for your use-case.
- disintegrator 3y agoI think it's totally fine for libraries to spin up goroutines internally. My interpretation is that the library's public interface should appear to be synchronous. As a contrived example: package main import ( "context" "example.com/spider" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() _, _ = spider.Crawl(ctx, "https://en.wikipedia.org") } In this case `Crawl` is a blocking call and, under the hood, it may well spin up a pool of goroutines to crawl a site. It's also really nice that context is available to tie the lifetimes of the main program (goroutine) to child goroutines without coloring functions (like with async-await). I used to work with the Go pubsub client (https://pkg.go.dev/cloud.google.com/go/pubsub https://pkg.go.dev/cloud.google.com/go/pubsub) a lot and that has a whole bunch of scheduling and batching functionality handled in goroutines and on the outside you're calling `topic.Publish(ctx, &pubsub.Message{Data: []byte("payload")})`.
- xyzzy_plugh 3y agoIf you're willing to plumb through all the knobs imaginable then this is a fine approach. But if spider.Crawl just ran unbounded or with a fixed bound it could trivially become a huge headache. There are many patterns in Go that are preferable to just starting a bunch of concurrent work. For example the pubsub client has options to disable batching and limit in flight connections.
- disintegrator 3y agoIndeed, it's down to the library API to give you the right knobs to control for concurrency. It says more about the quality of the library if it mismanages goroutines than it does about whether or not libraries should use goroutines at all.
- twic 3y ago> There are many patterns in Go that are preferable to just starting a bunch of concurrent work. I have not worked in Go for several years, but i remember that when i did, more experienced people told me that this was exactly what you were supposed to do in Go. That where in another language you might set up a queue and a threadpool and so on, in Go, you should just spawn a load of goroutines, and let the runtime sort it out. Is this no longer the canonical approach?
- randomdata 3y ago> This is something I had not discovered yet No doubt because the Go team disagrees. They have been abundantly clear that, from their point of view, goroutines should be used and even used as part of the public API when it makes sense. That said, it is still probably really good advice for newcomers who won't have a good understanding for the cases where it does make sense, and especially because goroutines are the shiny thing every newcomer wants to play with and will try to find uses that don't make sense just to use it. As a rule, you don't want to force the caller into things they might not want. In the vast majority of cases, a synchronous API is what you will want to give them as it is the most versatile. And, really, that's something that applies generally. For example, in the case of the common (T, error) pattern, you will want to ensure T is always useful even when there is an error. The caller may not care about the error condition. That's not you for, the library author, to decide. The fewer assumptions you can make about the caller the better.
- tsimionescu 3y ago> For example, in the case of the common (T, error) pattern, you will want to ensure T is always useful even when there is an error. The caller may not care about the error condition. This applies to maybe 0.1% of functions. The overwhelming majority of functions, in the Go stdlib as well as real projects, that return (T, error) return an empty meaningless value for the error cases.
- randomdata 3y agoNot my experience. Some very early code did not recognize this, but since then pretty much everyone has come to agree that values should always be useful. If you are writing a function today, there is no reason to not observe this. In practice, that typically means returning the zero value. To which idioms suggest that zero values should be useful. Rob Pike's Go Proverb[1] even states: "Make the zero value useful." Most commonly when returning (T, error) that zero value is nil. In Go, nil is a useful value! If the caller wants to observe the error state, great. But it is needlessly limiting if you force it upon them. That is not for the library author to decide. [1] https://go-proverbs.github.io https://go-proverbs.github.io
- acaloiar 3y ago> it’s an anti-pattern for libraries to start their own goroutines. Best practices dictate that you should perform your work synchronously and let the caller decide if they want it to be asynchronous. This line reminded me that we all need to beware of the "anti-pattern" police. Some develpers use this term effectively by explaining precisely why something is an "anti-pattern". But more often it's used as a way to shut down conversation and any actual critical thinking. There's a lot of nuance behind what makes something an "anti-pattern", and simply declaring something an anti-pattern isn't enough. "This is an anti-pattern and this is why ..." is enough. But I still avoid using the term regardless. FYI I'm not saying the author is the anti-pattern police, but it does sound like they've found themselves on the police's radar.
- chemcube 3y agoHey. Author here. Yeah, the Go style guide at Google (the setting for the dcode review) is quite prescriptive. I don't mind labeling things "anti-patterns". It doesn't mean there's a law against using said pattern (if you're in a healthy org). It just means you should be able to justify why you go against the guideline. The whole reason I was comfortable attempting that approach in the first place is because I had successfully used that pattern before and was able to provide a rationale. It just happens that this case differed enough that using this pattern would've been unwise. You're right that things shouldn't just be classed as an anti-pattern without explaining why. And that's enforced in most Go-heavy orgs at Google. If you're gonna say something is bad without providing an explanation and linking to the relevant sources, the comment is likely to be closed or argued. I didn't provide an explanation for why this is considered an anti-pattern in this post because that wasn't the topic. But it appears most persons here are interested in that, so maybe I'll write a post on that next week :)
- throwaway894345 3y ago>> it’s an anti-pattern for libraries to start their own goroutines The specific argument here is that by writing sync functions, the library is more abstract because the caller can decide whether to run the function sync or async. I agree with this, but there are lots of areas where we could issue guidance to make libraries more abstract. For example, instead of a library function which returns a pointer to allocated memory (e.g., `NewFoo() Foo`) we should write functions which take pointers to memory and the caller can figure out whether to allocate them on the stack or the heap (e.g., `NewFoo(out Foo)`). I'm not advocating for this as a general rule of thumb because writing that kind of code in Go would not be very ergonomic, but there's a lot of performance-sensitive code even in the standard library that is written that way. Another example would be 'inversion of control', wherein functions take interface parameters and callers decide what implementation to pass in.
- divan 3y agoIt might also come from the common sense of using the CSP model (goroutines and messages, basically). Being asynchronous or synchronous is not the property of the function; it's the caller's decision. So every function is "synchronous" and it's up to the caller to decide when to spin it into the background and what for. As others commented, it doesn't preclude from you using goroutines in libraries if you _really need to_. It is just important to remember that it's not obvious to the caller. In light of this "common sense", async/await concurrency concept doesn't make any sense. Why would function dictate how exactly it should be called by a caller? Is "watching TV" an async or sync action? Depends on the caller – whether they put all their attention into this action or doing it "in the background" while performing other tasks. It's not an inherent property of the "whatching TV" function. I have no idea why so many people think that async/await is a good idea for expressing concurrent systems.
- NorthTheRock 3y agoI've always seen this as the exact opposite view - from go's concurrency model, every function is "synchronous" so the caller is not given a choice, if they want to run it asynchronously they have to create a new thread, then if they care about the result deal with inter-thread communication. With async/await, you're explicitly giving control to the caller to decide, you can await this promise now and have the thread treat it as synchronous, you can spawn a new task to run it in the background, or you can join it with other promises where you don't care about order and await for the results of the group.
- divan 3y agoInteresting. I see two different aspects here: 1) Mental model. My claim comes from the firm belief that the more code is aligned with how we think, the easier it is to reason about the code. I naturally think about actions as they are not async or sync by nature – rather, it's me who's in charge of how the action is going to be executed (back to my "watching TV" example). Human attention here serves as an analogy to utilizing the logical CPU core during runtime. 2) Performance consideration. What you described indeed can work, too, but it comes at a cost. With Go, yes, you have to handle async results yourself (if you care about results), but you now understand the price of this and can make better judgments of the code and complexity and have better performance overall.
- JohnCClarke 3y agoTBH: Context feels like a wart to me. It works, but it's not elegant. Golang has an aversion to thread^H^H^H^H^H^Hgoroutine local storage. Instead it provides this kludgey experience. I really feel that Golang V.2 should invent a better, native, way of controlling threads of execution.
- deleted 3y ago[deleted]
- gray_-_wolf 3y agoI always thought that context should just be a goroutine local thing always available, automatically inherited when `go` is executed (obviously with the option to set an explicit one).
- mariusor 3y agoI think Jonathan Blow's programming language, Jai, has an implicit context available to any function. However in Jai the context has a lot of implicit functionality, on the top of my head at least logging plumbing, and allocator plumbing.
- deleted 3y ago[deleted]
- swdunlop 3y agoI think of contexts as being Go's answer to dynamic variables in earlier languages, like Lisp, and less like thread local storage (like Pthreads). Much like how Go works with errors, explicit is favored over implicit -- being able to see the context pass through, and whether a function expects a context, tells you a lot about the function you are about to call. If a function does not take a context, you know it probably cannot be interrupted, just like when a function does not return an error, you know it should not fail. In my work projects, this is also a cue that the function does not do any logging since we always carry a zerolog.Logger in our contexts enriched with trace information about the request and handler. This also makes life easier for me as a reviewer -- I can see the context passing, I can spot when there is a bad pattern, like retaining a context, or failing to handle an error. It does not require me to maintain a detailed mental map of which functions employ dynamic variables or can throw exceptions.
- kevincox 3y agoThis has a lot of interesting accidental rebuttals to some "features" of go. > If you’re not in an entry-point function and you need to call a function that takes a context, your function should accept a context and pass that along. This is in contrast to the fact that Go's cheap threading means that you don't need to colour your functions with async or not async. But this quote that you sort of have to do this with context or no context. It isn't quite as bad as you can "skip steps" such as passing a context to a callback directly rather than needing the function that calls the callback to support contexts. But still in general your functions do have colour if you want to use contexts properly. > most seasoned Go devs would leap out of their seats to tell you it’s an anti-pattern for libraries to start their own goroutines. If goroutines are so cheap then why not let the library spawn them. As long as the interface doesn't reveal if they are being used or not it shouldn't matter.
- icholy 3y ago> This is in contrast to the fact that Go's cheap threading means that you don't need to colour your functions with async or not async. But this quote that you sort of have to do this with context or no context. This doesn't have the usual problems associated with colored functions though (ie, calling async functions from non-async functions or vice-versa). If you don't need cancellation, pass a `context.Background()` and you're done. > If goroutines are so cheap then why not let the library spawn them. As long as the interface doesn't reveal if they are being used or not it shouldn't matter. Agreed. providing a synchronous API is what's important.
- mariusor 3y ago> you don't need to colour your functions with async or not async. Context is useful for synchronous functions also, it has nothing to do with async.
- jerf 3y agoYou can't extend the concept of "coloring" a function to all possible environment and parameters a function needs to execute. That's not because that's a useless concept; I actually find it a very important concept to be thinking about and I often explicitly think in terms of trying to minimize the size of such things in my code. But you can't extend the "coloring" concept that far because you've stretched it all out of shape at that point, into an entirely different concept. All code in all languages everywhere is going to have state that flows through some combination of function parameters through the program and have certain requirements for that state without which the functions (methods/whatever) will not run. Coloration is a very particular very strong instance of such things that is so strong it causes its own special effects and imposes very special constraints on the code. Generally if you need to call something that wants a context but you don't have one, you just pass in the trivially-obtained "context.Background()" and move on. Nowhere near the level of blockage as a color issue. "If goroutines are so cheap then why not let the library spawn them." It's not about cost, it's about software engineering, and it's a particular antipattern you may not know about if you're not in the community. As I said in another post, many libraries "helpfully" spawn goroutines to do a thing and offer a promise-like interface to the results. This is the core antipattern being referred to, which I've seen quite a lot. The resulting API is complexified relative to simply having a function that takes parameters and returns results. If you write such a complex API, an end-user of that API can't uncomplexify it. However, if you write the simple, normal function, an end-user of your API who does want that additional functionality can trivially add it, and moreover, they can add it in whatever other combination of things they may want, e.g., perhaps your library is part of a three-step pipeline you choose to run in its own goroutine, or some other complex threading setup you need. It is better for a library to provide the simple "synchronous" API than to try to guess and possibly even as a result forstall the real setup you need. It isn't a hard-and-fast rule that libraries must never spawn goroutines, it's a particular set of antipatterns being referred to.
- memset 3y agoQuestion about contexts in general: is there a way to "guarantee" that contexts are used correctly by whoever is consuming my context? For example, if I call http.NewRequestWithContext(), how do I, as the caller, know that http is doing the "right thing" with that value, rather than ignoring it? In the OP's example, intuitively it seems like an explicit Stop() function gives the caller explicit control of when to stop and that anyone implementing a Worker (if Worker were an interface) would know that the Stop() function should do cleanup. However, if I only pass in a context when calling Run(), wouldn't it be easy for someone to ignore a deadline?
- deleted 3y ago[deleted]
- badrequest 3y agoMost of the time you accept a context because some downstream function requires it (i.e. I'm writing an HTTP client, and the `net/http` std lib functions require context for some part of it). You can have general confidence that the standard library will respect things like context deadlines, even if the wrappers that invoke that don't necessarily.
- deleted 3y ago[deleted]
- rollulus 3y ago> most seasoned Go devs would leap out of their seats to tell you it’s an anti-pattern for libraries to start their own goroutines. Best practices dictate... Then I'm afraid that either the author is not familiar with the standard library, or that it is not built according the "best practices". Rant: the phrase "best practice" increasingly irritates me. Basically it has become a synonym for "my own opinion just trust me". It's like the Jedi hand gesture to force your beliefs onto someone to end a discussion.
- diggan 3y ago> Basically it has become a synonym for Its been a thing for a long time! As far as I know, Feynman first put it into words (regarding science, but applies equally to software engineering) in 1974: > In the South Seas there is a cargo cult of people. During the war they saw airplanes land with lots of good materials, and they want the same thing to happen now. So they've arranged to imitate things like runways, to put fires along the sides of the runways, to make a wooden hut for a man to sit in, with two wooden pieces on his head like headphones and bars of bamboo sticking out like antennas—he's the controller—and they wait for the airplanes to land. They're doing everything right. The form is perfect. It looks exactly the way it looked before. But it doesn't work. No airplanes land. So I call these things cargo cult science, because they follow all the apparent precepts and forms of scientific investigation, but they're missing something essential, because the planes don't land. We programmers usually call it "cargo culting", blindly following "best practices" and "design patterns" without any deeper understanding of why and when those should be applied.
- j1elo 3y agoTo be honest for me when someone talks about "best practices" I expect it to be actual best practices that usually get documented somewhere that is a documentation resource well regarded by the "community" of that tool or language. So if it happened (no idea) that the author here was taking this "best practice" out of their own ass... well, then yeah I'd agree that's just an opinion and not an actual, community-agreed-upon, very common and very well documented "best" practice in the sense that I usually regard as useful and reliable. Nevertheless, I agree with the author that the concept of "don't return immediately from your API function, instead block until the function's work has been completely done, and only then return", seems to me as a valid and quite good idea. Regardless of how many internal goroutines might have been used in order to comply with this behavior, the external surface should look like a blocking call. If any caller doesn't want to block their thread on it, they in turn can always run it in a goroutine.
- italicmew123 3y agoRule 3: Don’t store contexts - What about this: https://github.com/golang/go/blob/master/src/net/http/client.go#L668C4-L668C24 https://github.com/golang/go/blob/master/src/net/http/client... req = &Request{ Method: redirectMethod, Response: resp, URL: u, Header: make(Header), Host: host, Cancel: ireq.Cancel, ctx: ireq.ctx, } Isn't this considered storing the context?
- maxwellg 3y agoYep - (my understanding is) the Go HTTP stdlib module predates the concept of context in Golang, so the implementation was bolted on to ensure backwards compatibility. NewRequestWithContext was only added in Go 1.13 [1]. Previously, requests were cancelled manually with CancelReqest [2]. This is an unfortunate wart of the language - it means it's very easy to accidentally spin up a new Request which doesn't inherit the parent context by calling NewRequest instead. And adding the context via the builder pattern means it's possible to introduce the storage bugs described in the article. My preferred way to consume a context would be to take it in when the work is actually about to be performed - e.g. client.Do(ctx, reqest) 1 - https://pkg.go.dev/net/http#NewRequestWithContext https://pkg.go.dev/net/http#NewRequestWithContext 2 - https://pkg.go.dev/net/http#Transport.CancelRequest https://pkg.go.dev/net/http#Transport.CancelRequest
- __turbobrew__ 3y agoYea I don’t really agree with the rule that goroutines shouldn’t be started in libraries either. For example, say you are building a library to send metrics to a metrics collector. For me it makes sense that your metrics library contains a buffer of metrics which it batches data together and sends to the metrics collector asynchronously. This would be implemented as having a library goroutine which batches and sends metrics data. I guess in theory your library could have a ‘Flush’ method and then if the application wants async flushing the application can start a goroutine which periodically calls flush. But then the application needs to know the ideal frequency to flush, how to handle failures to flush, how to backoff, etc. These things are probably better done by the library writer.
- Philip-J-Fry 3y ago
- pianoben 3y agoI don't really agree that it's an antipattern for a library to create a goroutine. If you consider that starting a worker is, in a sense, an entry point, you can even claim to otherwise conform to The Rules. Why would I want to make my caller think about scheduling library internals? As long as I'm managing resources appropriately, and exposing knobs as necessary, what's the problem?
- matthewaveryusa 3y agoWhat about storing the context for lazy-loading? Like a client that's initialized in main() with a ctx, but starts async workers on first-use deeper in and across go routines?
- fl0ki 3y agoI find that combining context and errgroup, with due care, lets us approximate Structured Concurrency to great benefit. I just think more care is due than most people give. When a function returns, we should be able to trust that any goroutines it created have already terminated and will not have other side effects. This is important because Go doesn't enforce read/write thread-safety any other way, so we need it to be clear from the code when those reads/writes may happen. It's also important because side effects from those lingering routines could have other consequences, e.g. the retry for an IO operation could overlap with a past attempt, violating invariants in ways that would be really hard to reproduce and debug. This sounds really simple, why wouldn't you do it that way? Idiomatic use of errgroup encourages you to do it that way, but not everyone does it that way, and sadly not every project even uses errgroup in the first place. It's very common to see a routine observe cancellation and return immediately, even if it created its own goroutines which it can't guarantee have aborted yet. Aside, it's also sadly extremely common to see people reinvent errgroup badly with "error channels" that at best don't join the other routines and at worst deadlock them because they block on sending errors that nobody is receiving any more. That's why if you do this, you basically ban the `go` keyword and strictly use errgroup, even for routines which can't return errors. (WaitGroup can do this too, but it's harder to use right, because the Add/Done count have to add up exactly and there's no enforcement that they do). If you do this right, then it shouldn't matter whether a function creates goroutines to help with its work, such as timeout channels or parallel processing or what have you. What matters is that the function still acts like a synchronous one from the outside. The worst I've seen is when people know they have to use errgroup, but they create one large errgroup in main and pass it around as a mutable argument to everything to add more tasks to it. They don't understand that when it's used correctly, it also nests and encapsulates entirely, so it's never the argument to or return from a function. Of course it gets more complicated if an object has long-running goroutines that outlive any particular function. Then you need to call more functions just to create those wait points. For example, it's not enough to cancel a database cursor as a context, you should still block on closing it, otherwise its own routines can still be running when you go back and start another operation. Again, sadly all too common. Caveat 1: errgroup only returns the first error, which for many routines is just "cancelled". That's not useful, and it takes the place of what could have been a real error. I suppress errors like that, so that actual cleanup errors, if any, are the ones surfaced. Caveat 2: errgroup doesn't trap panics, if you want the panic to be surfaced as a neatly packaged error, you have to install your own handler. Every project I have has its own simple version of this, and I've seen many other projects come to the same conclusion.
- andrewstuart2 3y agoI'd argue here that it's not a problem of context storage, it's a problem of not ignoring cancellation in certain situations. Since context, for better or worse, has two purposes, you may still want a lot of the request-scoped data for later operations after the initial timeout/deadline is done. And since context has a standardized bag of data, it's more future-proof to just keep using the context and its data rather than, say, extracting the data you know about today (like trace/span ids) and storing that for later use. Fortunately, following appropriate patterns here got a lot easier with 1.21 and the addition of `context.WithoutCancel`. If you're going to store a context for later use, since there's potentially e.g. tracing data you still want to keep, make sure you appropriately `context.WithoutCancel` to keep the data without keeping the original deadline.
- SPBS 3y agoIt's not a hard rule that context should not be struct fields. See https://github.com/golang/go/issues/22602 https://github.com/golang/go/issues/22602 (context: relax recommendation against putting Contexts in structs) "Right now the context package documentation says > Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx: [...] This advice seems overly restrictive. @bradfitz wrote in that issue: > While we've told people not to add contexts to structs, I think that guidance is over-aggressive. The real advice is not to store contexts. They should be passed along like parameters. But if the struct is essentially just a parameter, it's okay. I think this concern can be addressed with package-level documentation and examples."
- neonsunset 3y agoMaybe C# got me spoiled with CancellationToken which seems like a nicer API. And, perhaps, synchronization context as well if you are writing a GUI application and need a render thread, to make sure you yield to the right one. Though if that's not the preferred pattern, publishing a message to a channel on one end and then reading them from another is always an option.
- metaltyphoon 3y agoJust wished ConfigureAwait was not true by default
- valcron1000 3y agoIs it correct to assume that the "cancellation" part of `Context` is similar to C#'s `CancellationToken`? Also, it looks like it allows to pass some "implicit parameters" to another function. If that's the case, why does Go have a single entity performing two roles: cancellation + implicit parameter passing? I would expect to have these things separated.
- valcron1000 3y agoI also find it curious that a language with a preemptive scheduler requires manual "yield" points by constantly checking on the context if the current function should stop executing.
- seneca 3y agoGo contexts are something that I found confusing for a while. I had assumed they were much more complicated than they actually are. I read the context section in Jon Bodner's Learning Go and realized they are actually pretty simple and I was just over thinking them.