github.com/nxadm/tail@v1.4.12-0.20231010141446-ba755e4d73b6/watch/filechanges.go (about)

     1  // Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
     2  package watch
     3  
     4  type 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  
    10  func NewFileChanges() *FileChanges {
    11  	return &FileChanges{
    12  		make(chan bool, 1), make(chan bool, 1), make(chan bool, 1)}
    13  }
    14  
    15  func (fc *FileChanges) NotifyModified() {
    16  	sendOnlyIfEmpty(fc.Modified)
    17  }
    18  
    19  func (fc *FileChanges) NotifyTruncated() {
    20  	sendOnlyIfEmpty(fc.Truncated)
    21  }
    22  
    23  func (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).
    32  func sendOnlyIfEmpty(ch chan bool) {
    33  	select {
    34  	case ch <- true:
    35  	default:
    36  	}
    37  }