Changeset

458:6c3797c0bb44

client: Correctly randomize SRV targets (thanks Zash)
author Matthew Wild <mwild1@gmail.com>
date Thu, 16 Mar 2023 11:42:28 +0000
parents 457:73d4eb93657b
children 459:6be4182cfb31
files client.lua
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/client.lua	Thu Mar 16 11:41:52 2023 +0000
+++ b/client.lua	Thu Mar 16 11:42:28 2023 +0000
@@ -23,8 +23,17 @@
 
 local xmlns_stream = "http://etherx.jabber.org/streams";
 
-local function compare_srv_priorities(a,b)
-	return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
+local function compare_srv_priorities(a, b)
+	if a.priority == b.priority then
+		if not a.weight_r then
+			a.weight_r = math.random();
+		end
+		if not b.weight_r then
+			b.weight_r = math.random();
+		end
+		return (1 + a.weight) * a.weight_r > (1 + b.weight) * b.weight_r;
+	end
+	return a.priority < b.priority;
 end
 
 local stream_callbacks = {