4 ms·
In case people skim over this. One of the nice things about Erlang / Elixir is that the tcp/udp libraries are well integrated with the actor-model. Incoming dat
by latch 6y ago
In case people skim over this. One of the nice things about Erlang / Elixir is that the tcp/udp libraries are well integrated with the actor-model. Incoming data can just be another message.
You can flood your process this way and there's no feedback to the OS that you aren't actually handling the data, but there are flags / patterns to cleanly deal with that (e.g, using active: :once which is probably what most cases should be doing)
Further, for the tcp library, there are a handful of message boundary options that can really speed development. Set "active: :true, packet: 4" on the socket, and you're telling the library that each message has a 4-byte length prefix. It'll take care of the rest - on both the sending and receiving end. (pairing this with the built-in object serialization (term_to_binary and binary_to_term) makes it possible to achieve relatively complex logic across systems with minimal amount of code).
There's also a newish socket module that exposes an API which is more similar to what most programmers are probably used to.
- dnautics 6y agobinary pattern matching is also a godsend for network protocols. I generally prefer using a blocking recv loop when writing network protocol servers in elixir. I learned some really cool things from this blog post, namely the stuff about multicast membership and groups.
- latch 6y agoYes. I recently wrote an Elixir driver for MonetDB. I also wrote a CLI tool in Go for MonetDB and had to write a subset of the driver. Pattern matching in general, and binary pattern matching specifically, made the experience (and the resulting code quality) night and day. The big issue I have with pattern matching / binaries (aka, strings) in elixir / erlang is that doing it efficiently is, at best, a dark art and, at worst, impossible. This was drastically improved in OTP 22, but it's still opaque.
- kamikaz1k 6y ago> The big issue I have with pattern matching / binaries (aka, strings) in elixir / erlang is that doing it efficiently is, at best, a dark art and, at worst, impossible. Sorry, can you elaborate what you mean by impossible? Or maybe what you mean by efficient. Thanks!
- latch 6y agoI just spent some time looking at some old code, and it does seem like OTP 22 solved any of the issues that I had run into at the time. My mistake, sorry about that. (Though I maintain that it's still opaque)
- ergl 6y agoThere's a section about pattern matching and binaries at https://erlang.org/doc/efficiency_guide/binaryhandling.html https://erlang.org/doc/efficiency_guide/binaryhandling.html Compiling with the bin_opt_info flag (erlc +bin_opt_info Mod.erl) will list the points in your code where binaries are created during pattern match, or any other points where binaries could be shared, but are not.
- non-entity 6y agoI really wish more languages had better handling for binary, such as in Erlang. I'm working on an implementation of a rather old networking protocol where frames are very bit oriented and writing code to to read and write individual bits feels so tedious.
- R0b0t1 6y agoI agree, if you are reading this please look more deeply at Elixir and Erlang. Unlike many languages that do things a different way Elixir/Erlang see real industry use at scale. Erlang is a language I've recommended people read about for ages because it is different and useful.