Skip to content

Commit 65e972a

Browse files
GODRIVER-2453 extend session function comments with thread/fork warnings (#995)
1 parent ec7894e commit 65e972a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

mongo/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ func (c *Client) Ping(ctx context.Context, rp *readpref.ReadPref) error {
278278
// StartSession does not actually communicate with the server and will not error if the client is
279279
// disconnected.
280280
//
281+
// StartSession is safe to call from multiple goroutines concurrently. However, Sessions returned by StartSession are
282+
// not safe for concurrent use by multiple goroutines.
283+
//
281284
// If the DefaultReadConcern, DefaultWriteConcern, or DefaultReadPreference options are not set, the client's read
282285
// concern, write concern, or read preference will be used, respectively.
283286
func (c *Client) StartSession(opts ...*options.SessionOptions) (Session, error) {
@@ -1023,6 +1026,9 @@ func (c *Client) ListDatabaseNames(ctx context.Context, filter interface{}, opts
10231026
// SessionContext must be used as the Context parameter for any operations in the fn callback that should be executed
10241027
// under the session.
10251028
//
1029+
// WithSession is safe to call from multiple goroutines concurrently. However, the SessionContext passed to the
1030+
// WithSession callback function is not safe for concurrent use by multiple goroutines.
1031+
//
10261032
// If the ctx parameter already contains a Session, that Session will be replaced with the one provided.
10271033
//
10281034
// Any error returned by the fn callback will be returned without any modifications.
@@ -1035,6 +1041,9 @@ func WithSession(ctx context.Context, sess Session, fn func(SessionContext) erro
10351041
// be executed under a session. After the callback returns, the created Session is ended, meaning that any in-progress
10361042
// transactions started by fn will be aborted even if fn returns an error.
10371043
//
1044+
// UseSession is safe to call from multiple goroutines concurrently. However, the SessionContext passed to the
1045+
// UseSession callback function is not safe for concurrent use by multiple goroutines.
1046+
//
10381047
// If the ctx parameter already contains a Session, that Session will be replaced with the newly created one.
10391048
//
10401049
// Any error returned by the fn callback will be returned without any modifications.
@@ -1043,6 +1052,9 @@ func (c *Client) UseSession(ctx context.Context, fn func(SessionContext) error)
10431052
}
10441053

10451054
// UseSessionWithOptions operates like UseSession but uses the given SessionOptions to create the Session.
1055+
//
1056+
// UseSessionWithOptions is safe to call from multiple goroutines concurrently. However, the SessionContext passed to
1057+
// the UseSessionWithOptions callback function is not safe for concurrent use by multiple goroutines.
10461058
func (c *Client) UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(SessionContext) error) error {
10471059
defaultSess, err := c.StartSession(opts)
10481060
if err != nil {

mongo/session.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ var ErrWrongClient = errors.New("session was not created by this client")
2929
var withTransactionTimeout = 120 * time.Second
3030

3131
// SessionContext combines the context.Context and mongo.Session interfaces. It should be used as the Context arguments
32-
// to operations that should be executed in a session. This type is not goroutine safe and must not be used concurrently
33-
// by multiple goroutines.
32+
// to operations that should be executed in a session.
33+
//
34+
// Implementations of SessionContext are not safe for concurrent use by multiple goroutines.
3435
//
3536
// There are two ways to create a SessionContext and use it in a session/transaction. The first is to use one of the
3637
// callback-based functions such as WithSession and UseSession. These functions create a SessionContext and pass it to
@@ -77,10 +78,13 @@ func SessionFromContext(ctx context.Context) Session {
7778
// for a group of operations or to execute operations in an ACID transaction. A new Session can be created from a Client
7879
// instance. A Session created from a Client must only be used to execute operations using that Client or a Database or
7980
// Collection created from that Client. Custom implementations of this interface should not be used in production. For
80-
// more information about sessions, and their use cases, see https://www.mongodb.com/docs/manual/reference/server-sessions/,
81+
// more information about sessions, and their use cases, see
82+
// https://www.mongodb.com/docs/manual/reference/server-sessions/,
8183
// https://www.mongodb.com/docs/manual/core/read-isolation-consistency-recency/#causal-consistency, and
8284
// https://www.mongodb.com/docs/manual/core/transactions/.
8385
//
86+
// Implementations of Session are not safe for concurrent use by multiple goroutines.
87+
//
8488
// StartTransaction starts a new transaction, configured with the given options, on this session. This method will
8589
// return an error if there is already a transaction in-progress for this session.
8690
//

0 commit comments

Comments
 (0)