9982-getdata 1 import time
9982-getdata 2
9982-getdata 3 class ShortBuffer(object):
9982-getdata 4 def __init__(self, state):
9982-getdata 5 self.state = state
9982-getdata 6 self.buffer = {}
9982-getdata 7
9982-getdata 8 def embargo(self, message):
9982-getdata 9 if message.message_hash not in self.buffer.keys():
9982-getdata 10 self.buffer[message.message_hash] = {
9982-getdata 11 'received': time.time(),
9982-getdata 12 'message': message,
9982-getdata 13 'low_bounce_count': message.bounces,
9982-getdata 14 'closest_peers': [message.peer],
9982-getdata 15 'reporting_peers': [message.peer]
9982-getdata 16 }
9982-getdata 17 else:
9982-getdata 18 embargoed_message = self.buffer[message.message_hash]
9982-getdata 19 if message.bounces < embargoed_message['low_bounce_count']:
9982-getdata 20 embargoed_message['low_bounce_count'] = message.bounces
9982-getdata 21 embargoed_message['closest_peers'] = [message.peer]
9982-getdata 22 elif message.bounces == embargoed_message['low_bounce_count']:
9982-getdata 23 embargoed_message['closest_peers'].append(message.peer)
9982-getdata 24 else:
9982-getdata 25
9982-getdata 26
9982-getdata 27
9982-getdata 28 pass
9982-getdata 29 embargoed_message['reporting_peers'].append(message.peer)
9982-getdata 30
9982-getdata 31 def flush(self):
9982-getdata 32 current_time = time.time()
9982-getdata 33 messages = []
9982-getdata 34 for message_with_stats in self.buffer.values():
9982-getdata 35 if (message_with_stats['received'] <
9982-getdata 36 (current_time - int(self.state.get_knob('short_buffer_expiration_seconds')))):
9982-getdata 37 messages.append(message_with_stats)
9982-getdata 38 del self.buffer[message_with_stats['message'].message_hash]
9982-getdata 39 return sorted(messages, key=lambda m: m['message'].timestamp)
9982-getdata 40
9982-getdata 41 def has(self, message_hash):
9982-getdata 42 return self.buffer.get(message_hash)
9982-getdata 43
9982-getdata 44 def drop(self, message_hash):
9982-getdata 45 del self.buffer[message_hash]