Skip to content

Commit 5a9d0bf

Browse files
author
Divjot Arora
authored
GODRIVER-1478 Update session's last used time when executing an operation (#291)
1 parent 5fc229a commit 5a9d0bf

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

mongo/integration/sessions_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ func TestSessionPool(t *testing.T) {
5050
got = getSessionID(mt, secondSess)
5151
assert.True(mt, sessionIDsEqual(mt, want, got), "expected session ID %v, got %v", want, got)
5252
})
53+
mt.Run("last use time updated", func(mt *mtest.T) {
54+
sess, err := mt.Client.StartSession()
55+
assert.Nil(mt, err, "StartSession error: %v", err)
56+
defer sess.EndSession(mtest.Background)
57+
initialLastUsedTime := getSessionLastUsedTime(mt, sess)
58+
59+
err = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
60+
return mt.Client.Ping(sc, readpref.Primary())
61+
})
62+
assert.Nil(mt, err, "WithSession error: %v", err)
63+
64+
newLastUsedTime := getSessionLastUsedTime(mt, sess)
65+
assert.True(mt, newLastUsedTime.After(initialLastUsedTime),
66+
"last used time %s is not after the initial last used time %s", newLastUsedTime, initialLastUsedTime)
67+
})
5368
}
5469

5570
func TestSessions(t *testing.T) {
@@ -363,3 +378,9 @@ func extractSentSessionID(mt *mtest.T) []byte {
363378
_, data := lsid.Document().Lookup("id").Binary()
364379
return data
365380
}
381+
382+
func getSessionLastUsedTime(mt *mtest.T, sess mongo.Session) time.Time {
383+
xsess, ok := sess.(mongo.XSession)
384+
assert.True(mt, ok, "expected session to implement mongo.XSession, but got %T", sess)
385+
return xsess.ClientSession().LastUsed
386+
}

x/mongo/driver/operation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,8 @@ func (op Operation) addSession(dst []byte, desc description.SelectedServer) ([]b
889889
if client == nil || !description.SessionsSupported(desc.WireVersion) || desc.SessionTimeoutMinutes == 0 {
890890
return dst, nil
891891
}
892-
if client.Terminated {
893-
return dst, session.ErrSessionEnded
892+
if err := client.UpdateUseTime(); err != nil {
893+
return dst, err
894894
}
895895
lsid, _ := client.SessionID.MarshalBSON()
896896
dst = bsoncore.AppendDocumentElement(dst, "lsid", lsid)

x/mongo/driver/session/client_session.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ func (c *Client) AdvanceOperationTime(opTime *primitive.Timestamp) error {
196196
return nil
197197
}
198198

199-
// UpdateUseTime updates the session's last used time.
200-
// Must be called whenver this session is used to send a command to the server.
199+
// UpdateUseTime sets the session's last used time to the current time. This must be called whenever the session is
200+
// used to send a command to the server to ensure that the session is not prematurely marked expired in the driver's
201+
// session pool. If the session has already been ended, this method will return ErrSessionEnded.
201202
func (c *Client) UpdateUseTime() error {
202203
if c.Terminated {
203204
return ErrSessionEnded

0 commit comments

Comments
 (0)