Live data from Hacker News

Goroutines and APIs

blog.gnoack.org

11–20 of 31 posts

Re: Goroutines and APIs

#11

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

The Raspberry Pi handles the load well after fixing that configuration mistake. (I still need to investigate to understand in detail what went wrong there...)

Re: Goroutines and APIs

#12
I think that it is universal advice for libraries: do not do multithreading inside, let the client code do it and/or pass necessary constructs.

Simple example for Java: do not create executorservice instances inside of a library, but allow client code to pass them.

Ayende wrote much better than me long time ago https://ayende.com/blog/159201/multi-threaded-design-guideli...

Re: Goroutines and APIs

#13

The 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

The net/http library is the caller in this case, so that would be consistent with the rule that callers should start goroutines.

Request handlers are a bit of a special case too, in that they are a framework for dispatching tasks to be worked on; what is main() for a command line program is the request handler for a webserver. It seems fair that there is some concurrency coordination happening at the top level.

Re: Goroutines and APIs

#14
"threads" (goroutines) and channels are the new gotos. They tend to make a codebase more difficult to reason about. The more you add, the more you tend to get spooky action at a distance.

Re: Goroutines and APIs

#15

"threads" (goroutines) and channels are the new gotos. They tend to make a codebase more difficult to reason about. The more you add, the more you tend to get spooky action at a distance.

These are hardly new concepts, nor are there any more appropriate mechanisms for modeling dependencies between multiple concurrent actors. If anything, the traditional shared memory nightmare has proven to be the goto in the room.

Re: Goroutines and APIs

#16

"threads" (goroutines) and channels are the new gotos. They tend to make a codebase more difficult to reason about. The more you add, the more you tend to get spooky action at a distance.

Unfortunately Moore's law is no longer expressing itself in terms of single threaded performance. So the only way to take advantage of the faster CPUs that are being released is to write multi-threaded code.

Many alternatives to channels (mutexes, atomics, semaphores, condition variables) have just as much if not more chance of turning the codebase into a mess. But yes I agree that if performance-wise you can handle being single-threaded, then do that, because it's much simpler.

Re: Goroutines and APIs

#17

"threads" (goroutines) and channels are the new gotos. They tend to make a codebase more difficult to reason about. The more you add, the more you tend to get spooky action at a distance.

These are hardly new concepts, nor are there any more appropriate mechanisms for modeling dependencies between multiple concurrent actors. If anything, the traditional shared memory nightmare has proven to be the goto in the room.

Funny you should say actors. While some people will equate the two, I find Erlang actors easier to reason about than go channels. Though that has everything to do with monitors and links, to tie dependent goroutines/processes together.

Re: Goroutines and APIs

#18

"threads" (goroutines) and channels are the new gotos. They tend to make a codebase more difficult to reason about. The more you add, the more you tend to get spooky action at a distance.

And, like the goto statement, surely there will be scolds warning everyone not to use these anywhere instead of treating them like any other language feature to be used with care and as sparingly as possible.

Re: Goroutines and APIs

#19
Some operations can use green threads, while others need kernel threads. Last I tried (~2015) Go would spawn unlimited kernel threads, so function callers had to know whether a call required a kernel thread or not, and rate-limit appropriately.

Is this still the case? How does modern Go handle operations which require kernel threads?

Re: Goroutines and APIs

#20

Some operations can use green threads, while others need kernel threads. Last I tried (~2015) Go would spawn unlimited kernel threads, so function callers had to know whether a call required a kernel thread or not, and rate-limit appropriately. Is this still the case? How does modern Go handle operations which require kernel threads?

Which operations need kernel threads? There is runtime.LockOSThread if you need underlying thread affinity, but I'm not aware of any other mechanism to interact with the underlying threads of execution.
Post reply on HN