The main issue with your use of socket options is that reuseaddr
and reuseport
options have to be set prior to calling socket:bind/2
. Try this:
{ok, LSock} = socket:open(inet, stream, tcp),
ok = socket:setopt(LSock, socket, reuseaddr, true),
ok = socket:setopt(LSock, socket, reuseport, true),
ok = socket:bind(LSock, #{family => inet, port => Port, addr => Addr}),
...
Secondly, if you need to have multiple worker processes doing the accept, you can pass the LSock
to a pool of worker processes so that they are calling the socket:accept/1
on the same instance of LSock
. If your idea was that the reuseaddr
and reuseport
will help you have multiple listening sockets open on the same address/port pair, that is not what those two options are for. These options are meant to allow your OS process to reconnect to the same address/port right after the process crashed that was previously bound to that address/port pair, so that the OS would release the socket and make it available for new binding immediately.