From 2ccbe6ec11e255674e4f3c36ab6ffdbf0344ffed Mon Sep 17 00:00:00 2001 From: Andrei Mihu Date: Mon, 13 Feb 2023 16:39:34 +0000 Subject: [PATCH] Custom matching function should not be given potential matches below max count if some of the tickets are still willing to wait. --- server/matchmaker.go | 1 + server/matchmaker_process.go | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/server/matchmaker.go b/server/matchmaker.go index 220855c72..d86c41a5e 100644 --- a/server/matchmaker.go +++ b/server/matchmaker.go @@ -302,6 +302,7 @@ func (m *LocalMatchmaker) Process() { return } + // Run the custom matching function if one is registered in the runtime, otherwise use the default process function. var matchedEntries [][]*MatchmakerEntry if m.runtime.matchmakerCustomMatchingFunction != nil { matchedEntries = m.processCustom() diff --git a/server/matchmaker_process.go b/server/matchmaker_process.go index 2786224fb..1d8f20d7d 100644 --- a/server/matchmaker_process.go +++ b/server/matchmaker_process.go @@ -1,12 +1,27 @@ +// Copyright 2023 The Nakama Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package server import ( - "github.com/blugelabs/bluge" - "go.uber.org/zap" "math" "math/bits" "sort" "time" + + "github.com/blugelabs/bluge" + "go.uber.org/zap" ) func (m *LocalMatchmaker) processDefault() [][]*MatchmakerEntry { @@ -344,6 +359,11 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry { defer timer.Stop() } + // Update all interval counts at once. + for _, index := range m.activeIndexes { + index.Intervals++ + } + for ticket, index := range m.activeIndexes { if !threshold && timer != nil { select { @@ -353,8 +373,7 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry { } } - index.Intervals++ - lastInterval := index.Intervals >= m.config.GetMatchmaker().MaxIntervals || index.MinCount == index.MaxCount + lastInterval := index.Intervals >= m.config.GetMatchmaker().MaxIntervals if lastInterval { // Drop from active indexes if it has reached its max intervals, or if its min/max counts are equal. In the // latter case keeping it active would have the same result as leaving it in the pool, so this saves work. @@ -472,7 +491,7 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry { hitIndexesCombinations := combinationsMinMax(hitIndexes, 1, index.MaxCount-index.Count) for _, hitIndexes := range hitIndexesCombinations { - // Check the min/max/multiple are acceptable across the hit. + // Check the min and max counts are met across the hit. var hitCount int for _, hitIndex := range hitIndexes { hitCount += hitIndex.Count @@ -486,10 +505,16 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry { } var reject bool for _, hitIndex := range hitIndexes { + // Check if count multiple is satisfied for this hit. if hitCount%hitIndex.CountMultiple != 0 { reject = true break } + // Check if the max is not met, but this hit has not reached its max intervals yet. + if hitCount < hitIndex.MaxCount && hitIndex.Intervals < m.config.GetMatchmaker().MaxIntervals { + reject = true + break + } } if reject { continue -- GitLab