-
Notifications
You must be signed in to change notification settings - Fork 8
/
timestamp.go
59 lines (52 loc) · 1.28 KB
/
timestamp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
package db19
import (
"log"
"sync"
"time"
. "github.com/apmckinlay/gsuneido/core"
)
var timestamp SuDate
var tsLock sync.Mutex
// StartTimestamp is called by gsuneido.go openDbms
func StartTimestamps() {
// start timestamp ahead one second
// to avoid overlap if it restarts in less than a second.
timestamp = Now().WithoutMs().Plus(0, 0, 0, 0, 0, 0, 990)
go ticker()
}
// ticker runs on the dbms i.e. server or standalone, not client
func ticker() {
var timeError = false
for {
time.Sleep(1 * time.Second)
t := Now().WithoutMs()
tsLock.Lock()
d := t.MinusMs(timestamp)
if d > 0 {
if d > 5000 {
log.Println("INFO: time skip from", timestamp, "to", t,
"=", time.Duration(d) * time.Millisecond)
}
timestamp = t // normal case
timeError = false
} else if d < 0 && !timeError {
log.Println("INFO: time went backwards from", timestamp, "to", t)
timeError = true
}
tsLock.Unlock()
}
}
// Timestamp is the backend
func Timestamp() SuDate {
tsLock.Lock()
defer tsLock.Unlock()
ts := timestamp
if ts.Millisecond() < TsThreshold {
timestamp = timestamp.AddMs(TsInitialBatch)
} else {
timestamp = timestamp.AddMs(1)
}
return ts
}