From da64a69b26b4ca4eaa3ed865df7c01216a07cc54 Mon Sep 17 00:00:00 2001
From: cyBerta <cyberta@riseup.net>
Date: Fri, 14 Mar 2025 11:42:11 +0100
Subject: [PATCH] avoid messing with **T when creating random weighted lists

---
 pkg/api/endpoints.go | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/pkg/api/endpoints.go b/pkg/api/endpoints.go
index e0cd9a9..ebf2197 100644
--- a/pkg/api/endpoints.go
+++ b/pkg/api/endpoints.go
@@ -69,18 +69,21 @@ func sortEndpoints[T m.Bridge | m.Gateway](cc string, endpoints []*T, locations
 	return result, nil
 }
 
-func createLocationTree[T m.Bridge | m.Gateway](endpoints []*T, locationWeights map[string]float64) *rbt.Tree[float64, []wr.Choice[*T]] {
-	tree := rbt.New[float64, []wr.Choice[*T]]()
+func createLocationTree[T m.Bridge | m.Gateway](endpoints []*T, locationWeights map[string]float64) *rbt.Tree[float64, []wr.Choice[T]] {
+	tree := rbt.New[float64, []wr.Choice[T]]()
 	for _, endpoint := range endpoints {
-		var list []wr.Choice[*T]
+		if endpoint == nil {
+			continue
+		}
+		var list []wr.Choice[T]
 		found := false
 		locationWeight := locationWeights[getLocation(endpoint)]
 		if list, found = tree.Get(locationWeight); !found {
 			// TODO: we want to set weights per gateways, so that gateways with higher capacities
 			// are priotized, the wr.Choice wrapper is a preparation for that
-			list = []wr.Choice[*T]{{Item: endpoint, Weight: 1}}
+			list = []wr.Choice[T]{{Item: *endpoint, Weight: 1}}
 		} else {
-			list = append(list, wr.Choice[*T]{Item: endpoint, Weight: 1})
+			list = append(list, wr.Choice[T]{Item: *endpoint, Weight: 1})
 		}
 		tree.Put(locationWeight, list)
 	}
-- 
GitLab