Goroutines and APIs
blog.gnoack.org
Goroutines and APIs
1–10 of 31 posts
Re: Goroutines and APIs
#2If I had to have a guess what it says, it's probably something along the lines of:
* Don't require users of your API to start goroutines to use your API correctly.
If Goroutines need to be started for your API to work, start them in an init function, on first use, or via some other mechanism.
* If possible, avoid high goroutine fanout in your implementation.
If a single request to your API fans out to 1k goroutines, it's going to be a problem for a high-traffic user of your API. Especially if there are other APIs that make the same design choice. As ever, try to be parsimonious with resource consumption.
IMO, these are two good principles to live by, and not just in Go.
Re: Goroutines and APIs
#3There is really little excuse for a web server to buckle under pressure these days - hackernews sends a spike of traffic but not enough to kill even a moderately spec'ed server.
Re: Goroutines and APIs
#4The blog seems sadly unable to tolerate the load. I don't see an archived version of the post anywhere either. If I had to have a guess what it says, it's probably something along the lines of: * Don't require users of your API to start goroutines to use your API correctly. If Goroutines need to be started for your API to work, start them in an init function, on first use, or via some other mechanism. * If possible,…
Edit: See line 2927 at https://golang.org/src/net/http/server.go
Re: Goroutines and APIs
#5The blog seems sadly unable to tolerate the load. I don't see an archived version of the post anywhere either. If I had to have a guess what it says, it's probably something along the lines of: * Don't require users of your API to start goroutines to use your API correctly. If Goroutines need to be started for your API to work, start them in an init function, on first use, or via some other mechanism. * If possible,…
Doesn't the native net/http server spin up a goroutine for every request? Edit: See line 2927 at https://golang.org/src/net/http/server.go
Re: Goroutines and APIs
#6Earlier quoted context omitted.
Doesn't the native net/http server spin up a goroutine for every request? Edit: See line 2927 at https://golang.org/src/net/http/server.go
Yeah, but it doesn't spin up a thousand for every request. And it doesn't require you to start them. It starts them under the covers as part of the API.
* Don't require users of your API to start [additional] goroutines to use your API correctly.
Re: Goroutines and APIs
#7I can't read the article but hopefully it does not ironically present advice on writing webservers. There is really little excuse for a web server to buckle under pressure these days - hackernews sends a spike of traffic but not enough to kill even a moderately spec'ed server.
Re: Goroutines and APIs
#8Earlier quoted context omitted.
Yeah, but it doesn't spin up a thousand for every request. And it doesn't require you to start them. It starts them under the covers as part of the API.
Right. So: * Don't require users of your API to start [additional] goroutines to use your API correctly.
go yourpackage.YourApiCall()
in order to use the API correctly. That's just a guess, though.Re: Goroutines and APIs
#9Re: Goroutines and APIs
#10The blog seems sadly unable to tolerate the load. I don't see an archived version of the post anywhere either. If I had to have a guess what it says, it's probably something along the lines of: * Don't require users of your API to start goroutines to use your API correctly. If Goroutines need to be started for your API to work, start them in an init function, on first use, or via some other mechanism. * If possible,…
> Don't require users of your API to start goroutines to use your API correctly.
Actually, what I postulate in the article is different... The theory is that making the caller kick off the goroutines puts more control over concurrency into the hands of the caller, and the resulting API is simpler.
> Avoid high goroutine fanout in your implementation.
That's for sure. :)