TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Elixir GenServer Explained

272 点作者 cheeseblubber大约 4 年前

12 条评论

davidw大约 4 年前
This is also a good reference. Even if it&#x27;s Erlang, what&#x27;s going on under the hood is the same:<p><a href="https:&#x2F;&#x2F;learnyousomeerlang.com&#x2F;clients-and-servers" rel="nofollow">https:&#x2F;&#x2F;learnyousomeerlang.com&#x2F;clients-and-servers</a>
ammanley大约 4 年前
Thank you so much for sharing this. I&#x27;ve not yet finished a decade in the industry, nor ever laid an eye on Elixir before, and this made sense pretty quickly. Great job explaining it all!<p>On a side note, its pretty amazing to see the impact on readability and understanding that native language features have on something like async. I&#x27;ve worked with similar systems to the example in the past in JS&#x2F;Go&#x2F;etc, and while you can definitely do the same stuff presented here, it tends to be a far messier callback-hell ime. For lack of better explanation, this Elixir code just &quot;flows&quot; better to my eye, which I know is a completely subjective and non-helpful unsolicited piece of info. Thanks again.
评论 #26965052 未加载
评论 #26964699 未加载
评论 #26966740 未加载
jimbokun大约 4 年前
Since these insert events are being buffered, what happens if the process dies? Are all of those inserts lost?<p>I feel like Erlang&#x2F;Elixir is designed to handle cases like this robustly, but it&#x27;s not clear to me how this code avoids losing data when a process crashes, potentially up to 5s worth of updates!
评论 #26960477 未加载
评论 #26960722 未加载
评论 #26959825 未加载
评论 #26960388 未加载
评论 #26959859 未加载
评论 #26960176 未加载
评论 #26960465 未加载
评论 #26970409 未加载
评论 #26960443 未加载
评论 #26960342 未加载
评论 #26959815 未加载
评论 #26960657 未加载
areichert大约 4 年前
In case anyone was curious what I used for the diagrams in this article, it was an awesome open source [0] tool called Excalidraw! [1]<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;excalidraw&#x2F;excalidraw" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;excalidraw&#x2F;excalidraw</a><p>[1] <a href="https:&#x2F;&#x2F;excalidraw.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;excalidraw.com&#x2F;</a>
评论 #26962031 未加载
namelosw大约 4 年前
It is very interesting to me that components in OTP like GenServer, Register, and Supervisor are surprisingly intuitive if one tries to learn and play Erlang first, but not so obvious and possibly looks artificial if people don&#x27;t learn Erlang in the first place.<p>At first, I was surprised that the Erlang processes resemble the Actor model in such a minimalistic interface. Then I play with them for a while, I realized I wanted to extract something similar to GenServers. Then when I played with GenServers, soon I was bothered by a bunch of Pids, and yearning for Registry. In the end, Supervisor is also pretty natural because things in the universe generally cannot revive themselves.<p>In a retrospective, OTP resembles a lot of concepts we are familiar with, the Registry just looks like how the web works because we need to resolve the address problem. And vice versa: Docker, Service Discovery, and Kubernetes look like GenServer, Registry, Supervisor respectively just at different scales.<p>It seems to me to let our systems fulfill some properties, some recurring themes are required. We&#x27;ll reuse them or rediscover them one way or another.
评论 #26980280 未加载
mulmen大约 4 年前
&gt; I&#x27;m ashamed to say most of my Elixir education has been through trial and error, figuring things out as I go along.<p>Oh, gosh. This is how I learned... everything? I feel like I just walked into homeroom without my pants on.
Zarathu大约 4 年前
&gt; In particular, I was itching to learn more about handling concurrency in Elixir. This, of course, led me to GenServers.<p>Might be a nitpicking here, but GenServers aren&#x27;t useful for concurrency. They just manage state, and only process one message at a time. If you&#x27;re using this as a cache, your reads will be bottlenecked by however quickly the GenServer can handle the read requests.
评论 #26961827 未加载
评论 #26961348 未加载
FrancoisBosun大约 4 年前
Well explained article. Good job OP!
xutopia大约 4 年前
GenServer is just one of the numerous things in the Elixir ecosystem that just bring joy. Once you I got the concepts behind Ecto and GenServers everything just felt easy to model in my mind. I find it so nice.
dmitriid大约 4 年前
Erlang&#x27;s OTP and Elixir&#x27;s wrappers on top of that is what is severely lacking from all languages claiming to provide tools for better concurrency.
intergalplan大约 4 年前
Maybe a dumb complaint, but I <i>hate</i> the name. I have a really hard time not reading it as the active name &quot;Generate Server&quot;. Like, I&#x27;d expect it to be a function that generates servers (whatever <i>that</i> means, since what it&#x27;s dealing with isn&#x27;t something I&#x27;d normally call &quot;a server&quot; either). It bugs the hell out of me every time I read Elixir code. One of those things I have to keep reminding myself: &quot;OK, it&#x27;s not what it looks like it is, at all... what did these misleading terms <i>actually</i> mean, again?&quot;
评论 #26959701 未加载
评论 #26960687 未加载
评论 #26959913 未加载
评论 #26964014 未加载
评论 #26960126 未加载
评论 #26959682 未加载
评论 #26960523 未加载
JohnCurran大约 4 年前
This line in the opening paragraph really rubs me the wrong way:<p>&gt; “I&#x27;m ashamed to say most of my Elixir education has been through trial and error, figuring things out as I go along“<p>This attitude is so prevalent in software as if we are all supposed to be divined with programming knowledge the moment our IDE spins up. In every other industry that’s exactly how you learn: Get your hands dirty, make mistakes, and fix them.<p>There’s a lot of things wrong with software engineering - harboring this attitude that self-taught learning is bad or shameful makes it unnecessarily worse<p>Great write-up otherwise
评论 #26960904 未加载
评论 #26961404 未加载
评论 #26960886 未加载
评论 #26963859 未加载
评论 #26963154 未加载
评论 #26961325 未加载
评论 #26960889 未加载