you are viewing a single comment's thread
view the rest of the comments
[–] 49 points 1 year ago (3 children)

It’s safe to assume that any non-trivial program written in Go is multithreaded

  • source
  • parent
  • hideshow 6 child comments
  • [–] 16 points 1 year ago (2 children)

    And yet: You’ll still be limited to two simultaneous calls to your REST API because the default HTTP client was built in the dumbest way possible.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 1 year ago (1 child)

    The client object or the library?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago

    … Is this a trick question? The object, provided by the library (net/http which is about as default as they come) sets “DefaultMaxIdleConnsPerHost” to 2. This is significant because if you finish a connection and you’ve got more than 2 idles, it slams that connection close. If you have a lot of simultaneous fast lived requests to the same IP (say a load balanced IP), your go programs will exhaust the ephemeral port list quickly. It’s one of the most common “gotchas” I see where Go programs work great in dev and blow themselves apart in prod.

    https://dev.to/gkampitakis/http-connection-churn-in-go-34pl is a fairly decent write up.

  • source
  • parent
  • [–] 7 points 1 year ago (1 child)

    I absolutely love how easy multi threading and communication between threads is made in Go. Easily one of the biggest selling points.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (1 child)

    Key point: they're not threads, at least not in the traditional sense. That makes a huge difference under the hood.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago* (1 child)

    Well, they're userspace threads. That's still concurrency just like kernel threads.

    Also, it still uses kernel threads, just not for every single goroutine.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (1 child)

    What I mean is, from the perspective of performance they are very different. In a language like C where (p)threads are kernel threads, creating a new thread is only marginally less expensive than creating a new process (in Linux, not sure about Windows). In comparison creating a new 'user thread' in Go is exceedingly cheap. Creating 10s of thousands of goroutines is feasible. Creating 10s of thousands of threads is a problem.

    Also, it still uses kernel threads, just not for every single goroutine.

    This touches on the other major difference. There is zero connection between the number of goroutines a program spawns and the number of kernel threads it spawns. A program using kernel threads is relying on the kernel's scheduler which adds a lot of complexity and non-determinism. But a Go program uses the same number of kernel threads (assuming the same hardware and you don't mess with GOMAXPROCS) regardless of the number of goroutines it uses, and the goroutines are cooperatively scheduled by the runtime instead of preemptively scheduled by the kernel.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago*

    Great details! I know the difference personally, but this is a really nice explanation for other readers.

    About the last point though: I'm not sure Go always uses the maximum amount of kernel threads it is allowed to use. I read it spawns one on blocking syscalls, but I can't confirm that. I could imagine it would make sense for it to spawn them lazily and then keep around to lessen the overhead of creating it in case it's needed later again, but that is speculation.

    Edit: I dove a bit deeper. It seems that nowadays it spawns as many kernel threads as CPU cores available plus additional ones for blocking syscalls. https://go.dev/doc/go1.5 https://docs.google.com/document/u/0/d/1At2Ls5_fhJQ59kDK2DFVhFu3g5mATSXqqV5QrxinasI/mobilebasic

  • source
  • parent