Skip to content

Commit aca8f84

Browse files
committed
add read pref to client
1 parent e120097 commit aca8f84

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

mongox/client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/reearth/reearthx/usecasex"
88
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
"go.mongodb.org/mongo-driver/mongo/readpref"
911
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
1012
)
1113

@@ -15,8 +17,12 @@ type Client struct {
1517
}
1618

1719
func NewClient(database string, c *mongo.Client) *Client {
20+
rp := readpref.Nearest()
21+
22+
sessionOpts := options.Database().SetReadPreference(rp)
23+
1824
return &Client{
19-
db: c.Database(database),
25+
db: c.Database(database, sessionOpts),
2026
transaction: &usecasex.NopTransaction{},
2127
}
2228
}

0 commit comments

Comments
 (0)