示例#1
0
def parse_redis_dsn(
        raw_str: str = 'redis://localhost:6379') -> t.Dict[str, t.Any]:
    # _, opts = parse_url(raw_str)
    # Не стоит использовать имена переменных _ и __. У них особое назначение
    # Так же нет смысла заводить переменную, которая не будет использоваться
    opts = parse_url(raw_str)[1]
    opts.setdefault('db', 0)
    parsed = furl(raw_str)

    address = furl()
    for attr in ['scheme', 'host', 'port']:
        setattr(address, attr, getattr(parsed, attr))

    opts.update({
        'address':
        str(address),
        'create_connection_timeout':
        parsed.args.get('create_connection_timeout', None),
        'minsize':
        int(parsed.args.get('minsize', 1)),
        'maxsize':
        int(parsed.args.get('maxsize', 10)),
        'name':
        parsed.args.get('name', ''),
        'display_name':
        parsed.args.get('display_name', ''),
        'service':
        coerce_str_to_bool(parsed.args.get('service', False)),
    })

    return opts
示例#2
0
def widgets_create_pool(*,
                        address,
                        db=None,
                        password=None,
                        ssl=None,
                        encoding=None,
                        size=8,
                        create_connection_timeout=None):
    address, options = parse_url(address)
    db = options.setdefault('db', db)
    password = options.setdefault('password', password)
    encoding = options.setdefault('encoding', encoding)
    create_connection_timeout = options.setdefault('timeout',
                                                   create_connection_timeout)
    if 'ssl' in options:
        assert options['ssl'] or (not options['ssl'] and not ssl), (
            'Conflicting ssl options are set', options['ssl'], ssl)
        ssl = ssl or options['ssl']

    return WidgetsRedisConnectionsPool(
        address=address,
        db=db,
        password=password,
        encoding=encoding,
        minsize=size,
        maxsize=size,
        ssl=ssl,
        create_connection_timeout=create_connection_timeout)
示例#3
0
 def get_alias(redis_arg):
     if redis_arg is None:
         raise
     address = redis_arg.pop('address', None)
     db = redis_arg.pop('db', None)
     if isinstance(address, str):
         address, options = parse_url(address)
         db = options.setdefault('db', db)
         redis_arg.update(options)
         redis_arg.update({"address": address})
     alias = redis_arg.pop('alias',
                           ''.join([str(i) for i in address]) + str(db))
     return alias, redis_arg
示例#4
0
def test_db_num_assertions(url):
    with pytest.raises(AssertionError, match="Invalid decimal integer"):
        parse_url(url)
示例#5
0
def test_url_assertions(url, expected_error):
    with pytest.raises(AssertionError) as exc_info:
        parse_url(url)
    assert exc_info.value.args == (expected_error, )
示例#6
0
def test_good_url(url, expected_address, expected_options):
    address, options = parse_url(url)
    assert address == expected_address
    assert options == expected_options
 def __init__(self, celery):
     self.celery: Celery = celery
     address, options = parse_url(celery.conf.result_backend)
     self.redis_pool: Optional[ConnectionsPool] = ConnectionsPool(
         address, minsize=0, maxsize=10, **options)
示例#8
0
async def create_cluster(
    startup_nodes: Sequence[AioredisAddress],
    *,
    # failover options
    retry_min_delay: float = None,
    retry_max_delay: float = None,
    max_attempts: int = None,
    attempt_timeout: float = None,
    # manager options
    state_reload_frequency: float = None,
    state_reload_interval: float = None,
    follow_cluster: bool = None,
    # pool options
    idle_connection_timeout: float = None,
    # node client options
    password: str = None,
    encoding: str = None,
    pool_minsize: int = None,
    pool_maxsize: int = None,
    connect_timeout: float = None,
) -> AbcCluster:
    corrected_nodes: List[Address] = []
    for mixed_addr in startup_nodes:
        if isinstance(mixed_addr, str):
            parsed_addr, options = parse_url(mixed_addr)
            addr = Address(parsed_addr[0], parsed_addr[1])
        else:
            addr = Address(mixed_addr[0], mixed_addr[1])
        corrected_nodes.append(addr)

    # shuffle startup nodes for every instance
    # for distribute initial load to cluster
    if len(corrected_nodes) > 1:
        random.shuffle(corrected_nodes)

    if state_reload_frequency is not None:
        warnings.warn(
            "`state_reload_frequency` is deprecated and is no affect anything",
            DeprecationWarning,
        )

    cluster = Cluster(
        corrected_nodes,
        retry_min_delay=retry_min_delay,
        retry_max_delay=retry_max_delay,
        max_attempts=max_attempts,
        state_reload_interval=state_reload_interval,
        follow_cluster=follow_cluster,
        idle_connection_timeout=idle_connection_timeout,
        password=password,
        encoding=encoding,
        pool_minsize=pool_minsize,
        pool_maxsize=pool_maxsize,
        connect_timeout=connect_timeout,
        attempt_timeout=attempt_timeout,
    )
    try:
        await cluster._init()
    except Exception:
        cluster.close()
        await cluster.wait_closed()
        raise

    return cluster
示例#9
0
def test_good_url(url, expected_address, expected_options):
    address, options = parse_url(url)
    assert address == expected_address
    assert options == expected_options
示例#10
0
def test_db_num_assertions(url):
    with pytest.raises(AssertionError, match="Invalid decimal integer"):
        parse_url(url)
示例#11
0
def test_url_assertions(url, expected_error):
    with pytest.raises(AssertionError) as exc_info:
        parse_url(url)
    assert exc_info.value.args == (expected_error,)