From fe5cf6f880cdb906006a6ca215872949c3388272 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Fri, 29 May 2020 13:50:06 +0100 Subject: [PATCH] fedsender: de-duplicate without sorting server names (#1073) --- federationsender/api/perform.go | 5 +++-- federationsender/internal/perform.go | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/federationsender/api/perform.go b/federationsender/api/perform.go index a73ba047..2a1834b6 100644 --- a/federationsender/api/perform.go +++ b/federationsender/api/perform.go @@ -44,8 +44,9 @@ func (h *httpFederationSenderInternalAPI) PerformDirectoryLookup( } type PerformJoinRequest struct { - RoomID string `json:"room_id"` - UserID string `json:"user_id"` + RoomID string `json:"room_id"` + UserID string `json:"user_id"` + // The sorted list of servers to try. Servers will be tried sequentially, after de-duplication. ServerNames types.ServerNames `json:"server_names"` Content map[string]interface{} `json:"content"` } diff --git a/federationsender/internal/perform.go b/federationsender/internal/perform.go index 9791afef..383ce488 100644 --- a/federationsender/internal/perform.go +++ b/federationsender/internal/perform.go @@ -46,8 +46,19 @@ func (r *FederationSenderInternalAPI) PerformJoin( supportedVersions = append(supportedVersions, version) } - // Deduplicate the server names we were provided. - util.SortAndUnique(request.ServerNames) + // Deduplicate the server names we were provided but keep the ordering + // as this encodes useful information about which servers are most likely + // to respond. + seenSet := make(map[gomatrixserverlib.ServerName]bool) + var uniqueList []gomatrixserverlib.ServerName + for _, srv := range request.ServerNames { + if seenSet[srv] { + continue + } + seenSet[srv] = true + uniqueList = append(uniqueList, srv) + } + request.ServerNames = uniqueList // Try each server that we were provided until we land on one that // successfully completes the make-join send-join dance.