{"id":18151340,"url":"https://github.com/msantos/reuseport","last_synced_at":"2025-04-28T17:50:27.326Z","repository":{"id":136621483,"uuid":"158006135","full_name":"msantos/reuseport","owner":"msantos","description":"SO_REUSEPORT socket load distribution using LD_PRELOAD ","archived":false,"fork":false,"pushed_at":"2025-03-12T11:26:37.000Z","size":26,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T11:41:41.108Z","etag":null,"topics":["fd","ldpreload","reuseport","socket"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/msantos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-17T17:04:12.000Z","updated_at":"2025-03-12T11:26:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"80189467-f923-470e-a81c-692193e48aee","html_url":"https://github.com/msantos/reuseport","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msantos%2Freuseport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msantos%2Freuseport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msantos%2Freuseport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msantos%2Freuseport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/msantos","download_url":"https://codeload.github.com/msantos/reuseport/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251360575,"owners_count":21577291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fd","ldpreload","reuseport","socket"],"created_at":"2024-11-02T01:07:24.802Z","updated_at":"2025-04-28T17:50:27.320Z","avatar_url":"https://github.com/msantos.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libreuseport\n\nlibreuseport is a small library for socket load distribution (aka port\nsharding).\n\nThe library works by intercepting calls to `bind(2)` using\n`LD_PRELOAD`. Before `bind(2)`ing, `setsockopt(2)` is called with the\n`SO_REUSEPORT` option.\n\nlibreuseport requires the program to be dynamically linked. libreuseport\nwill not work with statically linked programs or programs that directly\nmake syscalls.\n\n## Build\n\n```\nmake\n```\n\n## Environment Variables\n\n`LIBREUSEPORT_ADDR`\n: Set socket option only if the socket matches the specified IPv4 or\nIPv6 address (default: any address).\n\n```\n    LIBREUSEPORT_ADDR=\"127.0.0.1\"\n```\n\n`LIBREUSEPORT_PORT`\n: Set socket option only if the socket matches the specified port\n(default: any port).\n\n```\n    LIBREUSEPORT_PORT=\"80\"\n```\n\n`SO_REUSEPORT`\n: Enable or disable `SO_REUSEPORT` socket option (default: 1)\n\n```\n    0 : disable\n    1 : enable (default)\n    -1 : use system default\n```\n\n`SO_REUSEADDR`\n: Enable or disable `SO_REUSEADDR` socket option (default: -1)\n\n```\n    0 : disable\n    1 : enable\n    -1 : use system default (default)\n```\n\n`SO_BINDTODEVICE`\n: On a system with multiple interfaces, bind the socket to a specific\ninterface (default: disabled)\n\n```\n    SO_BINDTODEVICE=\"eth0\"\n```\n\n`LIBREUSEPORT_DEBUG`\n: Output errors to stderr\n\n```\n    LIBREUSEPORT_DEBUG=\"1\"\n```\n\n## Using\n\n* python HTTP server\n\n```\n# run in a shell\n$ LD_PRELOAD=./libreuseport.so python3 -m http.server 8000\n\n# in another shell\n$ LD_PRELOAD=./libreuseport.so python3 -m http.server 8000\n\n# yet another shell\nwhile :; do curl http://0.0.0.0:8000; sleep 1; done\n```\n\n* erlang\n\n```erlang\n$ LD_PRELOAD=./libreuseport.so erl\n\n1\u003e {ok, L1} = gen_tcp:listen(5678, [binary, {packet,0}, {active,true}]).\n{ok,#Port\u003c0.6\u003e}\n\n2\u003e {ok, L2} = gen_tcp:listen(5678, [binary, {packet,0}, {active,true}]).\n{ok,#Port\u003c0.7\u003e}\n\n3\u003e Accept = fun Loop(L) -\u003e {ok, S} = gen_tcp:accept(L), io:format(\"~p-\u003e~p~n\", [{listen, L}, {socket, S}]), gen_tcp:close(S), Loop(L) end.\n#Fun\u003cerl_eval.6.128620087\u003e\n\n4\u003e spawn(fun() -\u003e Accept(L1) end).\n\u003c0.86.0\u003e\n5\u003e spawn(fun() -\u003e Accept(L2) end).\n\u003c0.88.0\u003e\n\n% run: nc -z 127.0.0.1 5678\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.8\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.9\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.10\u003e}\n{listen,#Port\u003c0.7\u003e}-\u003e{socket,#Port\u003c0.11\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.12\u003e}\n{listen,#Port\u003c0.7\u003e}-\u003e{socket,#Port\u003c0.13\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.14\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.15\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.16\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.17\u003e}\n{listen,#Port\u003c0.7\u003e}-\u003e{socket,#Port\u003c0.18\u003e}\n{listen,#Port\u003c0.6\u003e}-\u003e{socket,#Port\u003c0.19\u003e}\n{listen,#Port\u003c0.7\u003e}-\u003e{socket,#Port\u003c0.20\u003e}\n{listen,#Port\u003c0.7\u003e}-\u003e{socket,#Port\u003c0.21\u003e}\n{listen,#Port\u003c0.7\u003e}-\u003e{socket,#Port\u003c0.22\u003e}\n```\n\n## See Also\n\n* Using `LD_PRELOAD` to bind an application to an interface:\n\n  https://gist.github.com/markusfisch/51b1ce6c3ca9ce67e081\n\n* Linux port sharding demo\n\n  https://github.com/joewalnes/port-sharding\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsantos%2Freuseport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmsantos%2Freuseport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsantos%2Freuseport/lists"}