blob: 85376bfd4c807596659ba9822447b759d929b7e5 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// 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
7package session
8
9import (
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.
16type ClusterClock struct {
17 clusterTime bson.Raw
18 lock sync.Mutex
19}
20
21// GetClusterTime returns the cluster's current time.
22func (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.
32func (cc *ClusterClock) AdvanceClusterTime(clusterTime bson.Raw) {
33 cc.lock.Lock()
34 cc.clusterTime = MaxClusterTime(cc.clusterTime, clusterTime)
35 cc.lock.Unlock()
36}