From 66134bc4a2f161f9909e0e71d8e3dfbc5ea97d35 Mon Sep 17 00:00:00 2001
From: Ruben Pollan <meskio@sindominio.net>
Date: Mon, 12 Nov 2018 17:05:25 -0600
Subject: [PATCH] [feat] select randomly the gateway if the have the same
 distance

This commit includes a hack to prioritize giraffe, a new riseup gateway
in europe to reduce the load in the existing node.

- Resolves: #81
---
 standalone/bonafide.go | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/standalone/bonafide.go b/standalone/bonafide.go
index 84250ea8..b46d9bb9 100644
--- a/standalone/bonafide.go
+++ b/standalone/bonafide.go
@@ -22,6 +22,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"log"
+	"math/rand"
 	"net/http"
 	"sort"
 	"strconv"
@@ -211,7 +212,22 @@ func (b *bonafide) sortGateways() {
 		gws = append(gws, gatewayDistance{gw, distance})
 	}
 
-	sort.Slice(gws, func(i, j int) bool { return gws[i].distance < gws[j].distance })
+	rand.Seed(time.Now().UnixNano())
+	cmp := func(i, j int) bool {
+		if gws[i].distance == gws[j].distance {
+			// TODO: a hack to distribute more the load into the new gw.
+			//       Let's delete it as soon as is more spread the load.
+			if gws[i].gateway.Host == "giraffe.riseup.net" {
+				return rand.Intn(4) != 0
+			} else if gws[j].gateway.Host == "giraffe.riseup.net" {
+				return rand.Intn(4) == 0
+			}
+
+			return rand.Intn(2) == 1
+		}
+		return gws[i].distance < gws[j].distance
+	}
+	sort.Slice(gws, cmp)
 	for i, gw := range gws {
 		b.eip.Gateways[i] = gw.gateway
 	}
-- 
GitLab