<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>Oh nice, maybe this is why I could never get the systemd socket activation to work! It relies on using the file descriptor 3 for the listen socket.<br></div><div><br></div><div>On Tue, Sep 24, 2019, at 09:33, Danil Zagoskin wrote:<br></div><blockquote type="cite" id="qt"><div dir="ltr"><div>Hi!<br></div><div><br></div><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></div><div>It allows to communicate over UNIX sockets,<br></div></div><div><div>and UNIX sockets have sendmsg feature, which has an option to pass a file descriptor.<br></div><div><br></div><div>I tried to pass the FD with a fresh OTP, and this didn't just work.<br></div><div>After a bit of hacking I've made a patch for socket NIF and a demo.<br></div><div>  Patch: <a href="https://github.com/erlang/otp/pull/2400">https://github.com/erlang/otp/pull/2400</a><br></div><div>  Demo: <a href="https://gist.github.com/stolen/303c30d4edbb8835f9bec3fad0d75ede">https://gist.github.com/stolen/303c30d4edbb8835f9bec3fad0d75ede</a><br></div><div><br></div><div><div 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;" class="qt-gmail-ml-6 qt-gmail-pl-3"><div style="box-sizing:border-box;display:flex;padding-top:16px;padding-right:0px;padding-bottom:16px;padding-left:0px;margin-left:16px;" class="qt-gmail-TimelineItem qt-gmail-js-comment-container"><div style="box-sizing:border-box;margin-top:4px;max-width:100%;color:rgb(68, 77, 86);margin-bottom:0px;" id="qt-gmail-gistcomment-3036239" class="qt-gmail-timeline-comment-group qt-gmail-js-minimizable-comment-group qt-gmail-js-targetable-comment qt-gmail-TimelineItem-body qt-gmail-my-0"><div style="box-sizing:border-box;color:rgb(36, 41, 46);border-top-color:rgb(192, 211, 235);border-top-style:solid;border-top-width:1px;border-right-color:rgb(192, 211, 235);border-right-style:solid;border-right-width:1px;border-bottom-color:rgb(192, 211, 235);border-bottom-style:solid;border-bottom-width:1px;border-left-color:rgb(192, 211, 235);border-left-style:solid;border-left-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="qt-gmail-ml-n3 qt-gmail-timeline-comment qt-gmail-unminimized-comment qt-gmail-comment qt-gmail-previewable-edit qt-gmail-js-task-list-container qt-editable-comment qt-gmail-js-comment qt-gmail-timeline-comment--caret qt-gmail-reorderable-task-lists qt-gmail-current-user"><div style="box-sizing:border-box;" class="qt-edit-comment-hide qt-gmail-js-edit-comment-hide"><span style="box-sizing:border-box;"><table style="box-sizing:border-box;border-spacing:0px;border-collapse:collapse;display:block;" class="qt-gmail-d-block"><tbody style="box-sizing:border-box;display:block;" class="qt-gmail-d-block"><tr style="box-sizing:border-box;display:block;" class="qt-gmail-d-block"><td style="box-sizing:border-box;padding-top:15px;padding-right:15px;padding-bottom:15px;padding-left: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-x:visible;overflow-y:visible;display:block;" class="qt-gmail-d-block qt-gmail-comment-body qt-gmail-markdown-body qt-gmail-js-comment-body"><p style="box-sizing:border-box;margin-bottom:16px;margin-top:0px;">Example: in four different shells execute:<br></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-top:0.2em;padding-right:0.4em;padding-bottom:0.2em;padding-left:0.4em;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(27, 31, 35, 0.05);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">./sendsock.escript proxy</code><br></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-top:0.2em;padding-right:0.4em;padding-bottom:0.2em;padding-left:0.4em;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(27, 31, 35, 0.05);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">./sendsock.escript worker</code><br></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-top:0.2em;padding-right:0.4em;padding-bottom:0.2em;padding-left:0.4em;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(27, 31, 35, 0.05);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">telnet localhost 13456</code><br></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-top:0.2em;padding-right:0.4em;padding-bottom:0.2em;padding-left:0.4em;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(27, 31, 35, 0.05);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">telnet localhost 13456</code><br></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-top:0.2em;padding-right:0.4em;padding-bottom:0.2em;padding-left:0.4em;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(27, 31, 35, 0.05);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">beam.smp</code> processes:<br></p><pre style="box-sizing:border-box;font-family:SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;font-size:11.9px;margin-top:0px;padding-top:16px;padding-right:16px;padding-bottom:16px;padding-left:16px;overflow-x:auto;overflow-y:auto;line-height:1.45;background-color:rgb(246, 248, 250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-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-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:initial;background-position-x:initial;background-position-y:initial;background-repeat:initial;background-attachment:initial;background-image:initial;background-size:initial;background-origin:initial;background-clip:initial;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;border-top-color:currentcolor;border-top-style:none;border-top-width:0px;border-right-color:currentcolor;border-right-style:none;border-right-width:0px;border-bottom-color:currentcolor;border-bottom-style:none;border-bottom-width:0px;border-left-color:currentcolor;border-left-style:none;border-left-width:0px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;display:inline;overflow-x:visible;overflow-y: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><br></pre></td></tr></tbody></table></span></div></div></div></div></div></div><div><br></div><div>What this may be useful for?<br></div><div>  * Hot upgrades with BEAM upgrade/restart<br></div><div>  * Load-balancing TCP load between unreliable workers without proxying<br></div><div>  * Single-connection distribution (see <a href="https://github.com/stolen/webdist">https://github.com/stolen/webdist</a>)<br></div><div>  * Maybe anything else<br></div><div><br></div><div>Have fun!<br></div><div><br></div><div>-- <br></div></div><div><div class="qt-gmail_signature" dir="ltr"><div dir="ltr"><div><span style="font-family:'courier new', monospace" class="font">Danil Zagoskin | <a href="mailto:z@gosk.in">z@gosk.in</a></span><br></div></div></div></div></div><div>_______________________________________________<br></div><div>erlang-questions mailing list<br></div><div>erlang-questions@erlang.org<br></div><div>http://erlang.org/mailman/listinfo/erlang-questions<br></div><div><br></div></blockquote><div><br></div></body></html>