TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Goroutines and APIs

106 pointsby gnoackover 5 years ago

7 comments

galkkover 5 years ago
I think that it is universal advice for libraries: do not do multithreading inside, let the client code do it and&#x2F;or pass necessary constructs.<p>Simple example for Java: do not create executorservice instances inside of a library, but allow client code to pass them.<p>Ayende wrote much better than me long time ago <a href="https:&#x2F;&#x2F;ayende.com&#x2F;blog&#x2F;159201&#x2F;multi-threaded-design-guidelines-for-libraries-part-i" rel="nofollow">https:&#x2F;&#x2F;ayende.com&#x2F;blog&#x2F;159201&#x2F;multi-threaded-design-guideli...</a>
评论 #21572483 未加载
asdfasgasdgasdgover 5 years ago
The blog seems sadly unable to tolerate the load. I don&#x27;t see an archived version of the post anywhere either.<p>If I had to have a guess what it says, it&#x27;s probably something along the lines of:<p>* Don&#x27;t require users of your API to start goroutines to use your API correctly.<p>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.<p>* If possible, avoid high goroutine fanout in your implementation.<p>If a single request to your API fans out to 1k goroutines, it&#x27;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.<p>IMO, these are two good principles to live by, and not just in Go.
评论 #21568436 未加载
评论 #21568264 未加载
boomlindeover 5 years ago
In general, decouple libraries from use case dependent assumptions. This holds true not just for concurrency, but for memory management, sockets, file streams etc. Don&#x27;t assume that a per-object heap allocation is good enough for me. Don&#x27;t assume that I want to serialize to a file system. Don&#x27;t assume that a channel has a large enough buffer.
ridiculous_fishover 5 years ago
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.<p>Is this still the case? How does modern Go handle operations which require kernel threads?
评论 #21571391 未加载
评论 #21570864 未加载
AndrewStephensover 5 years ago
I can&#x27;t read the article but hopefully it does not ironically present advice on writing webservers.<p>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&#x27;ed server.
评论 #21568633 未加载
评论 #21570901 未加载
评论 #21568287 未加载
kimiover 5 years ago
The problem with goroutines is that an unhandled crash can cause a shutdown, and if the goroutine is started by a badly-written library with no proper recovery, you are dead.
评论 #21579380 未加载
评论 #21572191 未加载
kstenerudover 5 years ago
&quot;threads&quot; (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.
评论 #21570223 未加载
评论 #21570689 未加载
评论 #21570412 未加载