def purge_expired_peers():
"""
Removes peers who haven't announced in the last internval.
Should be set as a recurring event source in your Zappa config.
"""
if DATASTORE == "DynamoDB":
# This is a costly operation, but I think it has to be done.
# Optimizations (pagination? queries? batching?) welcomed.
all_torrents = table.scan()
for torrent in all_torrents['Items']:
for peer_id in torrent['peers']:
peer_last_announce = int(torrent['peers'][peer_id][0]['last_announce'])
window = datetime.now() - timedelta(seconds=ANNOUNCE_INTERVAL)
window_unix = int(time.mktime(window.timetuple()))
if peer_last_announce < window_unix:
remove_peer_from_info_hash(torrent['info_hash'], peer_id)
else:
# There must be a better way to do this.
# Also, it should probably be done as a recurring function and cache,
# not dynamically every time.
for key in s3_client.list_objects(Bucket=BUCKET_NAME)['Contents']:
if 'peers.json' in key['Key']:
remote_object = s3.Object(BUCKET_NAME, key['Key']).get()
content = remote_object['Body'].read().decode('utf-8')
torrent = json.loads(content)
for peer_id in torrent['peers']:
peer_last_announce = int(torrent['peers'][peer_id]['last_announce'])
window = datetime.now() - timedelta(seconds=ANNOUNCE_INTERVAL)
window_unix = int(time.mktime(window.timetuple()))
if peer_last_announce < window_unix:
remove_peer_from_info_hash(torrent['info_hash'], peer_id)
return
##
# Database
##
评论列表
文章目录