{"id":13636242,"url":"https://github.com/openresty/lua-resty-balancer","last_synced_at":"2025-04-19T04:32:22.176Z","repository":{"id":45176439,"uuid":"49620478","full_name":"openresty/lua-resty-balancer","owner":"openresty","description":"A generic consistent hash implementation for OpenResty/Lua","archived":false,"fork":false,"pushed_at":"2023-08-23T00:05:17.000Z","size":48,"stargazers_count":321,"open_issues_count":12,"forks_count":77,"subscribers_count":26,"default_branch":"master","last_synced_at":"2024-08-17T14:02:33.855Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openresty.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-01-14T03:35:21.000Z","updated_at":"2024-08-14T02:42:47.000Z","dependencies_parsed_at":"2024-01-13T10:12:13.963Z","dependency_job_id":"ade93f58-21c8-49a0-bf7d-ee90bad79f4d","html_url":"https://github.com/openresty/lua-resty-balancer","commit_stats":null,"previous_names":["agentzh/lua-resty-chash"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-balancer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-balancer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-balancer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-balancer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openresty","download_url":"https://codeload.github.com/openresty/lua-resty-balancer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249606497,"owners_count":21298851,"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":[],"created_at":"2024-08-02T00:00:58.923Z","updated_at":"2025-04-19T04:32:21.897Z","avatar_url":"https://github.com/openresty.png","language":"Lua","funding_links":[],"categories":["Libraries","Lua"],"sub_categories":[],"readme":"Name\n====\n\nlua-resty-chash - A generic consistent hash implementation for OpenResty/LuaJIT\n\nlua-resty-roundrobin - A generic roundrobin implementation for OpenResty/LuaJIT\n\nTable of Contents\n=================\n\n* [Name](#name)\n* [Status](#status)\n* [Description](#description)\n* [Synopsis](#synopsis)\n* [Methods](#methods)\n    * [new](#new)\n    * [reinit](#reinit)\n    * [set](#set)\n    * [delete](#delete)\n    * [incr](#incr)\n    * [decr](#decr)\n    * [find](#find)\n    * [next](#next)\n* [Installation](#installation)\n* [Performance](#performance)\n* [Author](#author)\n* [Copyright and License](#copyright-and-license)\n* [See Also](#see-also)\n\nStatus\n======\n\nThis library is still under early development and is still experimental.\n\nDescription\n===========\n\nThis Lua library can be used with `balancer_by_lua*`.\n\nSynopsis\n========\n\n```lua\n    lua_package_path \"/path/to/lua-resty-chash/lib/?.lua;;\";\n    lua_package_cpath \"/path/to/lua-resty-chash/?.so;;\";\n\n    init_by_lua_block {\n        local resty_chash = require \"resty.chash\"\n        local resty_roundrobin = require \"resty.roundrobin\"\n        local resty_swrr = require \"resty.swrr\"\n\n        local server_list = {\n            [\"127.0.0.1:1985\"] = 2,\n            [\"127.0.0.1:1986\"] = 2,\n            [\"127.0.0.1:1987\"] = 1,\n        }\n\n        -- XX: we can do the following steps to keep consistency with nginx chash\n        local str_null = string.char(0)\n\n        local servers, nodes = {}, {}\n        for serv, weight in pairs(server_list) do\n            -- XX: we can just use serv as id when we doesn't need keep consistency with nginx chash\n            local id = string.gsub(serv, \":\", str_null)\n\n            servers[id] = serv\n            nodes[id] = weight\n        end\n\n        local chash_up = resty_chash:new(nodes)\n\n        package.loaded.my_chash_up = chash_up\n        package.loaded.my_servers = servers\n\n        local rr_up = resty_roundrobin:new(server_list)\n        package.loaded.my_rr_up = rr_up\n\n        local swrr_up = resty_swrr:new(server_list)\n        package.loaded.my_swrr_up = swrr_up\n    }\n\n    upstream backend_chash {\n        server 0.0.0.1;\n        balancer_by_lua_block {\n            local b = require \"ngx.balancer\"\n\n            local chash_up = package.loaded.my_chash_up\n            local servers = package.loaded.my_servers\n\n            -- we can balancer by any key here\n            local id = chash_up:find(ngx.var.arg_key)\n            local server = servers[id]\n\n            assert(b.set_current_peer(server))\n        }\n    }\n\n    upstream backend_rr {\n        server 0.0.0.1;\n        balancer_by_lua_block {\n            local b = require \"ngx.balancer\"\n\n            local rr_up = package.loaded.my_rr_up\n\n            -- Note that Round Robin picks the first server randomly\n            local server = rr_up:find()\n\n            assert(b.set_current_peer(server))\n        }\n    }\n\n    upstream backend_swrr {\n        server 0.0.0.1;\n        balancer_by_lua_block {\n            local b = require \"ngx.balancer\"\n\n            local swrr_up = package.loaded.my_swrr_up\n\n            -- Note that SWRR picks the first server randomly\n            local server = swrr_up:find()\n\n            assert(b.set_current_peer(server))\n        }\n    }\n\n    server {\n        location /chash {\n            proxy_pass http://backend_chash;\n        }\n\n        location /roundrobin {\n            proxy_pass http://backend_rr;\n        }\n\n        location /swrr {\n            proxy_pass http://backend_swrr;\n        }\n    }\n```\n\n[Back to TOC](#table-of-contents)\n\nMethods\n=======\n\nBoth `resty.chash`, `resty.roundrobin` and `resty.swrr` have the same apis.\n\n[Back to TOC](#table-of-contents)\n\nnew\n---\n**syntax:** `obj, err = class.new(nodes)`\n\nInstantiates an object of this class. The `class` value is returned by the call `require \"resty.chash\"`.\n\nThe `id` should be `table.concat({host, string.char(0), port})` like the nginx chash does,\nwhen we need to keep consistency with nginx chash.\n\nThe `id` can be any string value when we do not need to keep consistency with nginx chash.\nThe `weight` should be a non negative integer.\n\n```lua\nlocal nodes = {\n    -- id =\u003e weight\n    server1 = 10,\n    server2 = 2,\n}\n\nlocal resty_chash = require \"resty.chash\"\n\nlocal chash = resty_chash:new(nodes)\n\nlocal id = chash:find(\"foo\")\n\nngx.say(id)\n```\n\n[Back to TOC](#table-of-contents)\n\nreinit\n--------\n**syntax:** `obj:reinit(nodes)`\n\nReinit the chash obj with the new `nodes`.\n\n[Back to TOC](#table-of-contents)\n\nset\n--------\n**syntax:** `obj:set(id, weight)`\n\nSet `weight` of the `id`.\n\n[Back to TOC](#table-of-contents)\n\ndelete\n--------\n**syntax:** `obj:delete(id)`\n\nDelete the `id`.\n\n[Back to TOC](#table-of-contents)\n\nincr\n--------\n**syntax:** `obj:incr(id, weight?)`\n\nIncrements weight for the `id` by the step value `weight`(default to 1).\n\n[Back to TOC](#table-of-contents)\n\ndecr\n--------\n**syntax:** `obj:decr(id, weight?)`\n\nDecrease weight for the `id` by the step value `weight`(default to 1).\n\n[Back to TOC](#table-of-contents)\n\nfind\n--------\n**syntax:** `id, index = obj:find(key)`\n\nFind an id by the `key`, same key always return the same `id` in the same `obj`.\n\nThe second return value `index` is the index in the chash circle of the hash value of the `key`.\n\n[Back to TOC](#table-of-contents)\n\nnext\n--------\n**syntax:** `id, new_index = obj:next(old_index)`\n\nIf we have chance to retry when the first `id`(server) doesn't work well,\nthen we can use `obj:next` to get the next `id`.\n\nThe new `id` may be the same as the old one.\n\n[Back to TOC](#table-of-contents)\n\nInstallation\n============\n\nFirst you need to run `make` to generate the librestychash.so.\nThen you need to configure the lua_package_path and lua_package_cpath directive\nto add the path of your lua-resty-chash source tree to ngx_lua's LUA_PATH search\npath, as in\n\n```nginx\n    # nginx.conf\n    http {\n        lua_package_path \"/path/to/lua-resty-chash/lib/?.lua;;\";\n        lua_package_cpath \"/path/to/lua-resty-chash/?.so;;\";\n        ...\n    }\n```\n\nEnsure that the system account running your Nginx ''worker'' proceses have\nenough permission to read the `.lua` and `.so` file.\n\n[Back to TOC](#table-of-contents)\n\nPerformance\n===========\n\nThere is a benchmark script `t/bench.lua`.\n\nI got the result when I run `make bench`:\n\n```\nchash new servers\n10000 times\nelasped: 0.61600017547607\n\nchash new servers2\n1000 times\nelasped: 0.77300000190735\n\nchash new servers3\n10000 times\nelasped: 0.66899991035461\n\nnew in func\n10000 times\nelasped: 0.62000012397766\n\nnew dynamic\n10000 times\nelasped: 0.75499987602234\n\nincr server3\n10000 times\nelasped: 0.19000029563904\n\nincr server1\n10000 times\nelasped: 0.33699989318848\n\ndecr server1\n10000 times\nelasped: 0.27300024032593\n\ndelete server3\n10000 times\nelasped: 0.037999868392944\n\ndelete server1\n10000 times\nelasped: 0.065000057220459\n\nset server1 9\n10000 times\nelasped: 0.26600003242493\n\nset server1 8\n10000 times\nelasped: 0.32000017166138\n\nset server1 1\n10000 times\nelasped: 0.56699991226196\n\nbase for find\n1000000 times\nelasped: 0.01800012588501\n\nfind\n1000000 times\nelasped: 0.9469997882843\n```\n\n[Back to TOC](#table-of-contents)\n\nAuthor\n======\n\nDejiang Zhu (doujiang24) \u003cdoujiang24@gmail.com\u003e.\n\n[Back to TOC](#table-of-contents)\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2015-2016, by Yichun Zhang (agentzh) \u003cagentzh@gmail.com\u003e, CloudFlare Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n[Back to TOC](#table-of-contents)\n\nSee Also\n========\n* the ngx_lua module: http://wiki.nginx.org/HttpLuaModule\n* the json lib for Lua and C: https://github.com/cloudflare/lua-resty-json\n\n[Back to TOC](#table-of-contents)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-balancer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenresty%2Flua-resty-balancer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-balancer/lists"}