-
Notifications
You must be signed in to change notification settings - Fork 0
/
responder.go
59 lines (50 loc) · 1.41 KB
/
responder.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
package diplodocus
import "sync"
// ResponderMap keeps track of file objects that are responsible
// for processing of paths on filesystem. Only useful if logs
// are rotated with symlink shifting, like in scribe.
type ResponderMap struct {
mapping map[string][]*file
mutex sync.Mutex
}
// newResponderMap returns empty responder map.
func newResponderMap() *ResponderMap {
return &ResponderMap{
mapping: make(map[string][]*file),
}
}
// AddMapping adds mapping between path on filesystem and file object.
// Should be called from file object on opening.
func (r *ResponderMap) AddMapping(path string, f *file) {
r.mutex.Lock()
defer r.mutex.Unlock()
if files, ok := r.mapping[path]; ok {
files = append(files, f)
r.mapping[path] = files
} else {
r.mapping[path] = []*file{f}
}
}
// GetMappings returns file objects responsible for specified path.
func (r *ResponderMap) GetMappings(path string) []*file {
r.mutex.Lock()
defer r.mutex.Unlock()
if files, ok := r.mapping[path]; ok {
return files
} else {
return []*file{}
}
}
// RemoveMapping removes association between path on filesystem
// and file object that is responsible for processing.
func (r *ResponderMap) RemoveMapping(path string, f *file) {
r.mutex.Lock()
defer r.mutex.Unlock()
if files, ok := r.mapping[path]; ok {
for i, current := range files {
if current == f {
r.mapping[path] = append(files[:i], files[i+1:]...)
}
}
}
}