From 83f730def73b528d4398491ea3f507a290acbfef Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 16 Jul 2021 14:51:05 -0700 Subject: [PATCH] use a lock --- peer/record.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/peer/record.go b/peer/record.go index 95c8f6c..0b6775b 100644 --- a/peer/record.go +++ b/peer/record.go @@ -2,6 +2,7 @@ package peer import ( "fmt" + "sync" "sync/atomic" "time" @@ -126,20 +127,22 @@ func PeerRecordFromProtobuf(msg *pb.PeerRecord) (*PeerRecord, error) { return record, nil } -var lastTimestamp uint64 +var ( + lastTimestampMu sync.Mutex + lastTimestamp uint64 +) // TimestampSeq is a helper to generate a timestamp-based sequence number for a PeerRecord. func TimestampSeq() uint64 { now := uint64(time.Now().UnixNano()) - previous := atomic.LoadUint64(&lastTimestamp) - // If the new time is not greater than the last tiemstamp, or if someone else beats us to - // updateing the timestamp, just use last+1. - // - // Technically, last+1 could be before "now". But it's still strictly increasing and close - // enough. - if now <= previous || !atomic.CompareAndSwapUint64(&lastTimestamp, previous, now) { - now = atomic.AddUint64(&lastTimestamp, 1) + lastTimestampMu.Lock() + defer lastTimestampMu.Unlock() + // Not all clocks are strictly increasing, but we need these sequence numbers to be strictly + // increasing. + if now <= lastTimestamp { + now = lastTimestamp + 1 } + lastTimestamp = now return now }