<div dir="ltr">Hi!<div><br></div><div>As you may know, OTP 22 has a new low-level socket API: <a href="http://erlang.org/doc/man/socket.html">http://erlang.org/doc/man/socket.html</a><br>It allows to communicate over UNIX sockets,</div><div>and UNIX sockets have sendmsg feature, which has an option to pass a file descriptor.<br clear="all"><div><br></div><div>I tried to pass the FD with a fresh OTP, and this didn't just work.</div><div>After a bit of hacking I've made a patch for socket NIF and a demo.</div><div>  Patch: <a href="https://github.com/erlang/otp/pull/2400">https://github.com/erlang/otp/pull/2400</a></div><div>  Demo: <a href="https://gist.github.com/stolen/303c30d4edbb8835f9bec3fad0d75ede">https://gist.github.com/stolen/303c30d4edbb8835f9bec3fad0d75ede</a></div><div><br></div><div><div class="gmail-ml-6 gmail-pl-3" style="box-sizing:border-box;color:rgb(36,41,46);font-family:-apple-system,system-ui,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";font-size:14px;margin-left:40px;padding-left:16px"><div class="gmail-TimelineItem gmail-js-comment-container" style="box-sizing:border-box;display:flex;padding:16px 0px;margin-left:16px"><div class="gmail-timeline-comment-group gmail-js-minimizable-comment-group gmail-js-targetable-comment gmail-TimelineItem-body gmail-my-0" id="gmail-gistcomment-3036239" style="box-sizing:border-box;margin-top:4px;max-width:100%;color:rgb(68,77,86);margin-bottom:0px"><div class="gmail-ml-n3 gmail-timeline-comment gmail-unminimized-comment gmail-comment gmail-previewable-edit gmail-js-task-list-container editable-comment gmail-js-comment gmail-timeline-comment--caret gmail-reorderable-task-lists gmail-current-user" style="box-sizing:border-box;color:rgb(36,41,46);border:1px solid rgb(192,211,235);border-radius:3px"><div class="edit-comment-hide gmail-js-edit-comment-hide" style="box-sizing:border-box"><span style="box-sizing:border-box"><table class="gmail-d-block" style="box-sizing:border-box;border-spacing:0px;border-collapse:collapse;display:block"><tbody class="gmail-d-block" style="box-sizing:border-box;display:block"><tr class="gmail-d-block" style="box-sizing:border-box;display:block"><td class="gmail-d-block gmail-comment-body gmail-markdown-body gmail-js-comment-body" style="box-sizing:border-box;padding:15px;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";line-height:1.5;width:922px;overflow:visible;display:block"><p style="box-sizing:border-box;margin-bottom:16px;margin-top:0px">Example: in four different shells execute:</p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px"><li style="box-sizing:border-box;margin-left:0px"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;padding:0.2em 0.4em;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">./sendsock.escript proxy</code></li><li style="box-sizing:border-box;margin-top:0.25em;margin-left:0px"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;padding:0.2em 0.4em;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">./sendsock.escript worker</code></li><li style="box-sizing:border-box;margin-top:0.25em;margin-left:0px"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;padding:0.2em 0.4em;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">telnet localhost 13456</code></li><li style="box-sizing:border-box;margin-top:0.25em;margin-left:0px"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;padding:0.2em 0.4em;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">telnet localhost 13456</code></li></ul><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px">See the listen and established sockets are owned by different <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;padding:0.2em 0.4em;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">beam.smp</code> processes:</p><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;margin-top:0px;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;margin-bottom:0px"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,monospace;font-size:11.9px;padding:0px;margin:0px;background:initial;border-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit">$ lsof -i:13456
COMMAND    PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
beam.smp 82931 stolen   32u  IPv4 0x3c2e6eba43bdf26f      0t0  TCP *:13456 (LISTEN)
beam.smp 82952 stolen   32u  IPv4 0x3c2e6eba47fa726f      0t0  TCP localhost:13456->localhost:62163 (ESTABLISHED)
beam.smp 82952 stolen   33u  IPv4 0x3c2e6eba41348e8f      0t0  TCP localhost:13456->localhost:62185 (ESTABLISHED)
telnet   82976 stolen    5u  IPv4 0x3c2e6eba4f19452f      0t0  TCP localhost:62163->localhost:13456 (ESTABLISHED)
telnet   82993 stolen    5u  IPv4 0x3c2e6eba4786052f      0t0  TCP localhost:62185->localhost:13456 (ESTABLISHED)</code></pre></td></tr></tbody></table></span></div></div></div></div></div></div><div><br></div><div>What this may be useful for?</div><div>  * Hot upgrades with BEAM upgrade/restart</div><div>  * Load-balancing TCP load between unreliable workers without proxying</div><div>  * Single-connection distribution (see <a href="https://github.com/stolen/webdist">https://github.com/stolen/webdist</a>)</div><div>  * Maybe anything else</div><div><br></div><div>Have fun!</div><div><br></div>-- </div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><font face="'courier new', monospace">Danil Zagoskin | <a href="mailto:z@gosk.in" target="_blank">z@gosk.in</a></font></div></div></div></div></div>