@@ -10,11 +10,43 @@ import (
10
10
"errors"
11
11
12
12
"github.com/mongodb/mongo-go-driver/bson"
13
+ "github.com/mongodb/mongo-go-driver/core/description"
13
14
"github.com/mongodb/mongo-go-driver/core/readconcern"
15
+ "github.com/mongodb/mongo-go-driver/core/session"
14
16
"github.com/mongodb/mongo-go-driver/core/wiremessage"
15
17
"github.com/mongodb/mongo-go-driver/core/writeconcern"
16
18
)
17
19
20
+ func responseClusterTime (response bson.Reader ) * bson.Document {
21
+ clusterTime , err := response .Lookup ("$clusterTime" )
22
+ if err != nil {
23
+ // $clusterTime not included by the server
24
+ return nil
25
+ }
26
+
27
+ return bson .NewDocument (clusterTime )
28
+ }
29
+
30
+ func updateClusterTimes (sess * session.Client , clock * session.ClusterClock , response bson.Reader ) error {
31
+ clusterTime := responseClusterTime (response )
32
+ if clusterTime == nil {
33
+ return nil
34
+ }
35
+
36
+ if sess != nil {
37
+ err := sess .AdvanceClusterTime (clusterTime )
38
+ if err != nil {
39
+ return err
40
+ }
41
+ }
42
+
43
+ if clock != nil {
44
+ clock .AdvanceClusterTime (clusterTime )
45
+ }
46
+
47
+ return nil
48
+ }
49
+
18
50
func marshalCommand (cmd * bson.Document ) (bson.Reader , error ) {
19
51
if cmd == nil {
20
52
return bson.Reader {5 , 0 , 0 , 0 , 0 }, nil
@@ -23,6 +55,54 @@ func marshalCommand(cmd *bson.Document) (bson.Reader, error) {
23
55
return cmd .MarshalBSON ()
24
56
}
25
57
58
+ // add a session ID to a BSON doc representing a command
59
+ func addSessionID (cmd * bson.Document , desc description.SelectedServer , client * session.Client ) error {
60
+ if client == nil || ! description .SessionsSupported (desc .WireVersion ) || desc .SessionTimeoutMinutes == 0 {
61
+ return nil
62
+ }
63
+
64
+ if client .Terminated {
65
+ return session .ErrSessionEnded
66
+ }
67
+
68
+ if _ , err := cmd .LookupElementErr ("lsid" ); err != nil {
69
+ cmd .Delete ("lsid" )
70
+ }
71
+
72
+ cmd .Append (bson .EC .SubDocument ("lsid" , client .SessionID ))
73
+ return nil
74
+ }
75
+
76
+ func addClusterTime (cmd * bson.Document , desc description.SelectedServer , sess * session.Client , clock * session.ClusterClock ) error {
77
+ if (clock == nil && sess == nil ) || ! description .SessionsSupported (desc .WireVersion ) {
78
+ return nil
79
+ }
80
+
81
+ var clusterTime * bson.Document
82
+ if clock != nil {
83
+ clusterTime = clock .GetClusterTime ()
84
+ }
85
+
86
+ if sess != nil {
87
+ if clusterTime == nil {
88
+ clusterTime = sess .ClusterTime
89
+ } else {
90
+ clusterTime = session .MaxClusterTime (clusterTime , sess .ClusterTime )
91
+ }
92
+ }
93
+
94
+ if clusterTime == nil {
95
+ return nil
96
+ }
97
+
98
+ if _ , err := cmd .LookupElementErr ("$clusterTime" ); err != nil {
99
+ cmd .Delete ("$clusterTime" )
100
+ }
101
+
102
+ return cmd .Concat (clusterTime )
103
+ }
104
+
105
+ // add a read concern to a BSON doc representing a command
26
106
func addReadConcern (cmd * bson.Document , rc * readconcern.ReadConcern ) error {
27
107
if rc == nil {
28
108
return nil
@@ -41,6 +121,7 @@ func addReadConcern(cmd *bson.Document, rc *readconcern.ReadConcern) error {
41
121
return nil
42
122
}
43
123
124
+ // add a write concern to a BSON doc representing a command
44
125
func addWriteConcern (cmd * bson.Document , wc * writeconcern.WriteConcern ) error {
45
126
if wc == nil {
46
127
return nil
0 commit comments