podman
1// Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
2package watch
3
4type FileChanges struct {
5Modified chan bool // Channel to get notified of modifications
6Truncated chan bool // Channel to get notified of truncations
7Deleted chan bool // Channel to get notified of deletions/renames
8}
9
10func NewFileChanges() *FileChanges {
11return &FileChanges{
12make(chan bool, 1), make(chan bool, 1), make(chan bool, 1)}
13}
14
15func (fc *FileChanges) NotifyModified() {
16sendOnlyIfEmpty(fc.Modified)
17}
18
19func (fc *FileChanges) NotifyTruncated() {
20sendOnlyIfEmpty(fc.Truncated)
21}
22
23func (fc *FileChanges) NotifyDeleted() {
24sendOnlyIfEmpty(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) {
33select {
34case ch <- true:
35default:
36}
37}
38