/
position.go
121 lines (100 loc) · 2.33 KB
/
position.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package promtail
import (
"flag"
"io/ioutil"
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const positionFileMode = 0700
type PositionsConfig struct {
SyncPeriod time.Duration
PositionsFile string
}
func (cfg *PositionsConfig) RegisterFlags(flags *flag.FlagSet) {
flags.DurationVar(&cfg.SyncPeriod, "positions.sync-period", 10*time.Second, "Period with this to sync the position file.")
flag.StringVar(&cfg.PositionsFile, "positions.file", "/var/log/positions.yaml", "Location to read/wrtie positions from.")
}
type Positions struct {
cfg PositionsConfig
mtx sync.Mutex
positions map[string]int64
quit chan struct{}
}
type positionsFile struct {
Positions map[string]int64 `yaml:"positions"`
}
func NewPositions(cfg PositionsConfig) (*Positions, error) {
positions, err := readPositionsFile(cfg.PositionsFile)
if err != nil {
return nil, err
}
p := &Positions{
cfg: cfg,
positions: positions,
quit: make(chan struct{}),
}
go p.run()
return p, nil
}
func (p *Positions) Stop() {
close(p.quit)
}
func (p *Positions) Put(path string, pos int64) {
p.mtx.Lock()
defer p.mtx.Unlock()
p.positions[path] = pos
}
func (p *Positions) Get(path string) int64 {
p.mtx.Lock()
defer p.mtx.Unlock()
return p.positions[path]
}
func (p *Positions) run() {
defer p.save()
ticker := time.NewTicker(p.cfg.SyncPeriod)
for {
select {
case <-p.quit:
return
case <-ticker.C:
p.save()
}
}
}
func (p *Positions) save() {
p.mtx.Lock()
positions := make(map[string]int64, len(p.positions))
for k, v := range p.positions {
positions[k] = v
}
p.mtx.Unlock()
if err := writePositionFile(p.cfg.PositionsFile, positions); err != nil {
log.Errorf("Error writing positions file: %v", err)
}
}
func readPositionsFile(filename string) (map[string]int64, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return map[string]int64{}, nil
}
return nil, err
}
var p positionsFile
if err := yaml.Unmarshal(buf, &p); err != nil {
return nil, err
}
return p.Positions, nil
}
func writePositionFile(filename string, positions map[string]int64) error {
buf, err := yaml.Marshal(positionsFile{
Positions: positions,
})
if err != nil {
return err
}
return ioutil.WriteFile(filename, buf, os.FileMode(positionFileMode))
}