-
+ 6AE2E0B4022E647056CD53DF1B08F99F9DBDCEDA1F3EB8CF5A371BFA6F59A4A5CF90DCD9FB594C2D67466E202A684AE5E3BEB3528AE4CB61D4C198A3E493E315blatta/lib/short_buffer.py(0 . 0)(1 . 45)
2104 import time
2105
2106 class ShortBuffer(object):
2107 def __init__(self, state):
2108 self.state = state
2109 self.buffer = {}
2110
2111 def embargo(self, message):
2112 if message.message_hash not in self.buffer.keys():
2113 self.buffer[message.message_hash] = {
2114 'received': time.time(),
2115 'message': message,
2116 'low_bounce_count': message.bounces,
2117 'closest_peers': [message.peer],
2118 'reporting_peers': [message.peer]
2119 }
2120 else:
2121 embargoed_message = self.buffer[message.message_hash]
2122 if message.bounces < embargoed_message['low_bounce_count']:
2123 embargoed_message['low_bounce_count'] = message.bounces
2124 embargoed_message['closest_peers'] = [message.peer]
2125 elif message.bounces == embargoed_message['low_bounce_count']:
2126 embargoed_message['closest_peers'].append(message.peer)
2127 else:
2128 # not interested in the message because the bounce count
2129 # is higher than what we've already got and we just want the
2130 # list of peers closest to the originator
2131 pass
2132 embargoed_message['reporting_peers'].append(message.peer)
2133
2134 def flush(self):
2135 current_time = time.time()
2136 messages = []
2137 for message_with_stats in self.buffer.values():
2138 if (message_with_stats['received'] <
2139 (current_time - int(self.state.get_knob('short_buffer_expiration_seconds')))):
2140 messages.append(message_with_stats)
2141 del self.buffer[message_with_stats['message'].message_hash]
2142 return sorted(messages, key=lambda m: m['message'].timestamp)
2143
2144 def has(self, message_hash):
2145 return self.buffer.get(message_hash)
2146
2147 def drop(self, message_hash):
2148 del self.buffer[message_hash]