client: Correctly randomize SRV targets (thanks Zash)

Thu, 16 Mar 2023 11:42:28 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 16 Mar 2023 11:42:28 +0000
changeset 458
6c3797c0bb44
parent 457
73d4eb93657b
child 459
6be4182cfb31

client: Correctly randomize SRV targets (thanks Zash)

client.lua file | annotate | diff | comparison | revisions
--- 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 = {

mercurial