filechanges.go

 1// Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
 2package watch
 3
 4type FileChanges struct {
 5	Modified  chan bool // Channel to get notified of modifications
 6	Truncated chan bool // Channel to get notified of truncations
 7	Deleted   chan bool // Channel to get notified of deletions/renames
 8}
 9
10func NewFileChanges() *FileChanges {
11	return &FileChanges{
12		make(chan bool, 1), make(chan bool, 1), make(chan bool, 1)}
13}
14
15func (fc *FileChanges) NotifyModified() {
16	sendOnlyIfEmpty(fc.Modified)
17}
18
19func (fc *FileChanges) NotifyTruncated() {
20	sendOnlyIfEmpty(fc.Truncated)
21}
22
23func (fc *FileChanges) NotifyDeleted() {
24	sendOnlyIfEmpty(fc.Deleted)
25}
26
27// sendOnlyIfEmpty sends on a bool channel only if the channel has no
28// backlog to be read by other goroutines. This concurrency pattern
29// can be used to notify other goroutines if and only if they are
30// looking for it (i.e., subsequent notifications can be compressed
31// into one).
32func sendOnlyIfEmpty(ch chan bool) {
33	select {
34	case ch <- true:
35	default:
36	}
37}