26 lines
977 B
Go
26 lines
977 B
Go
package source
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
|
|
)
|
|
|
|
type majority struct{ Source }
|
|
|
|
// Majority retains the source's lease and transaction lifecycle while requiring
|
|
// durable writes and primary majority reads for externally visible handoffs.
|
|
func Majority(database Source) Source { return &majority{Source: database} }
|
|
|
|
func (s *majority) Acquire(ctx context.Context) (*mongo.Database, context.Context, func(), error) {
|
|
database, scoped, release, err := s.Source.Acquire(ctx)
|
|
if err != nil {
|
|
return nil, scoped, release, err
|
|
}
|
|
return database.Client().Database(database.Name(), options.Database().SetWriteConcern(writeconcern.Majority()).SetReadConcern(readconcern.Majority()).SetReadPreference(readpref.Primary())), scoped, release, nil
|
|
}
|