summaryrefslogtreecommitdiff
path: root/sleekxmpp
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2011-06-29 09:20:48 +0800
committerLance Stout <lancestout@gmail.com>2011-07-16 11:00:59 +0800
commit45412fd404157d4afc1d08eb5b785b94a55e957c (patch)
treec6c0b3b3145041329d865d8c111c7199df70ee06 /sleekxmpp
parentccc6ab128128990babbf63d4586cdd43384ae320 (diff)
downloadslixmpp-45412fd404157d4afc1d08eb5b785b94a55e957c.tar.gz
slixmpp-45412fd404157d4afc1d08eb5b785b94a55e957c.tar.bz2
slixmpp-45412fd404157d4afc1d08eb5b785b94a55e957c.tar.xz
slixmpp-45412fd404157d4afc1d08eb5b785b94a55e957c.zip
Do a weighted choice among the highest prioritized items based on weight instead of a weighted choice based on priorities.
Diffstat (limited to 'sleekxmpp')
-rw-r--r--sleekxmpp/clientxmpp.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/sleekxmpp/clientxmpp.py b/sleekxmpp/clientxmpp.py
index fb5b2087..1ac3d8bd 100644
--- a/sleekxmpp/clientxmpp.py
+++ b/sleekxmpp/clientxmpp.py
@@ -168,18 +168,23 @@ class ClientXMPP(BaseXMPP):
addresses = {}
intmax = 0
+ topprio = 65535
for answer in answers:
- intmax += answer.priority
- addresses[intmax] = (answer.target.to_text()[:-1],
+ topprio = min(topprio, answer.priority)
+ for answer in answers:
+ if answer.priority == topprio:
+ intmax += answer.weight
+ addresses[intmax] = (answer.target.to_text()[:-1],
answer.port)
+
#python3 returns a generator for dictionary keys
- priorities = [x for x in addresses.keys()]
- priorities.sort()
+ items = [x for x in addresses.keys()]
+ items.sort()
picked = random.randint(0, intmax)
- for priority in priorities:
+ for item in items:
if picked <= priority:
- address = addresses[priority]
+ address = addresses[item]
break
if not address: