Skip to content

Commit

Permalink
feat(mongox): set nearest read preference
Browse files Browse the repository at this point in the history
  • Loading branch information
pyshx committed Jul 25, 2024
1 parent 5cbc45b commit 4666e11
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mongox/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/reearth/reearthx/usecasex"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/x/mongo/driver"
)

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

func (t *Transaction) Begin(ctx context.Context) (usecasex.Tx, error) {
s, err := t.client.StartSession(options.Session())
// Set the read preference to Nearest
rp := readpref.Nearest()

// Create session options with the Nearest read preference
sessionOpts := options.Session().SetDefaultReadPreference(rp)

s, err := t.client.StartSession(sessionOpts)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4666e11

Please sign in to comment.