-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexMessages.go
175 lines (145 loc) · 3.8 KB
/
indexMessages.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package archiving
import (
"time"
"github.com/golang/glog"
"github.com/rokeller/bart/domain"
)
type message interface{}
type keyedMessage struct {
relPath string
}
type setMessage struct {
keyedMessage
indexEntry
markDirty bool
}
type getMessage struct {
keyedMessage
result chan<- *indexEntry
}
type delMessage struct {
keyedMessage
}
type syncMessage struct {
start chan<- bool // This channel is used to indicate that the synced logic can start
done <-chan bool // this channel is used to indicate that the synced logic is done
}
func (i Index) setEntry(entry domain.Entry, flags EntryFlags, markDirty bool) {
if i.closed {
glog.Warningf("Not sending 'set' message for '%s', because message handling has stopped.",
entry.RelPath)
return
}
i.messages <- setMessage{
keyedMessage: keyedMessage{relPath: entry.RelPath},
indexEntry: indexEntry{
EntryMetadata: entry.EntryMetadata,
EntryFlags: flags,
},
markDirty: markDirty,
}
}
func (i Index) getEntry(relPath string) *indexEntry {
if i.closed {
glog.Warningf("Not sending 'get' message for '%s', because message handling has stopped.",
relPath)
return nil
}
// Use an unbuffered channel: when the message handler has the response,
// we need to be ready to accept it.
resultChannel := make(chan *indexEntry)
i.messages <- getMessage{
keyedMessage: keyedMessage{relPath: relPath},
result: resultChannel,
}
return <-resultChannel
}
func (i Index) deleteEntry(relPath string) {
if i.closed {
glog.Warningf("Not sending 'delete' message for '%s', because message handling has stopped.",
relPath)
return
}
i.messages <- delMessage{
keyedMessage: keyedMessage{relPath: relPath},
}
}
func (i Index) sync(fn func()) {
done := make(chan bool, 1)
// Make sure the done signal always reaches the handler.
defer func() {
// Signal the message handler (if it's still running), that we're done.
done <- true
}()
if !i.closed {
start := make(chan bool, 1)
sync := syncMessage{
start: start,
done: done,
}
i.messages <- sync
// Wait for the message handler to tell us that we can start
<-start
}
fn()
}
func (i *Index) handleMessages() {
// TODO: better ticker frequency -- should get from command line params
maintenanceTicker := time.NewTicker(30 * time.Second)
defer maintenanceTicker.Stop()
for {
select {
case msg, hasMore := <-i.messages:
if !hasMore {
goto terminate
}
i.handleMessage(msg)
case <-maintenanceTicker.C:
glog.V(1).Info("MaintenaceTicker fired. Check for changes in index.")
if i.dirty {
glog.Info("The index has changed, upload current index checkpoint to backup destination.")
// Calling writeIndex here is safe because message handling is
// "paused" to handle the maintenance ticker.
if err := i.writeIndex(); nil != err {
glog.Errorf("The archive index could not be uploaded: %v", err)
}
}
}
}
terminate:
glog.V(1).Info("Index message handling terminated.")
i.wgClose.Done()
}
func (i *Index) handleMessage(msg message) {
glog.V(3).Infof("Handle message [%v] (%T) ...", msg, msg)
switch m := msg.(type) {
case setMessage:
if m.markDirty {
i.dirty = true
}
i.entries[m.relPath] = m.indexEntry
case getMessage:
indexEntry, found := i.entries[m.relPath]
if !found {
m.result <- nil
} else {
m.result <- &indexEntry
}
case delMessage:
_, found := i.entries[m.relPath]
delete(i.entries, m.relPath)
if found {
// We removed an existing entry from the index, so mark it dirty.
i.dirty = true
}
case syncMessage:
// Signal the sender that its logic can start
m.start <- true
// Wait for the sender to have its logic finished.
<-m.done
close(m.start)
default:
glog.Warningf("Unsupported message type: %v (%T)", m, m)
}
glog.V(3).Infof("Handled message [%v] (%T).", msg, msg)
}