Do a weighted choice among the highest prioritized items based on weight instead of a weighted choice based on priorities.

This commit is contained in:
Kim Alvefur 2011-06-29 09:20:48 +08:00 committed by Lance Stout
parent ccc6ab1281
commit 45412fd404

View file

@ -168,18 +168,23 @@ class ClientXMPP(BaseXMPP):
addresses = {} addresses = {}
intmax = 0 intmax = 0
topprio = 65535
for answer in answers: for answer in answers:
intmax += answer.priority topprio = min(topprio, answer.priority)
for answer in answers:
if answer.priority == topprio:
intmax += answer.weight
addresses[intmax] = (answer.target.to_text()[:-1], addresses[intmax] = (answer.target.to_text()[:-1],
answer.port) answer.port)
#python3 returns a generator for dictionary keys #python3 returns a generator for dictionary keys
priorities = [x for x in addresses.keys()] items = [x for x in addresses.keys()]
priorities.sort() items.sort()
picked = random.randint(0, intmax) picked = random.randint(0, intmax)
for priority in priorities: for item in items:
if picked <= priority: if picked <= priority:
address = addresses[priority] address = addresses[item]
break break
if not address: if not address: