favetelinguis
I have an ssl socket which receives messages in JSON format. Right now im using Erlang ssl to setup the connection. In my implementation handle_info is called when some data with a fixed size is available. However the JSON messages can be large so that handle_info is called multiple times with only parts of the message. I then do:
String.ends_with?(new_msg, "\n")
to put together whole messages.
This feels very slow, what I would like is to move this logic down and only have handle_info called when a complete message is available on the socket, that is when a message terminated with \n has arrived.
Is there any support for this in Erlang/Elixir?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
You can buffer the message in the process controlling the socket and only after it’s received in full, send to the process where
handle_infois defined. Or you can add a message length header to your protocol (instead of\n) and receive only what’s needed with:ssl.recv(socket, message_length, timeout)– that would be the fastest approach (you’d need to open the socket withactive: falseand:rawoptions).favetelinguis
Thanks for the tips, right now im buffering, and its not my protocol so I cant change that. The issue I have is that checking
ends_with?is O(n) in the length of the message, I was hoping there was some better way.voltone
For stream-based sockets, including
ssl, Erlang can do the buffering for you for a number of packet formats, including line protocols. The mode is controlled through thepacketoption: inet — OTP 29.0.2 (kernel 11.0.2).So, to get messages from a TLS socket delivered at line break boundaries:
:ssl.connect('example.net', 443, packet: :line)favetelinguis
The docs says the following,
Line mode, a packet is a line-terminated with newline, lines longer than the receive buffer are truncated. Does this not mean that I still need to buffer in my process since it is exactly the case where a line is longer then the receive buffer im interested in?voltone
Right, there is an upper bound, but if you know the upper limit of the messages you expect to get you can customise it using
:inet.setopts(s, buffer: @max_size).If you want to be able to receive messages of arbitrary size, you’d still have to implement buffering in your application. But even in that case using
packet: :linemakes your job easier, because it guarantees that you won’t get multiple lines (of fragments of multiple lines) in a single packet. Using:rawmode that can happen, and you’d have to scan not just for a newline at the end, but also newlines inside a message: you’d have to do the entire reassembly into lines, both joining and splitting.As for scanning for the newline at the end, if you’re using charlists it is indeed O(n), but if put the socket in binary mode and you use
String.ends_with?it should be O(1).NobbZ
Nope, it is still O(n), since elixir does also check if the full string is valid UTF-8.
If one does not care for UTF-8 validity of the string when checking for the final newline (remember, we could have a chunk split midst a multibyte codepoint anyway), then one can use
:binary.last(input) == ?\n.voltone
But does it? It is implemented as a combination of
byte_size/1andbinary_part/3, and both are not UTF-8 aware…https://github.com/elixir-lang/elixir/blob/v1.7.3/lib/elixir/lib/string.ex#L2000-L2018
NobbZ
Oh, then it has been optimized. Or was always. But I remember that @josevalim once said, all functions in
Stringwhere checking for validity.And in fact, in my opinion they should, and we should have another module working with pure binaries that does not check for string specifics, as erlang makes a different between the
stringsandlistsmodule as well…voltone
Oh, I agree, and I was going to suggest a real binary alternative when I decided to just check the source real quick anyway
favetelinguis
I dont know the exact max size but would there be any negative impact of just reserving a very large buffer like 1mb. I know nothing will be larger than that and most messages will be around 500chars.