Skip to content

Commit

Permalink
added ability to handle redis password
Browse files Browse the repository at this point in the history
  • Loading branch information
biplap-sarkar committed May 29, 2016
1 parent bb364c3 commit c46a767
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
20 changes: 12 additions & 8 deletions pylimit/pyratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def __init__(self):
self.limit = None # type: int

@classmethod
def init(cls, redis_host: str, redis_port: int, is_sentinel_redis=False, redis_sentinel_service="mymaster"):
def init(cls, redis_host: str, redis_port: int, is_sentinel_redis=False, redis_sentinel_service="mymaster",
redis_password=None):
"""
Initializes redis connection
:param redis_host: Hostname of redis server
Expand All @@ -22,25 +23,28 @@ def init(cls, redis_host: str, redis_port: int, is_sentinel_redis=False, redis_s
:type redis_port:int
:param is_sentinel_redis: Parameter indicating if redis server is a sentinel server. Default is false
:type: bool
:type is_sentinel_redis: bool
:param redis_sentinel_service: If redis server is a sentinel server, service name for redis sentinel
:type str
:type redis_sentinel_service: str
:param redis_password: Password for redis connection
:type redis_password: str
"""
if not cls.redis_helper:
cls.redis_helper = RedisHelper(host=redis_host, port=redis_port, is_sentinel=is_sentinel_redis,
sentinel_service=redis_sentinel_service)
sentinel_service=redis_sentinel_service, password=redis_password)

def create(self, period: int, limit: int):
"""
Creates a rate limiting rule with rate limiting period and attempt limit
:param period: Rate limiting period in seconds
:type: int
:type period: int
:param limit: Number of attempts permitted by rate limiting within a given period
:type: int
:type limit: int
"""
self.period = period
Expand All @@ -51,7 +55,7 @@ def __can_attempt(self, namespace: str, add_attempt=True) -> bool:
Checks if a namespace is rate limited or not with including/excluding the current call
:param namespace: Rate limiting namespace
:type: str
:type namespace: str
:param add_attempt: Boolean value indicating if the current call should be considered as an attempt or not
:type add_attempt: bool
Expand Down Expand Up @@ -83,7 +87,7 @@ def attempt(self, namespace: str):
Records an attempt and returns true of false depending on whether attempt can go through or not
:param namespace: Rate limiting namespace
:type: str
:type namespace: str
:return: Returns true if attempt can go ahead under current rate limiting rules, false otherwise
"""
Expand Down
19 changes: 14 additions & 5 deletions pylimit/redis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@


class RedisHelper(object):
def __init__(self, host: str, port: int, is_sentinel=False, sentinel_service=None):
def __init__(self, host: str, port: int, is_sentinel=False, sentinel_service=None, password=None):
self.host = host
self.port = port
self.is_sentinel = is_sentinel
self.sentinel_service = sentinel_service
self.password = password

def get_connection(self, is_read_only=False) -> redis.StrictRedis:
"""
Expand All @@ -21,13 +22,17 @@ def get_connection(self, is_read_only=False) -> redis.StrictRedis:
:return: Returns a StrictRedis connection
"""
if self.is_sentinel:
sentinel = Sentinel([(self.host, self.port)])
kwargs = dict()
if self.password:
kwargs["password"] = self.password
sentinel = Sentinel([(self.host, self.port)], **kwargs)
if is_read_only:
connection = sentinel.slave_for(self.sentinel_service, decode_responses=True)
else:
connection = sentinel.master_for(self.sentinel_service, decode_responses=True)
else:
connection = redis.StrictRedis(host=self.host, port=self.port, decode_responses=True)
connection = redis.StrictRedis(host=self.host, port=self.port, decode_responses=True,
password=self.password)
return connection

def get_atomic_connection(self) -> StrictPipeline:
Expand All @@ -37,9 +42,13 @@ def get_atomic_connection(self) -> StrictPipeline:
:return: Returns a StrictPipeline object
"""
if self.is_sentinel:
sentinel = Sentinel([(self.host, self.port)])
kwargs = dict()
if self.password:
kwargs["password"] = self.password
sentinel = Sentinel([(self.host, self.port)], **kwargs)
pipeline = sentinel.master_for(self.sentinel_service, decode_responses=True).pipeline(True)
else:
pipeline = redis.StrictRedis(host=self.host, port=self.port, decode_responses=True).pipeline(True)
pipeline = redis.StrictRedis(host=self.host, port=self.port, decode_responses=True,
password=self.password).pipeline(True)
return pipeline

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
setup(
name = 'pylimit',
packages = ['pylimit'],
version = '0.1.4',
version = '0.1.5',
description = 'A distributed rate limiting library for python using leaky bucket algorithm and Redis',
author='Biplap Sarkar',
author_email='[email protected]',
url='https://github.com/biplap-sarkar/pylimit',
download_url='https://github.com/biplap-sarkar/pylimit/archive/v0.1.4.tar.gz',
download_url='https://github.com/biplap-sarkar/pylimit/archive/v0.1.5.tar.gz',
keywords=['rate limiting', 'throttle', 'redis'],
classifiers=[],
)

0 comments on commit c46a767

Please sign in to comment.