Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package session |
| 8 | |
| 9 | import ( |
| 10 | "sync" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | ) |
| 14 | |
| 15 | // ClusterClock represents a logical clock for keeping track of cluster time. |
| 16 | type ClusterClock struct { |
| 17 | clusterTime bson.Raw |
| 18 | lock sync.Mutex |
| 19 | } |
| 20 | |
| 21 | // GetClusterTime returns the cluster's current time. |
| 22 | func (cc *ClusterClock) GetClusterTime() bson.Raw { |
| 23 | var ct bson.Raw |
| 24 | cc.lock.Lock() |
| 25 | ct = cc.clusterTime |
| 26 | cc.lock.Unlock() |
| 27 | |
| 28 | return ct |
| 29 | } |
| 30 | |
| 31 | // AdvanceClusterTime updates the cluster's current time. |
| 32 | func (cc *ClusterClock) AdvanceClusterTime(clusterTime bson.Raw) { |
| 33 | cc.lock.Lock() |
| 34 | cc.clusterTime = MaxClusterTime(cc.clusterTime, clusterTime) |
| 35 | cc.lock.Unlock() |
| 36 | } |