Skip to content

Commit

Permalink
GODRIVER-3108 [master] Optimize writeServerSelector (#1530)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Sopov <isopov@joom.com>
  • Loading branch information
blink1073 and isopov authored Jan 30, 2024
1 parent 00e355c commit 43797ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
21 changes: 21 additions & 0 deletions mongo/description/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ func BenchmarkSelector_Sharded(b *testing.B) {
}
}

func Benchmark_SelectServer_SelectServer(b *testing.B) {
topology := Topology{Kind: ReplicaSet} // You can change the topology as needed
candidates := []Server{
{Kind: Mongos},
{Kind: RSPrimary},
{Kind: Standalone},
}

selector := writeServerSelector{} // Assuming this is the receiver type

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := selector.SelectServer(topology, candidates)
if err != nil {
b.Fatalf("Error selecting server: %v", err)
}
}
}

func TestSelector_Single(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 11 additions & 1 deletion mongo/description/server_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ func (writeServerSelector) SelectServer(t Topology, candidates []Server) ([]Serv
case Single, LoadBalanced:
return candidates, nil
default:
result := []Server{}
// Determine the capacity of the results slice.
selected := 0
for _, candidate := range candidates {
switch candidate.Kind {
case Mongos, RSPrimary, Standalone:
selected++
}
}

// Append candidates to the results slice.
result := make([]Server, 0, selected)
for _, candidate := range candidates {
switch candidate.Kind {
case Mongos, RSPrimary, Standalone:
Expand Down

0 comments on commit 43797ef

Please sign in to comment.