Exemple #1
0
 def setUp(self):
     pool = ConnectionPool(
         host='localhost', port=6379, db=9,
         encoding='utf-8', decode_responses=True,
         parser_class=HiredisParser)
     self.client = redis.Redis(connection_pool=pool)
     self.client.flushdb()
Exemple #2
0
def _construct_connection_pool(pool):
    """ Construct a blocking socket connection pool based on asynchronous pool
    """
    _pool = ConnectionPool(Connection, pool.max_connections,
                           **pool.connection_kwargs)

    return _pool
Exemple #3
0
    def __init__(self,
                 host='localhost',
                 port=6379,
                 db=0,
                 password=None,
                 socket_timeout=None,
                 connection_pool=None,
                 charset='utf-8',
                 errors='strict',
                 unix_socket_path=None):
        if not connection_pool:
            kwargs = {
                'db': db,
                'password': password,
                'socket_timeout': socket_timeout,
                'encoding': charset,
                'encoding_errors': errors
            }
            # based on input, setup appropriate connection args
            if unix_socket_path:
                kwargs.update({
                    'path': unix_socket_path,
                    'connection_class': UnixDomainSocketConnection
                })
            else:
                kwargs.update({'host': host, 'port': port})
            connection_pool = ConnectionPool(**kwargs)
        self.connection_pool = connection_pool

        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
Exemple #4
0
    def __init__(self,
                 client_ns_versions,
                 host='localhost',
                 port=6379,
                 db=0,
                 password=None,
                 socket_timeout=None,
                 socket_connect_timeout=None,
                 socket_keepalive=None,
                 socket_keepalive_options=None,
                 connection_pool=None,
                 unix_socket_path=None,
                 encoding='utf-8',
                 encoding_errors='strict',
                 charset=None,
                 errors=None,
                 decode_responses=False,
                 retry_on_timeout=False,
                 ssl=False,
                 ssl_keyfile=None,
                 ssl_certfile=None,
                 ssl_cert_reqs=None,
                 ssl_ca_certs=None):

        kwargs = {
            'db': db,
            'password': password,
            'socket_timeout': socket_timeout,
            'encoding': encoding,
            'encoding_errors': encoding_errors,
            'decode_responses': decode_responses,
            'retry_on_timeout': retry_on_timeout,
            'fun_ptr': self.verify_ns_info
        }
        # TCP specific options
        kwargs.update({
            'host': host,
            'port': port,
            'socket_connect_timeout': socket_connect_timeout,
            'socket_keepalive': socket_keepalive,
            'socket_keepalive_options': socket_keepalive_options,
        })

        self.connection_pool = ConnectionPool(connection_class=NoReConnection,
                                              **kwargs)
        self._use_lua_lock = None
        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
        self.client_ns_versions = dict()
        #dict of lists of namespace versions
        self.ns_versions_dict = dict()

        # check to see if we are the initial version and must init
        for (ns, v) in client_ns_versions:
            self.append_new_version(v, ns, startup=True)
        #logging.debug(self.client_ns_versions)

        # check to see if existing updates need to be loaded into this client
        self.upd_dict = dict()
        self.load_upd_tuples()
        self.client_setname(self.ns_dict_as_string())
Exemple #5
0
class RedisClient(Redis):
    """Redis client, 使用一个全局的连接池"""

    _pool = ConnectionPool(**config['redis'])

    def __init__(self, **kwargs):
        kwargs.setdefault('connection_pool', self._pool)
        super().__init__(**kwargs)
Exemple #6
0
    def __init__(self,
                 host='localhost',
                 port=7711,
                 password=None,
                 socket_timeout=None,
                 socket_connect_timeout=None,
                 socket_keepalive=None,
                 socket_keepalive_options=None,
                 connection_pool=None,
                 unix_socket_path=None,
                 encoding='utf-8',
                 encoding_errors='strict',
                 decode_responses=False,
                 retry_on_timeout=False,
                 job_origin_ttl_secs=5,
                 record_job_origin=False):
        """
        job_origin_ttl_secs is the number of seconds to store counts of
        incoming jobs. The higher the throughput you're expecting, the lower
        this number should be.
        """
        self.record_job_origin = record_job_origin
        kwargs = {
            'password': password,
            'socket_timeout': socket_timeout,
            'encoding': encoding,
            'encoding_errors': encoding_errors,
            'decode_responses': decode_responses,
            'retry_on_timeout': retry_on_timeout,
            'db': 0,
        }
        # based on input, setup appropriate connection args
        if unix_socket_path is not None:
            kwargs.update({
                'path': unix_socket_path,
                'connection_class': UnixDomainSocketConnection
            })
        else:
            # TCP specific options
            kwargs.update({
                'host': host,
                'port': port,
                'socket_connect_timeout': socket_connect_timeout,
                'socket_keepalive': socket_keepalive,
                'socket_keepalive_options': socket_keepalive_options,
            })

        if not connection_pool:
            connection_pool = ConnectionPool(**kwargs)

        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()

        self.connection_pool = {'default': connection_pool}
        self.default_node = 'default'

        self._job_score = RollingCounter(ttl_secs=job_origin_ttl_secs)

        self.__connect_cluster(kwargs)
Exemple #7
0
    def __connect_cluster(self, connection_kwargs):
        hi = self.hello()

        self.default_node = bin_to_str(hi['id'][:8])
        self.connection_pool.pop('default')
        for node, ip, port, version in hi['nodes']:
            connection_kwargs.update(dict(host=ip, port=port))
            self.connection_pool[bin_to_str(
                node[:8])] = ConnectionPool(**connection_kwargs)
Exemple #8
0
 def __init__(self, hosts, **kwargs):
     self.pools = []
     for host in hosts:
         pool = ConnectionPool(host=host['host'],
                               port=host['port'],
                               **kwargs)
         self.pools.append(pool)
     self._len = len(pool)
     self.idx = 0
Exemple #9
0
    def test_conflicting_init_args(self):
        options = {'host': 'localhost', 'url': 'redis://localhost',
                   'connection_pool': ConnectionPool()}
        combinations = itertools.combinations(options.items(), 2)
        for kwargs in (dict(item) for item in combinations):
            self.assertRaises(ConfigurationError, lambda: RedisHuey(**kwargs))

        # None values are fine, however.
        RedisHuey(host=None, port=None, db=None, url='redis://localhost')
Exemple #10
0
 def __init__(self, hosts, **kwargs):
     host = hosts[0]
     if isinstance(host, str):
         self.pool = ConnectionPool.from_url(host, **kwargs)
     else:
         db = host.get('db', 0)
         self.pool = ConnectionPool(host=host['host'],
                                    port=host['port'],
                                    db=db,
                                    **kwargs)
Exemple #11
0
def normal_poll():
    pool = ConnectionPool(host='127.0.0.1', port=6379, db=0)
    cli = Redis(connection_pool=pool)
    poller = poll()
    conns = [(key, cli.connection_pool.get_connection('_')) for key in keys]
    register(conns, poller)
    try:
        start_poll(conns, poller, cli)
    except KeyboardInterrupt:
        unregister(conns, poller)
Exemple #12
0
    def test_conflicting_init_args(self):
        options = {
            'host': 'localhost',
            'url': 'redis://localhost',
            'connection_pool': ConnectionPool()
        }
        combinations = itertools.combinations(options.items(), 2)

        for kwargs in (dict(item) for item in combinations):
            self.assertRaises(ValueError, lambda: RedisStorage(**kwargs))
Exemple #13
0
 def test_pool(self):
     pool = ConnectionPool(host=self.ip,
                           port=self.port,
                           db=1,
                           max_connections=20)
     r = Redis(connection_pool=pool)
     r.set("huawei", "huawei")
     print r.get("huawei")
     r.delete("huawei")
     print r.ping()
     print r.get("huawei")
     print 2**2
Exemple #14
0
 def __init__(self,
              ip="",
              port=6379,
              defalutDb=1,
              max_connections=30,
              password=""):
     self.pool = ConnectionPool(host=ip,
                                port=port,
                                db=defalutDb,
                                max_connections=max_connections,
                                password=password)
     self.redis = Redis(connection_pool=self.pool)
Exemple #15
0
    def init_pools(self, hosts, **kwargs):
        # Ensure consistency of db value
        kwargs.update({'db': 0})
        for host in hosts:
            if isinstance(host, str):
                addr = self.parse_addr(host)
                pool = ConnectionPool.from_url(host, **kwargs)
            else:
                addr = '{}:{}'.format(host['host'], host['port'])
                pool = ConnectionPool(host=host['host'],
                                      port=host['port'],
                                      **kwargs)
            self.cluster_pools[addr] = pool

        self.reset_slots(**kwargs)
Exemple #16
0
 def get_pool(self, key=None):
     if not key:
         pool = self.get_random_pool()
         return pool
     addr = self._key_to_addr(key)
     if addr:
         pool = self.cluster_pools.get(addr)
         if not pool:
             host, port = addr.split(':')
             pool = ConnectionPool(host=host,
                                   port=int(port),
                                   **self.pool_kwargs)
             self.cluster_pools[addr] = pool
         return pool
     raise ClusterError("No slot found for key...", key)
Exemple #17
0
def _shared_pool(**opts):
    if "host" in opts:
        key = "{}:{}/{}".format(opts["host"], opts["port"], opts["db"])
    else:
        key = "{}/{}".format(opts["path"], opts["db"])
    pool = _pool_cache.get(key)
    if pool is not None:
        return pool
    with _pool_lock:
        pool = _pool_cache.get(key)
        if pool is not None:
            return pool
        pool = ConnectionPool(**opts)
        _pool_cache[key] = pool
        return pool
Exemple #18
0
def _shared_pool(**opts):
    if 'host' in opts:
        key = '%s:%s/%s' % (opts['host'], opts['port'], opts['db'], )
    else:
        key = '%s/%s' % (opts['path'], opts['db'])
    pool = _pool_cache.get(key)
    if pool is not None:
        return pool
    with _pool_lock:
        pool = _pool_cache.get(key)
        if pool is not None:
            return pool
        pool = ConnectionPool(**opts)
        _pool_cache[key] = pool
        return pool
Exemple #19
0
 def __init__(self,
              host='localhost',
              port=6379,
              db=0,
              password=None,
              connection_pool=None,
              max_connections=None):
     if not connection_pool:
         kwargs = {
             'host': host,
             'port': port,
             'db': db,
             'password': password,
             'max_connections': max_connections
         }
         connection_pool = ConnectionPool(**kwargs)
     self.connection_pool = connection_pool
Exemple #20
0
    def reset_slots(self, **kwargs):
        pool = self.get_random_pool()
        resp = pool.execute_command('CLUSTER', 'SLOTS')
        if not isinstance(resp, (list, tuple)):
            raise RedisError('Unable to locate redis slots.')

        _pools = {}
        for elem in resp:
            _start, _end, master = elem[0], elem[1], elem[2]
            ip, port = self._stringconv(master[0]), self._stringconv(master[1])
            addr = '{}:{}'.format(ip, port)

            for i in range(int(_start), int(_end) + 1):
                self.mapping[i] = addr

            if addr not in _pools:
                p = (self.cluster_pools.pop(addr, None)
                     or ConnectionPool(host=ip, port=port, **kwargs))
                p.addr = addr
                _pools[addr] = p

        self.cluster_pools.clear()
        self.cluster_pools = _pools
Exemple #21
0
# -*- coding: utf-8 -*-

import redis
import threading
import logging
from redis.connection import ConnectionPool

redis_settings = {"servers": "localhost", "port": 6379, "db": 11}

_pool0 = ConnectionPool(**redis_settings)


class RedisClient(object):

    instance = None
    locker = threading.Lock()

    def __init__(self):
        """ intialize the client of redis  include port db and servers """
        try:
            self.servers = redis_settings["servers"]
            self.port = redis_settings["port"]
            self.db = redis_settings["db"]
            self.redis = redis.Redis(connection_pool=_pool0)
        except Exception, e:
            logging.error(e)

    @classmethod
    def get_instance(cls):
        """
        get the instance of RedisClient
def getPool():
    return ConnectionPool(host='223.4.155.152')
Exemple #23
0
# encoding: utf-8
import tornado.ioloop
import redis

from tornado import gen
from tornado.web import RequestHandler, Application
from redis.connection import ConnectionPool
from async_redis.connection import AsyncConnection
from async_redis.client import AsyncRedis


connection_pool = ConnectionPool(AsyncConnection, host='localhost', port=6379, db=0, socket_connect_timeout=0.00000001)
async_redis = AsyncRedis(connection_pool=connection_pool)
sync_redis = redis.StrictRedis()


class MainHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        key1, value1 = 'my_key1', 'value1'
        key2, value2 = 'my_key2', 'value2'
        key3, value3 = 'my_key3', 'value3'

        with async_redis.pipeline() as p:
            p.set(key1, value1)
            p.set(key2, value2)
            p.set(key3, value3)
            p.get(key1)
            p.get(key2)
            p.get(key3)
            result = yield p.execute()
Exemple #24
0
import redis
from redis.connection import ConnectionPool, PythonParser

from config import CONFIG as cfg

# Because of a bug (https://github.com/andymccurdy/redis-py/issues/318) with
# script reloading in `redis-py, we need to force the `PythonParser` to prevent
# sixpack from crashing if redis restarts (or scripts are flushed).
pool = ConnectionPool(host=cfg.get('redis_host'),
                      port=cfg.get('redis_port'),
                      db=cfg.get('redis_db'),
                      parser_class=PythonParser)
REDIS = redis.StrictRedis(connection_pool=pool)
DEFAULT_PREFIX = cfg.get('redis_prefix')


def _key(k):
    return "{0}:{1}".format(DEFAULT_PREFIX, k)


monotonic_zadd = REDIS.register_script("""
    local sequential_id = redis.call('zscore', KEYS[1], ARGV[1])
    if not sequential_id then
        sequential_id = redis.call('zcard', KEYS[1])
        redis.call('zadd', KEYS[1], sequential_id, ARGV[1])
    end
    return sequential_id
""")


def sequential_id(k, identifier):
Exemple #25
0
# sixpack from crashing if redis restarts (or scripts are flushed).
if cfg.get('redis_sentinels'):
    from redis.sentinel import Sentinel, SentinelConnectionPool
    service_name = cfg.get('redis_sentinel_service_name')
    sentinel = Sentinel(sentinels=cfg.get('redis_sentinels'),
                        password=cfg.get('redis_password', None),
                        socket_timeout=cfg.get('redis_socket_timeout'))
    pool = SentinelConnectionPool(service_name, sentinel,
                                db=cfg.get('redis_db'),
                                max_connections=cfg.get('redis_max_connections'),
                                parser_class=PythonParser)
else:
    from redis.connection import ConnectionPool
    pool = ConnectionPool(host=cfg.get('redis_host'),
                        port=cfg.get('redis_port'),
                        password=cfg.get('redis_password', None),
                        db=cfg.get('redis_db'),
                        max_connections=cfg.get('redis_max_connections'),
                        parser_class=PythonParser)

REDIS = redis.StrictRedis(connection_pool=pool)
DEFAULT_PREFIX = cfg.get('redis_prefix')


def _key(k):
    return "{0}:{1}".format(DEFAULT_PREFIX, k)


monotonic_zadd = REDIS.register_script("""
    local sequential_id = redis.call('zscore', KEYS[1], ARGV[1])
    if not sequential_id then
        sequential_id = redis.call('zcard', KEYS[1])
Exemple #26
0
 def __init__(self):
     pool = ConnectionPool(host=cfg.REDIS_HOST, port=cfg.REDIS_PORT)
     self.client = Redis(connection_pool=pool)