Change imlpementation to use best result

This commit is contained in:
Till Faelligen 2024-01-20 19:55:54 +01:00
parent 5b9b1d31a8
commit b2f99b31b9
No known key found for this signature in database
GPG key ID: 3DF82D8AB9211D4E

View file

@ -8,7 +8,6 @@ import (
"github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/gomatrixserverlib/spec"
"github.com/matrix-org/util"
) )
type PerformCreateRoomRequest struct { type PerformCreateRoomRequest struct {
@ -91,26 +90,39 @@ type PerformBackfillRequest struct {
VirtualHost spec.ServerName `json:"virtual_host"` VirtualHost spec.ServerName `json:"virtual_host"`
} }
// PrevEventIDs returns the prev_event IDs of all backwards extremities, de-duplicated in a lexicographically sorted order. // PrevEventIDs returns the prev_event IDs of either 100 backwards extremities or
// len(r.BackwardsExtremities). Limited to 100, due to Synapse stopping after reaching
// this limit. (which sounds sane)
func (r *PerformBackfillRequest) PrevEventIDs() []string { func (r *PerformBackfillRequest) PrevEventIDs() []string {
// Collect 1k eventIDs, if possible, they may be cleared out below var uniqueIDs map[string]struct{}
maxPrevEventIDs := len(r.BackwardsExtremities) * 3
if maxPrevEventIDs > 2000 { // Create a unique eventID map of either 100 or len(r.BackwardsExtremities).
maxPrevEventIDs = 2000 // 100 since Synapse stops after reaching 100 events.
if len(r.BackwardsExtremities) > 100 {
uniqueIDs = make(map[string]struct{}, 100)
} else {
uniqueIDs = make(map[string]struct{}, len(r.BackwardsExtremities))
} }
prevEventIDs := make([]string, 0, maxPrevEventIDs)
outerLoop:
for _, pes := range r.BackwardsExtremities { for _, pes := range r.BackwardsExtremities {
prevEventIDs = append(prevEventIDs, pes...) for _, evID := range pes {
if len(prevEventIDs) > 1000 { uniqueIDs[evID] = struct{}{}
break // We found enough unique eventIDs.
if len(uniqueIDs) >= 100 {
break outerLoop
}
} }
} }
prevEventIDs = util.UniqueStrings(prevEventIDs)
// If we still have > 100 eventIDs, only return the first 100 // map -> []string
if len(prevEventIDs) > 100 { result := make([]string, len(uniqueIDs))
return prevEventIDs[:100] i := 0
for evID := range uniqueIDs {
result[i] = evID
} }
return prevEventIDs
return result
} }
// PerformBackfillResponse is a response to PerformBackfill. // PerformBackfillResponse is a response to PerformBackfill.