-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptimestamp.go
52 lines (48 loc) · 1.16 KB
/
optimestamp.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
package main
import (
"expvar"
"github.com/duego/cryriver/mongodb"
"log"
"os"
"time"
)
var (
lastEsSeen *mongodb.Timestamp
lastEsSeenC = make(chan *mongodb.Timestamp, 1)
lastEsSeenStat = expvar.NewString("Last optime seen")
)
func init() {
// Restore any previously saved timestamp
lastEsSeen = new(mongodb.Timestamp)
if f, err := os.Open(*optimeStore); err != nil {
log.Println("Failed to load previous lastEsSeen timestamp:", err)
} else {
lastEsSeen.Load(f)
f.Close()
}
go saveLastEsSeen()
}
// saveLastEsSeen loops the channel to save our progress on what timestamp we have seen so far.
// It will be flushed to disk when our timer ticks.
func saveLastEsSeen() {
lastEsSeenTimer := time.NewTicker(time.Second)
for {
select {
case <-lastEsSeenTimer.C:
if lastEsSeen == nil {
continue
}
if f, err := os.Create(*optimeStore); err != nil {
log.Println("Error saving oplog timestamp:", err)
} else {
if err := lastEsSeen.Save(f); err != nil {
log.Println("Error saving oplog timestamp:", err)
}
f.Close()
lastEsSeenStat.Set(lastEsSeen.String())
lastEsSeen = nil
}
case lastEsSeen = <-lastEsSeenC:
}
}
}