def __init__( self, hosts, host_defaults=None, pool_cls=None, pool_options=None, router_cls=None, router_options=None, ): if pool_cls is None: pool_cls = ConnectionPool if router_cls is None: router_cls = PartitionRouter self._lock = Lock() self.pool_cls = pool_cls self.pool_options = pool_options self.router_cls = router_cls self.router_options = router_options self._pools = {} self._router = None self.hosts = {} self._hosts_age = 0 self.host_defaults = host_defaults or {} for host_config in _iter_hosts(hosts): if self.host_defaults: for k, v in iteritems(self.host_defaults): host_config.setdefault(k, v) self.add_host(**host_config)
def _promise_from_dict(d): d = dict((k, _ensure_promise(v)) for k, v in iteritems(d)) if not d: return Promise.resolved({}) pending = set(d.keys()) rv = Promise() def on_success(key, value): pending.discard(key) if not pending: rv.resolve(dict((k, p.value) for k, p in iteritems(d))) for key, promise in iteritems(d): promise.done(partial(on_success, key), rv.reject) return rv
def _iter_hosts(iterable): if isinstance(iterable, dict): iterable = iteritems(iterable) for item in iterable: if isinstance(item, tuple): host_id, cfg = item cfg = dict(cfg) cfg["host_id"] = host_id else: cfg = item yield cfg
def get_pool_for_host(self, host_id): """Returns the connection pool for the given host. This connection pool is used by the redis clients to make sure that it does not have to reconnect constantly. If you want to use a custom redis client you can pass this in as connection pool manually. """ if isinstance(host_id, HostInfo): host_info = host_id host_id = host_info.host_id else: host_info = self.hosts.get(host_id) if host_info is None: raise LookupError("Host %r does not exist" % (host_id, )) rv = self._pools.get(host_id) if rv is not None: return rv with self._lock: rv = self._pools.get(host_id) if rv is None: opts = dict(self.pool_options or ()) opts["db"] = host_info.db opts["password"] = host_info.password if host_info.unix_socket_path is not None: opts["path"] = host_info.unix_socket_path opts["connection_class"] = UnixDomainSocketConnection if host_info.ssl: raise TypeError("SSL is not supported for unix " "domain sockets.") else: opts["host"] = host_info.host opts["port"] = host_info.port if host_info.ssl: if SSLConnection is None: raise TypeError("This version of py-redis does " "not support SSL connections.") opts["connection_class"] = SSLConnection opts.update( ("ssl_" + k, v) for k, v in iteritems(host_info.ssl_options or {})) rv = self.pool_cls(**opts) self._pools[host_id] = rv return rv
def mset(self, *args, **kwargs): return Promise.all([ self.set(k, v) for k, v in iteritems(dict(*args, **kwargs)) ]).then(lambda x: None)
def on_success(key, value): pending.discard(key) if not pending: rv.resolve(dict((k, p.value) for k, p in iteritems(d)))