Skip to content

Commit

Permalink
feat[redis cache plugin]: Add Redis connection parameters full contro…
Browse files Browse the repository at this point in the history
…l support
  • Loading branch information
pierre-claranet committed Jul 4, 2024
1 parent 01d8c7b commit 6d28dd5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- redis cache plugin - add Redis connection parameters full access in order to be able to set advanced socket options, like enabling keep alive, etc...
47 changes: 32 additions & 15 deletions plugins/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
options:
_uri:
description:
- A colon separated string of connection information for Redis.
- The format is V(host:port:db:password), for example V(localhost:6379:0:changeme).
- To use encryption in transit, prefix the connection with V(tls://), as in V(tls://localhost:6379:0:changeme).
- To use redis sentinel, use separator V(;), for example V(localhost:26379;localhost:26379;0:changeme). Requires redis>=2.9.0.
- Simple format:
- A colon separated string of connection information for Redis.
- The format is V(host:port:db:password), for example V(localhost:6379:0:changeme).
- To use encryption in transit, prefix the connection with V(tls://), as in V(tls://localhost:6379:0:changeme).
- To use redis sentinel, use separator V(;), for example V(localhost:26379;localhost:26379;0:changeme). Requires redis>=2.9.0.
- Full connection control format:
- Redis socket connection parameters as <name>=<value> pairs '&' separated.
- Example: V(host=localhost&port=6379&db=0&password=changeme&socket_keepalive=true).
- See https://redis-py2.readthedocs.io/en/latest/ for Redis connection parameters name and accepted values.
required: true
env:
- name: ANSIBLE_CACHE_PLUGIN_CONNECTION
Expand Down Expand Up @@ -94,6 +99,7 @@ class CacheModule(BaseCacheModule):
_sentinel_service_name = None
re_url_conn = re.compile(r'^([^:]+|\[[^]]+\]):(\d+):(\d+)(?::(.*))?$')
re_sent_conn = re.compile(r'^(.*):(\d+)$')
re_full_desc_url_conn = re.compile(r'^(?:([a-z_]+=[^&]+)&)(?:([a-z_]+=[^&]+)&)*([a-z_]+=[^&]+)$')

def __init__(self, *args, **kwargs):
uri = ''
Expand All @@ -112,19 +118,30 @@ def __init__(self, *args, **kwargs):
self._cache = {}
kw = {}

# tls connection
tlsprefix = 'tls://'
if uri.startswith(tlsprefix):
kw['ssl'] = True
uri = uri[len(tlsprefix):]
if self.re_full_desc_url_conn.match(uri):
conn_params = uri.split('&')

# redis sentinel connection
if self._sentinel_service_name:
self._db = self._get_sentinel_connection(uri, kw)
# normal connection
connection = {}

for param in conn_params:
p_key, p_value = param.split('=')
connection[p_key] = p_value

self._db = StrictRedis(**connection)
else:
connection = self._parse_connection(self.re_url_conn, uri)
self._db = StrictRedis(*connection, **kw)
# tls connection
tlsprefix = 'tls://'
if uri.startswith(tlsprefix):
kw['ssl'] = True
uri = uri[len(tlsprefix):]

# redis sentinel connection
if self._sentinel_service_name:
self._db = self._get_sentinel_connection(uri, kw)
# normal connection
else:
connection = self._parse_connection(self.re_url_conn, uri)
self._db = StrictRedis(*connection, **kw)

display.vv('Redis connection: %s' % self._db)

Expand Down

0 comments on commit 6d28dd5

Please sign in to comment.