Skip to content

Commit 71831b2

Browse files
GODRIVER-2630 Rename all instances of sessCtx to ctx and reformat Session documentation. (#1113)
Co-authored-by: Kevin Albertson <[email protected]>
1 parent 4c5c832 commit 71831b2

File tree

6 files changed

+103
-93
lines changed

6 files changed

+103
-93
lines changed

mongo/crud_examples_test.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -625,16 +625,17 @@ func ExampleWithSession() {
625625
err = mongo.WithSession(
626626
context.TODO(),
627627
sess,
628-
func(sessCtx mongo.SessionContext) error {
629-
// Use sessCtx as the Context parameter for InsertOne and FindOne so
630-
// both operations are run under the new Session.
628+
func(ctx mongo.SessionContext) error {
629+
// Use the mongo.SessionContext as the Context parameter for
630+
// InsertOne and FindOne so both operations are run under the new
631+
// Session.
631632

632633
if err := sess.StartTransaction(); err != nil {
633634
return err
634635
}
635636

636637
coll := client.Database("db").Collection("coll")
637-
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
638+
res, err := coll.InsertOne(ctx, bson.D{{"x", 1}})
638639
if err != nil {
639640
// Abort the transaction after an error. Use
640641
// context.Background() to ensure that the abort can complete
@@ -646,7 +647,7 @@ func ExampleWithSession() {
646647

647648
var result bson.M
648649
err = coll.FindOne(
649-
sessCtx,
650+
ctx,
650651
bson.D{{"_id", res.InsertedID}},
651652
).Decode(result)
652653
if err != nil {
@@ -681,44 +682,45 @@ func ExampleClient_UseSessionWithOptions() {
681682
err := client.UseSessionWithOptions(
682683
context.TODO(),
683684
opts,
684-
func(sessCtx mongo.SessionContext) error {
685-
// Use sessCtx as the Context parameter for InsertOne and FindOne so
686-
// both operations are run under the new Session.
685+
func(ctx mongo.SessionContext) error {
686+
// Use the mongo.SessionContext as the Context parameter for
687+
// InsertOne and FindOne so both operations are run under the new
688+
// Session.
687689

688-
if err := sessCtx.StartTransaction(); err != nil {
690+
if err := ctx.StartTransaction(); err != nil {
689691
return err
690692
}
691693

692694
coll := client.Database("db").Collection("coll")
693-
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
695+
res, err := coll.InsertOne(ctx, bson.D{{"x", 1}})
694696
if err != nil {
695697
// Abort the transaction after an error. Use
696698
// context.Background() to ensure that the abort can complete
697699
// successfully even if the context passed to mongo.WithSession
698700
// is changed to have a timeout.
699-
_ = sessCtx.AbortTransaction(context.Background())
701+
_ = ctx.AbortTransaction(context.Background())
700702
return err
701703
}
702704

703705
var result bson.M
704706
err = coll.FindOne(
705-
sessCtx,
707+
ctx,
706708
bson.D{{"_id", res.InsertedID}},
707709
).Decode(result)
708710
if err != nil {
709711
// Abort the transaction after an error. Use
710712
// context.Background() to ensure that the abort can complete
711713
// successfully even if the context passed to mongo.WithSession
712714
// is changed to have a timeout.
713-
_ = sessCtx.AbortTransaction(context.Background())
715+
_ = ctx.AbortTransaction(context.Background())
714716
return err
715717
}
716718
fmt.Println(result)
717719

718720
// Use context.Background() to ensure that the commit can complete
719721
// successfully even if the context passed to mongo.WithSession is
720722
// changed to have a timeout.
721-
return sessCtx.CommitTransaction(context.Background())
723+
return ctx.CommitTransaction(context.Background())
722724
})
723725
if err != nil {
724726
log.Fatal(err)
@@ -748,19 +750,19 @@ func ExampleClient_StartSession_withTransaction() {
748750
SetReadPreference(readpref.PrimaryPreferred())
749751
result, err := sess.WithTransaction(
750752
context.TODO(),
751-
func(sessCtx mongo.SessionContext) (interface{}, error) {
752-
// Use sessCtx as the Context parameter for InsertOne and FindOne so
753-
// both operations are run in a transaction.
753+
func(ctx mongo.SessionContext) (interface{}, error) {
754+
// Use the mongo.SessionContext as the Context parameter for
755+
// InsertOne and FindOne so both operations are run in the same transaction
754756

755757
coll := client.Database("db").Collection("coll")
756-
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
758+
res, err := coll.InsertOne(ctx, bson.D{{"x", 1}})
757759
if err != nil {
758760
return nil, err
759761
}
760762

761763
var result bson.M
762764
err = coll.FindOne(
763-
sessCtx,
765+
ctx,
764766
bson.D{{"_id", res.InsertedID}},
765767
).Decode(result)
766768
if err != nil {
@@ -784,16 +786,17 @@ func ExampleNewSessionContext() {
784786
panic(err)
785787
}
786788
defer sess.EndSession(context.TODO())
787-
sessCtx := mongo.NewSessionContext(context.TODO(), sess)
789+
ctx := mongo.NewSessionContext(context.TODO(), sess)
788790

789-
// Start a transaction and sessCtx as the Context parameter to InsertOne and
790-
// FindOne so both operations will be run in the transaction.
791+
// Start a transaction and use the mongo.SessionContext as the Context
792+
// parameter for InsertOne and FindOne so both operations are run in the
793+
// transaction.
791794
if err = sess.StartTransaction(); err != nil {
792795
panic(err)
793796
}
794797

795798
coll := client.Database("db").Collection("coll")
796-
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
799+
res, err := coll.InsertOne(ctx, bson.D{{"x", 1}})
797800
if err != nil {
798801
// Abort the transaction after an error. Use context.Background() to
799802
// ensure that the abort can complete successfully even if the context
@@ -804,7 +807,7 @@ func ExampleNewSessionContext() {
804807

805808
var result bson.M
806809
err = coll.FindOne(
807-
sessCtx,
810+
ctx,
808811
bson.D{{"_id", res.InsertedID}},
809812
).Decode(&result)
810813
if err != nil {

mongo/integration/load_balancer_prose_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ func TestLoadBalancerSupport(t *testing.T) {
8181
assertErrorHasInfo(mt, err, 1, 0, 0)
8282
})
8383
mt.RunOpts("transactions", maxPoolSizeMtOpts, func(mt *mtest.T) {
84-
sess, err := mt.Client.StartSession()
85-
assert.Nil(mt, err, "StartSession error: %v", err)
86-
defer sess.EndSession(context.Background())
87-
sessCtx := mongo.NewSessionContext(context.Background(), sess)
84+
{
85+
sess, err := mt.Client.StartSession()
86+
assert.Nil(mt, err, "StartSession error: %v", err)
87+
defer sess.EndSession(context.Background())
88+
ctx := mongo.NewSessionContext(context.Background(), sess)
8889

89-
// Start a transaction and perform one transactional operation to pin a connection.
90-
err = sess.StartTransaction()
91-
assert.Nil(mt, err, "StartTransaction error: %v", err)
92-
_, err = mt.Coll.InsertOne(sessCtx, bson.M{"x": 1})
93-
assert.Nil(mt, err, "InsertOne error: %v", err)
90+
// Start a transaction and perform one transactional operation to pin a connection.
91+
err = sess.StartTransaction()
92+
assert.Nil(mt, err, "StartTransaction error: %v", err)
93+
_, err = mt.Coll.InsertOne(ctx, bson.M{"x": 1})
94+
assert.Nil(mt, err, "InsertOne error: %v", err)
95+
}
9496

9597
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
9698
defer cancel()
97-
_, err = mt.Coll.InsertOne(ctx, bson.M{"x": 1})
99+
_, err := mt.Coll.InsertOne(ctx, bson.M{"x": 1})
98100
assertErrorHasInfo(mt, err, 0, 1, 0)
99101
})
100102
})

mongo/integration/sessions_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ func TestSessions(t *testing.T) {
301301
assert.Nil(mt, err, "StartSession error: %v", err)
302302
defer sess.EndSession(context.Background())
303303

304-
sessCtx := mongo.NewSessionContext(context.Background(), sess)
305-
assert.Equal(mt, sess.ID(), sessCtx.ID(), "expected Session ID %v, got %v", sess.ID(), sessCtx.ID())
304+
ctx := mongo.NewSessionContext(context.Background(), sess)
305+
assert.Equal(mt, sess.ID(), ctx.ID(), "expected Session ID %v, got %v", sess.ID(), ctx.ID())
306306

307-
gotSess := mongo.SessionFromContext(sessCtx)
307+
gotSess := mongo.SessionFromContext(ctx)
308308
assert.NotNil(mt, gotSess, "expected SessionFromContext to return non-nil value, got nil")
309309
assert.Equal(mt, sess.ID(), gotSess.ID(), "expected Session ID %v, got %v", sess.ID(), gotSess.ID())
310310
})
@@ -323,8 +323,8 @@ func TestSessions(t *testing.T) {
323323
return mongo.NewSessionContext(context.Background(), sess)
324324
}
325325

326-
sessCtx := createSessionContext(mt)
327-
sess := mongo.SessionFromContext(sessCtx)
326+
ctx := createSessionContext(mt)
327+
sess := mongo.SessionFromContext(ctx)
328328
assert.NotNil(mt, sess, "expected SessionFromContext to return non-nil value, got nil")
329329
defer sess.EndSession(context.Background())
330330

@@ -333,14 +333,14 @@ func TestSessions(t *testing.T) {
333333

334334
numDocs := 2
335335
for i := 0; i < numDocs; i++ {
336-
_, err = mt.Coll.InsertOne(sessCtx, bson.D{{"x", 1}})
336+
_, err = mt.Coll.InsertOne(ctx, bson.D{{"x", 1}})
337337
assert.Nil(mt, err, "InsertOne error at index %d: %v", i, err)
338338
}
339339

340340
// Assert that the collection count is 0 before committing and numDocs after. This tests that the InsertOne
341341
// calls were actually executed in the transaction because the pre-commit count does not include them.
342342
assertCollectionCount(mt, 0)
343-
err = sess.CommitTransaction(sessCtx)
343+
err = sess.CommitTransaction(ctx)
344344
assert.Nil(mt, err, "CommitTransaction error: %v", err)
345345
assertCollectionCount(mt, int64(numDocs))
346346
})

mongo/integration/unified/session_operation_execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func executeWithTransaction(ctx context.Context, op *operation, loopDone <-chan
103103
return fmt.Errorf("error unmarshalling arguments to transactionOptions: %v", err)
104104
}
105105

106-
_, err = sess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
106+
_, err = sess.WithTransaction(ctx, func(ctx mongo.SessionContext) (interface{}, error) {
107107
for idx, oper := range operations {
108108
if err := oper.execute(ctx, loopDone); err != nil {
109109
return nil, fmt.Errorf("error executing operation %q at index %d: %v", oper.Name, idx, err)

mongo/session.go

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,53 +84,58 @@ func SessionFromContext(ctx context.Context) Session {
8484
// https://www.mongodb.com/docs/manual/core/transactions/.
8585
//
8686
// Implementations of Session are not safe for concurrent use by multiple goroutines.
87-
//
88-
// StartTransaction starts a new transaction, configured with the given options, on this session. This method will
89-
// return an error if there is already a transaction in-progress for this session.
90-
//
91-
// CommitTransaction commits the active transaction for this session. This method will return an error if there is no
92-
// active transaction for this session or the transaction has been aborted.
93-
//
94-
// AbortTransaction aborts the active transaction for this session. This method will return an error if there is no
95-
// active transaction for this session or the transaction has been committed or aborted.
96-
//
97-
// WithTransaction starts a transaction on this session and runs the fn callback. Errors with the
98-
// TransientTransactionError and UnknownTransactionCommitResult labels are retried for up to 120 seconds. Inside the
99-
// callback, sessCtx must be used as the Context parameter for any operations that should be part of the transaction. If
100-
// the ctx parameter already has a Session attached to it, it will be replaced by this session. The fn callback may be
101-
// run multiple times during WithTransaction due to retry attempts, so it must be idempotent. Non-retryable operation
102-
// errors or any operation errors that occur after the timeout expires will be returned without retrying. If the
103-
// callback fails, the driver will call AbortTransaction. Because this method must succeed to ensure that server-side
104-
// resources are properly cleaned up, context deadlines and cancellations will not be respected during this call. For a
105-
// usage example, see the Client.StartSession method documentation.
106-
//
107-
// ClusterTime, OperationTime, Client, and ID return the session's current cluster time, the session's current operation
108-
// time, the Client associated with the session, and the ID document associated with the session, respectively. The ID
109-
// document for a session is in the form {"id": <BSON binary value>}.
110-
//
111-
// EndSession method should abort any existing transactions and close the session.
112-
//
113-
// AdvanceClusterTime advances the cluster time for a session. This method will return an error if the session has ended.
114-
//
115-
// AdvanceOperationTime advances the operation time for a session. This method will return an error if the session has
116-
// ended.
11787
type Session interface {
118-
// Functions to modify session state.
88+
// StartTransaction starts a new transaction, configured with the given options, on this
89+
// session. This method returns an error if there is already a transaction in-progress for this
90+
// session.
11991
StartTransaction(...*options.TransactionOptions) error
92+
93+
// AbortTransaction aborts the active transaction for this session. This method returns an error
94+
// if there is no active transaction for this session or if the transaction has been committed
95+
// or aborted.
12096
AbortTransaction(context.Context) error
97+
98+
// CommitTransaction commits the active transaction for this session. This method returns an
99+
// error if there is no active transaction for this session or if the transaction has been
100+
// aborted.
121101
CommitTransaction(context.Context) error
122-
WithTransaction(ctx context.Context, fn func(sessCtx SessionContext) (interface{}, error),
102+
103+
// WithTransaction starts a transaction on this session and runs the fn callback. Errors with
104+
// the TransientTransactionError and UnknownTransactionCommitResult labels are retried for up to
105+
// 120 seconds. Inside the callback, the SessionContext must be used as the Context parameter
106+
// for any operations that should be part of the transaction. If the ctx parameter already has a
107+
// Session attached to it, it will be replaced by this session. The fn callback may be run
108+
// multiple times during WithTransaction due to retry attempts, so it must be idempotent.
109+
// Non-retryable operation errors or any operation errors that occur after the timeout expires
110+
// will be returned without retrying. If the callback fails, the driver will call
111+
// AbortTransaction. Because this method must succeed to ensure that server-side resources are
112+
// properly cleaned up, context deadlines and cancellations will not be respected during this
113+
// call. For a usage example, see the Client.StartSession method documentation.
114+
WithTransaction(ctx context.Context, fn func(ctx SessionContext) (interface{}, error),
123115
opts ...*options.TransactionOptions) (interface{}, error)
116+
117+
// EndSession aborts any existing transactions and close the session.
124118
EndSession(context.Context)
125119

126-
// Functions to retrieve session properties.
120+
// ClusterTime returns the current cluster time document associated with the session.
127121
ClusterTime() bson.Raw
122+
123+
// OperationTime returns the current operation time document associated with the session.
128124
OperationTime() *primitive.Timestamp
125+
126+
// Client the Client associated with the session.
129127
Client() *Client
128+
129+
// ID returns the current ID document associated with the session. The ID document is in the
130+
// form {"id": <BSON binary value>}.
130131
ID() bson.Raw
131132

132-
// Functions to modify mutable session properties.
133+
// AdvanceClusterTime advances the cluster time for a session. This method returns an error if
134+
// the session has ended.
133135
AdvanceClusterTime(bson.Raw) error
136+
137+
// AdvanceOperationTime advances the operation time for a session. This method returns an error
138+
// if the session has ended.
134139
AdvanceOperationTime(*primitive.Timestamp) error
135140

136141
session()
@@ -175,7 +180,7 @@ func (s *sessionImpl) EndSession(ctx context.Context) {
175180
}
176181

177182
// WithTransaction implements the Session interface.
178-
func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(sessCtx SessionContext) (interface{}, error),
183+
func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(ctx SessionContext) (interface{}, error),
179184
opts ...*options.TransactionOptions) (interface{}, error) {
180185
timeout := time.NewTimer(withTransactionTimeout)
181186
defer timeout.Stop()

0 commit comments

Comments
 (0)