From 45412fd404157d4afc1d08eb5b785b94a55e957c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 29 Jun 2011 09:20:48 +0800 Subject: Do a weighted choice among the highest prioritized items based on weight instead of a weighted choice based on priorities. --- sleekxmpp/clientxmpp.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'sleekxmpp/clientxmpp.py') 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: -- cgit v1.2.3 From ad032e5ed77cfd1ec354e68e49d755e004408d0e Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 27 Jul 2011 18:40:57 -0700 Subject: Fix error with DNS selection. Missed a renaming of 'priority' to 'item' --- sleekxmpp/clientxmpp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sleekxmpp/clientxmpp.py') diff --git a/sleekxmpp/clientxmpp.py b/sleekxmpp/clientxmpp.py index 1ac3d8bd..2019b239 100644 --- a/sleekxmpp/clientxmpp.py +++ b/sleekxmpp/clientxmpp.py @@ -183,7 +183,7 @@ class ClientXMPP(BaseXMPP): picked = random.randint(0, intmax) for item in items: - if picked <= priority: + if picked <= item: address = addresses[item] break -- cgit v1.2.3