示例#1
0
def get_content_size(key, redis_client=redis_client):
    data = redis_client.get(key)
    if not data:
        return 0
    # res = json.dumps(data)
    s = len(data)/1024
    return s
示例#2
0
def get_content_size(key, redis_client=redis_client):
    data = redis_client.get(key)
    if not data:
        return 0

    s = len(data) / 1024
    return s
示例#3
0
def revoke_all_tokens_of_user(user_id):

    key = f'user-id-{user_id}'
    tokens = redis_client.get(key)
    if tokens:
        tokens = json.loads(tokens)
        for token in tokens:
            revoke_token(token)
示例#4
0
    def __init__(self, room_dict, user_dict=None):
        self.user_dict = user_dict
        self.room_dict = room_dict
        self.user_count = 0

        room_in_cache = redis_client.get(f'room-{room_dict["id"]}')
        if room_in_cache:
            room_in_cache = json.loads(room_in_cache)
            self.user_count = len(room_in_cache.get('users', []))
示例#5
0
def analyze_room(room_id):
    data = redis_client.get(room_id)
    if data:
        room_data = json.loads(data)
        room_size = len(room_data) / 1024
        print(f'room size {room_size} kb')
        users = room_data['users']
        for u in users:
            print(f"{u['name']} connections: {len(u['connections'])}")
示例#6
0
def get_room(room_id):
    if not room_id:
        return None
    data = redis_client.get(room_id)
    if data:
        room = json.loads(data)
        # ensure basic fields
        room['users'] = room.get('users', [])
        return room
    return None
示例#7
0
def add_token_to_user(user_id, token):
    # one user can have multiple tokens, so value is list
    key = f'user-id-{user_id}'
    tokens = redis_client.get(key)
    if tokens:
        tokens = json.loads(tokens)
    else:
        tokens = []
    tokens.append(token)
    redis_client.set(key, json.dumps(tokens))
示例#8
0
def remove_token_from_user(user_id, token):
    key = f'user-id-{user_id}'
    tokens = redis_client.get(key)
    if tokens:
        tokens = json.loads(tokens)
        tokens = [t for t in tokens if t != token]
        redis_client.set(key, json.dumps(tokens))

    else:
        # TODO: log error
        pass
示例#9
0
def kill_ghost_connections():
    # TODO: mock scan_iter() when not using redis
    # low priority, when it's single instance without redis
    # ghost connections are rare
    for key in redis_client.scan_iter("room*"):
        data = redis_client.get(key)
        if data:
            # value might already be deleted by another
            # ghost buster thread
            room = json.loads(data)

            now = time.time()
            user_ids_to_be_removed = []
            for user in room['users']:
                connection_ids_to_be_removed = []

                for connection in user['connections']:
                    time_elapse = now - connection['heartbeat']

                    if time_elapse > HEARTBEAT_TIMOUT:
                        connection_id = connection['id']
                        logger.info(
                            f'[{key}] ghost connection {connection_id} will be removed'
                        )
                        connection_ids_to_be_removed.append(connection_id)

                user['connections'] = [
                    c for c in user['connections']
                    if c['id'] not in connection_ids_to_be_removed
                ]
                if (len(user['connections']) == 0):
                    user_id = user['id']
                    logger.info(f'ghost user {user_id} will be removed')
                    user_ids_to_be_removed.append(user_id)

            room['users'] = [
                u for u in room['users']
                if u['id'] not in user_ids_to_be_removed
            ]

            if len(room['users']) == 0:
                logger.info(f'ghost room {key} will be removed')
                redis_client.delete(key)
            else:
                if len(connection_ids_to_be_removed) > 0:
                    upsert_room(room)
示例#10
0
def get_connection(connection_id):
    data = redis_client.get(connection_id)
    if data:
        connection = json.loads(data)
        return connection
    return None
示例#11
0
def get_user(token):
    user = redis_client.get(token)
    if user:
        user = json.loads(user)

    return user
示例#12
0
def _delete_connection_from_connections(connection_id):
    data = redis_client.get(connection_id)
    if data:
        connection = json.loads(data)
        redis_client.delete(connection_id)
        return connection