From f46a862aacf6882d571f4e67fe53ee379cf785b9 Mon Sep 17 00:00:00 2001 From: James Baldassari Date: Sun, 25 Aug 2019 00:52:49 -0400 Subject: [PATCH] Make Redis timeout configurable via environment variable --- README.md | 1 + api/ceryx/db.py | 5 +++-- api/ceryx/settings.py | 1 + ceryx/nginx/conf/nginx.conf.tmpl | 3 ++- ceryx/nginx/lualib/ceryx/redis.lua | 3 ++- docker-compose.yml | 2 ++ 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index def7bff..69417e4 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Ceryx is configured with the following environment variables: - `CERYX_REDIS_PASSWORD`: Optional password to use for authenticating with Redis (default: None) - `CERYX_REDIS_PORT`: The where Redis should be reached (default: `6379`) - `CERYX_REDIS_PREFIX`: The prefix to use in Ceryx-related Redis keys (default: `ceryx`) + - `CERYX_REDIS_TIMEOUT`: The timeout for all Redis operations, including the intial connection to Redis, specified in milliseconds (default: `100`) - `CERYX_SSL_DEFAULT_CERTIFICATE`: The path to the fallback SSL certificate (default: `/etc/ceryx/ssl/default.crt` — randomly generated at build time) - `CERYX_SSL_DEFAULT_KEY`: The path to the fallback SSL certificate key (default: `/etc/ceryx/ssl/default.key` — randomly generated at build time) diff --git a/api/ceryx/db.py b/api/ceryx/db.py index 052894b..d26b2ae 100644 --- a/api/ceryx/db.py +++ b/api/ceryx/db.py @@ -23,10 +23,11 @@ def from_config(path=None): settings.REDIS_PASSWORD, 0, settings.REDIS_PREFIX, + settings.REDIS_TIMEOUT, ) - def __init__(self, host, port, password, db, prefix): - self.client = redis.StrictRedis(host=host, port=port, password=password, db=db) + def __init__(self, host, port, password, db, prefix, timeout): + self.client = redis.StrictRedis(host=host, port=port, password=password, db=db, socket_timeout=timeout, socket_connect_timeout=timeout) self.prefix = prefix def _prefixed_key(self, key): diff --git a/api/ceryx/settings.py b/api/ceryx/settings.py index c07fc07..ddfa891 100644 --- a/api/ceryx/settings.py +++ b/api/ceryx/settings.py @@ -20,3 +20,4 @@ REDIS_PORT = int(os.getenv("CERYX_REDIS_PORT", 6379)) REDIS_PASSWORD = os.getenv("CERYX_REDIS_PASSWORD", None) REDIS_PREFIX = os.getenv("CERYX_REDIS_PREFIX", "ceryx") +REDIS_TIMEOUT = int(os.getenv("CERYX_REDIS_TIMEOUT", 100)) * 0.001 # Python Redis client requires units of seconds diff --git a/ceryx/nginx/conf/nginx.conf.tmpl b/ceryx/nginx/conf/nginx.conf.tmpl index 68d45e7..db04840 100644 --- a/ceryx/nginx/conf/nginx.conf.tmpl +++ b/ceryx/nginx/conf/nginx.conf.tmpl @@ -7,6 +7,7 @@ env CERYX_REDIS_PREFIX; env CERYX_REDIS_HOST; env CERYX_REDIS_PASSWORD; env CERYX_REDIS_PORT; +env CERYX_REDIS_TIMEOUT; events { worker_connections 1024; @@ -54,4 +55,4 @@ http { include mime.types; include ceryx.conf; include ../sites-enabled/*; -} \ No newline at end of file +} diff --git a/ceryx/nginx/lualib/ceryx/redis.lua b/ceryx/nginx/lualib/ceryx/redis.lua index 8260601..a3bae84 100644 --- a/ceryx/nginx/lualib/ceryx/redis.lua +++ b/ceryx/nginx/lualib/ceryx/redis.lua @@ -5,6 +5,7 @@ local prefix = utils.getenv("CERYX_REDIS_PREFIX", "ceryx") local host = utils.getenv("CERYX_REDIS_HOST", "127.0.0.1") local port = utils.getenv("CERYX_REDIS_PORT", 6379) local password = utils.getenv("CERYX_REDIS_PASSWORD", nil) +local timeout = utils.getenv("CERYX_REDIS_TIMEOUT", 100) -- 100 ms local exports = {} @@ -13,7 +14,7 @@ function exports.client() ngx.log(ngx.DEBUG, "Preparing Redis client.") local red = redis:new() - red:set_timeout(100) -- 100 ms + red:set_timeout(timeout) local res, err = red:connect(host, port) diff --git a/docker-compose.yml b/docker-compose.yml index c9a3785..d39f449 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,7 @@ services: CERYX_DOCKERIZE_EXTRA_ARGS: -no-overwrite CERYX_REDIS_HOST: ${CERYX_REDIS_HOST:-redis} CERYX_REDIS_PORT: ${CERYX_REDIS_PORT:-6379} + CERYX_REDIS_TIMEOUT: ${CERYX_REDIS_TIMEOUT:-100} command: - usr/local/openresty/bin/openresty - -g @@ -32,6 +33,7 @@ services: CERYX_DEBUG: ${CERYX_DEBUG:-false} CERYX_REDIS_HOST: ${CERYX_REDIS_HOST:-redis} CERYX_REDIS_PORT: ${CERYX_REDIS_PORT:-6379} + CERYX_REDIS_TIMEOUT: ${CERYX_REDIS_TIMEOUT:-100} redis: image: redis:3.2.11-alpine