Skip to content

Commit e120097

Browse files
committed
feat(mongox): support nearest read preference
1 parent 5cbc45b commit e120097

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

mongox/transaction.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/reearth/reearthx/usecasex"
88
"go.mongodb.org/mongo-driver/mongo"
99
"go.mongodb.org/mongo-driver/mongo/options"
10+
"go.mongodb.org/mongo-driver/mongo/readpref"
1011
"go.mongodb.org/mongo-driver/x/mongo/driver"
1112
)
1213

@@ -23,7 +24,13 @@ func NewTransaction(client *mongo.Client) *Transaction {
2324
}
2425

2526
func (t *Transaction) Begin(ctx context.Context) (usecasex.Tx, error) {
26-
s, err := t.client.StartSession(options.Session())
27+
// Set the read preference to Nearest
28+
rp := readpref.Nearest()
29+
30+
// Create session options with the Nearest read preference
31+
sessionOpts := options.Session().SetDefaultReadPreference(rp)
32+
33+
s, err := t.client.StartSession(sessionOpts)
2734
if err != nil {
2835
return nil, err
2936
}

0 commit comments

Comments
 (0)