Example #1
0
    async def get_all_rules(db: AioRedis) -> list:
        """
        gets all rules

        @param db: db connection
        """
        rules_keys = await DB.fetch_members(rules_set, db)
        coroutines = []
        for key in rules_keys:
            coroutines.append(db.hgetall(key, encoding='utf-8'))
        return await Async.all(coroutines)
Example #2
0
    async def get_rule_by_service_id(service_id: str, db: AioRedis):
        """
        gets rules by service id

        @param service_id (str) service id to get rules by
        @param db: db connection
        """
        keys = await RateLimiter._search_indexes(rule_service_id_index, service_id, db)
        coroutines = []
        for key in keys:
            coroutines.append(db.hgetall(key, encoding='utf-8'))
        return await Async.all(coroutines)
Example #3
0
    async def get_entry_by_host(host: str, db: AioRedis):
        """
        gets entry by host

        @param host: (str) host of entry
        @param db: redis instance
        """
        entries_keys = await RateLimiter._search_indexes(entry_host_index, host, db)
        coroutines = []
        for key in entries_keys:
            coroutines.append(db.hgetall(key, encoding='utf-8'))
        entries = await Async.all(coroutines)
        return list(filter(lambda entry: entry, entries))
Example #4
0
    async def get_entry_by_rule_id(rule_id: str, db: AioRedis):
        """
        gets entry by rule id

        @param rule_id: (str) id of entry
        @param db: redis instance
        """
        entries_keys = await RateLimiter._search_indexes(entry_rule_id_index, rule_id, db)
        coroutines = []
        for key in entries_keys:
            coroutines.append(db.hgetall(key, encoding='utf-8'))
        entries = await Async.all(coroutines)
        return list(filter(lambda entry: entry, entries))
Example #5
0
    async def get_all_entries(db: AioRedis):
        """
        Gets all rate limiter entries

        @param db: (object) db connection
        @return: the records with the provided statusCode
        """
        entries_keys = await DB.fetch_members(entry_set, db)
        coroutines = []
        for key in entries_keys:
            coroutines.append(db.hgetall(key, encoding='utf-8'))
        entries = await Async.all(coroutines)
        return list(filter(lambda entry: entry, entries))
Example #6
0
    async def get_rule_by_status_code(status_code: int, db: AioRedis) -> list:
        """
        Gets a rate limiter rule by the status code

        @param status_code: (string) status code of the rate limiter rule
        @param db: (object) db connection
        @return: the records with the provided status_code
        """
        keys = await RateLimiter._search_indexes(rule_status_code_index, status_code, db)
        coroutines = []
        for key in keys:
            coroutines.append(db.hgetall(key, encoding='utf-8'))
        return await Async.all(coroutines)
Example #7
0
async def list_for(redis_cli: Redis, user_id: str) -> List[UserLocation]:
    if await redis_cli.zrank(_get_key(), user_id) is None:
        return []

    geomembers = await redis_cli.georadiusbymember(_get_key(),
                                                   member=user_id,
                                                   radius=50,
                                                   unit="km")
    locations = await asyncio.gather(*[
        redis_cli.hgetall(_get_key_for(geomember.decode()))
        for geomember in geomembers
    ])
    locations = [_decode(location) for location in locations]
    return [UserLocation(**location) for location in locations]