Skip to content

Commit

Permalink
Add 'service_name' to redis sentinel scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Oct 5, 2015
1 parent 11f0b3b commit 0e62fe1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions limits/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,20 @@ def __init__(self, uri, **options):
:raise ConfigurationError: when the redis library is not available
or if the redis master host cannot be pinged.
"""
if "service_name" not in options:
raise ConfigurationError("'service_name' is a required argument")
if not get_dependency("redis"):
raise ConfigurationError("redis prerequisite not available") # pragma: no cover

parsed = urllib.parse.urlparse(uri)
self.service_name = options.get("service_name")
self.sentinel_configuration = []
for loc in parsed.netloc.split(","):
host, port = loc.split(":")
self.sentinel_configuration.append((host, int(port)))

self.service_name = (
parsed.path.replace("/", "") if parsed.path
else options.get("service_name", None)
)
if self.service_name is None:
raise ConfigurationError("'service_name' not provided")
self.sentinel = get_dependency("redis.sentinel").Sentinel(
self.sentinel_configuration,
socket_timeout=options.get("socket_timeout", 0.2)
Expand Down
1 change: 1 addition & 0 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_storage_string(self):
self.assertTrue(isinstance(storage_from_string("redis://localhost:6379"), RedisStorage))
self.assertTrue(isinstance(storage_from_string("memcached://localhost:11211"), MemcachedStorage))
self.assertTrue(isinstance(storage_from_string("redis+sentinel://localhost:26379", service_name="localhost-redis-sentinel"), RedisSentinelStorage))
self.assertTrue(isinstance(storage_from_string("redis+sentinel://localhost:26379/localhost-redis-sentinel"), RedisSentinelStorage))
self.assertRaises(ConfigurationError, storage_from_string, "blah://")
self.assertRaises(ConfigurationError, storage_from_string, "redis+sentinel://localhost:26379")

Expand Down

0 comments on commit 0e62fe1

Please sign in to comment.