From 1aab908091fe95688cfcfbed2baf696c11d0fa53 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Fri, 10 Apr 2015 21:32:42 -0500 Subject: [PATCH] allocate_channel_id: use DB to get list of previous allocations --- src/wormhole/servers/relay.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wormhole/servers/relay.py b/src/wormhole/servers/relay.py index bedac04..540f85e 100644 --- a/src/wormhole/servers/relay.py +++ b/src/wormhole/servers/relay.py @@ -199,17 +199,19 @@ class Relay(resource.Resource): self.free_child(channel_id) def allocate_channel_id(self): + c = self.db.execute("SELECT DISTINCT `channel_id` FROM `allocations`") + allocated = set([row["channel_id"] for row in c.fetchall()]) for size in range(1,4): # stick to 1-999 for now available = set() for cid in range(10**(size-1), 10**size): - if cid not in self.channels: + if cid not in allocated: available.add(cid) if available: return random.choice(list(available)) # ouch, 999 currently allocated. Try random ones for a while. for tries in range(1000): cid = random.randrange(1000, 1000*1000) - if cid not in self.channels: + if cid not in allocated: return cid raise ValueError("unable to find a free channel-id")