{"id":13636400,"url":"https://github.com/ledgetech/lua-resty-redis-connector","last_synced_at":"2025-04-19T08:32:09.513Z","repository":{"id":31715355,"uuid":"35281159","full_name":"ledgetech/lua-resty-redis-connector","owner":"ledgetech","description":"Connection utilities for lua-resty-redis","archived":false,"fork":false,"pushed_at":"2022-01-07T12:08:49.000Z","size":139,"stargazers_count":234,"open_issues_count":8,"forks_count":71,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-08-17T14:02:30.278Z","etag":null,"topics":["lua","lua-resty-redis","luajit","nginx","openresty","redis","redis-sentinel"],"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/ledgetech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"pintsized"}},"created_at":"2015-05-08T13:35:09.000Z","updated_at":"2024-07-03T08:24:59.000Z","dependencies_parsed_at":"2022-07-20T15:15:07.523Z","dependency_job_id":null,"html_url":"https://github.com/ledgetech/lua-resty-redis-connector","commit_stats":null,"previous_names":["pintsized/lua-resty-redis-connector"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledgetech%2Flua-resty-redis-connector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledgetech%2Flua-resty-redis-connector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledgetech%2Flua-resty-redis-connector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledgetech%2Flua-resty-redis-connector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ledgetech","download_url":"https://codeload.github.com/ledgetech/lua-resty-redis-connector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249650183,"owners_count":21305976,"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":["lua","lua-resty-redis","luajit","nginx","openresty","redis","redis-sentinel"],"created_at":"2024-08-02T00:01:00.872Z","updated_at":"2025-04-19T08:32:09.277Z","avatar_url":"https://github.com/ledgetech.png","language":"Lua","readme":"# lua-resty-redis-connector\n\n[![Build\nStatus](https://travis-ci.org/ledgetech/lua-resty-redis-connector.svg?branch=master)](https://travis-ci.org/ledgetech/lua-resty-redis-connector)\n\nConnection utilities for\n[lua-resty-redis](https://github.com/openresty/lua-resty-redis), making it easy\nand reliable to connect to Redis hosts, either directly or via [Redis\nSentinel](http://redis.io/topics/sentinel).\n\n\n## Synopsis\n\nQuick and simple authenticated connection on localhost to DB 2:\n\n```lua\nlocal redis, err = require(\"resty.redis.connector\").new({\n    url = \"redis://PASSWORD@127.0.0.1:6379/2\",\n}):connect()\n```\n\nMore verbose configuration, with timeouts and a default password:\n\n```lua\nlocal rc = require(\"resty.redis.connector\").new({\n    connect_timeout = 50,\n    send_timeout = 5000,\n    read_timeout = 5000,\n    keepalive_timeout = 30000,\n    password = \"mypass\",\n})\n\nlocal redis, err = rc:connect({\n    url = \"redis://127.0.0.1:6379/2\",\n})\n\n-- ...\n\nlocal ok, err = rc:set_keepalive(redis)  -- uses keepalive params\n```\n\nKeep all config in a table, to easily create / close connections as needed:\n\n```lua\nlocal rc = require(\"resty.redis.connector\").new({\n    connect_timeout = 50,\n    send_timeout = 5000,\n    read_timeout = 5000,\n    keepalive_timeout = 30000,\n\n    host = \"127.0.0.1\",\n    port = 6379,\n    db = 2,\n    password = \"mypass\",\n})\n\nlocal redis, err = rc:connect()\n\n-- ...\n\nlocal ok, err = rc:set_keepalive(redis)\n```\n\n[connect](#connect) can be used to override some defaults given in [new](#new),\nwhich are pertinent to this connection only.\n\n\n```lua\nlocal rc = require(\"resty.redis.connector\").new({\n    host = \"127.0.0.1\",\n    port = 6379,\n    db = 2,\n})\n\nlocal redis, err = rc:connect({\n    db = 5,\n})\n```\n\n\n## DSN format\n\nIf the `params.url` field is present then it will be parsed to set the other\nparams. Any manually specified params will override values given in the DSN.\n\n*Note: this is a behaviour change as of v0.06. Previously, the DSN values would\ntake precedence.*\n\n### Direct Redis connections\n\nThe format for connecting directly to Redis is:\n\n`redis://USERNAME:PASSWORD@HOST:PORT/DB`\n\nThe `USERNAME`, `PASSWORD` and `DB` fields are optional, all other components\nare required.\n\nUse of username requires Redis 6.0.0 or newer.\n\n### Connections via Redis Sentinel\n\nWhen connecting via Redis Sentinel, the format is as follows:\n\n`sentinel://USERNAME:PASSWORD@MASTER_NAME:ROLE/DB`\n\nAgain, `USERNAME`, `PASSWORD` and `DB` are optional. `ROLE` must be either `m`\nor `s` for master / slave respectively.\n\nOn versions of Redis newer than 5.0.1, Sentinels can optionally require their\nown password. If enabled, provide this password in the `sentinel_password`\nparameter. On Redis 6.2.0 and newer you can pass username using\n`sentinel_username` parameter.\n\nA table of `sentinels` must also be supplied. e.g.\n\n```lua\nlocal redis, err = rc:connect{\n    url = \"sentinel://mymaster:a/2\",\n    sentinels = {\n        { host = \"127.0.0.1\", port = 26379 },\n    },\n    sentinel_username = \"default\",\n    sentinel_password = \"password\"\n}\n```\n\n## Proxy Mode\n\nEnable the `connection_is_proxied` parameter if connecting to Redis through a\nproxy service (e.g. Twemproxy). These proxies generally only support a limited\nsub-set of Redis commands, those which do not require state and do not affect\nmultiple keys. Databases and transactions are also not supported.\n\nProxy mode will disable switching to a DB on connect. Unsupported commands\n(defaults to those not supported by Twemproxy) will return `nil, err`\nimmediately rather than being sent to the proxy, which can result in dropped\nconnections.\n\n`discard` will not be sent when adding connections to the keepalive pool\n\n\n## Disabled commands\n\nIf configured as a table of commands, the command methods will be replaced by a\nfunction which immediately returns `nil, err` without forwarding the command to\nthe server\n\n## Default Parameters\n\n\n```lua\n{\n    connect_timeout = 100,\n    send_timeout = 1000,\n    read_timeout = 1000,\n    keepalive_timeout = 60000,\n    keepalive_poolsize = 30,\n\n    -- ssl, ssl_verify, server_name, pool, pool_size, backlog\n    -- see: https://github.com/openresty/lua-resty-redis#connect\n    connection_options = {},\n\n    host = \"127.0.0.1\",\n    port = \"6379\",\n    path = \"\",  -- unix socket path, e.g. /tmp/redis.sock\n    username = \"\",\n    password = \"\",\n    sentinel_username = \"\",\n    sentinel_password = \"\",\n    db = 0,\n\n    master_name = \"mymaster\",\n    role = \"master\",  -- master | slave\n    sentinels = {},\n\n    connection_is_proxied = false,\n\n    disabled_commands = {},\n}\n```\n\n\n## API\n\n* [new](#new)\n* [connect](#connect)\n* [set_keepalive](#set_keepalive)\n* [Utilities](#utilities)\n    * [connect_via_sentinel](#connect_via_sentinel)\n    * [try_hosts](#try_hosts)\n    * [connect_to_host](#connect_to_host)\n    * [sentinel.get_master](#sentinelget_master)\n    * [sentinel.get_slaves](#sentinelget_slaves)\n\n\n### new\n\n`syntax: rc = redis_connector.new(params)`\n\nCreates the Redis Connector object, overriding default params with the ones given.\nIn case of failures, returns `nil` and a string describing the error.\n\n\n### connect\n\n`syntax: redis, err = rc:connect(params)`\n\nAttempts to create a connection, according to the `params` supplied, falling back\nto [defaults](#default-parameters) given in `new` or the predefined defaults. If\na connection cannot be made, returns `nil` and a string describing the reason.\n\nNote that `params` given here do not change the connector's own configuration,\nand are only used to alter this particular connection operation. As such, the\nfollowing parameters have no meaning when given in `connect`.\n\n* `keepalive_poolsize`\n* `keepalive_timeout`\n* `connection_is_proxied`\n* `disabled_commands`\n\n\n### set_keepalive\n\n`syntax: ok, err = rc:set_keepalive(redis)`\n\nAttempts to place the given Redis connection on the keepalive pool, according to\ntimeout and poolsize params given in `new` or the predefined defaults.\n\nThis allows an application to release resources without having to keep track of\napplication wide keepalive settings.\n\nReturns `1` or in the case of error, `nil` and a string describing the error.\n\n\n## Utilities\n\nThe following methods are not typically needed, but may be useful if a custom\ninterface is required.\n\n\n### connect_via_sentinel\n\n`syntax: redis, err = rc:connect_via_sentinel(params)`\n\nReturns a Redis connection by first accessing a sentinel as supplied by the\n`params.sentinels` table, and querying this with the `params.master_name` and\n`params.role`.\n\n\n### try_hosts\n\n`syntax: redis, err = rc:try_hosts(hosts)`\n\nTries the hosts supplied in order and returns the first successful connection.\n\n\n### connect_to_host\n\n`syntax: redis, err = rc:connect_to_host(host)`\n\nAttempts to connect to the supplied `host`.\n\n\n### sentinel.get_master\n\n`syntax: master, err = sentinel.get_master(sentinel, master_name)`\n\nGiven a connected Sentinel instance and a master name, will return the current\nmaster Redis instance.\n\n\n### sentinel.get_slaves\n\n`syntax: slaves, err = sentinel.get_slaves(sentinel, master_name)`\n\nGiven a connected Sentinel instance and a master name, will return a list of\nregistered slave Redis instances.\n\n\n# Author\n\nJames Hurst \u003cjames@pintsized.co.uk\u003e\n\n\n# Licence\n\nThis module is licensed under the 2-clause BSD license.\n\nCopyright (c) James Hurst \u003cjames@pintsized.co.uk\u003e\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification,\nare permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n  list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this\n  list of conditions and the following disclaimer in the documentation and/or\n  other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","funding_links":["https://github.com/sponsors/pintsized"],"categories":["Lua","Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledgetech%2Flua-resty-redis-connector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fledgetech%2Flua-resty-redis-connector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledgetech%2Flua-resty-redis-connector/lists"}