github.com/netdata/go.d.plugin@v0.58.1/modules/vsphere/task.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package vsphere 4 5 import ( 6 "sync" 7 "time" 8 ) 9 10 func newTask(doWork func(), doEvery time.Duration) *task { 11 task := task{ 12 done: make(chan struct{}), 13 running: make(chan struct{}), 14 } 15 16 go func() { 17 t := time.NewTicker(doEvery) 18 defer func() { 19 t.Stop() 20 close(task.running) 21 }() 22 for { 23 select { 24 case <-task.done: 25 return 26 case <-t.C: 27 doWork() 28 } 29 } 30 }() 31 32 return &task 33 } 34 35 type task struct { 36 once sync.Once 37 done chan struct{} 38 running chan struct{} 39 } 40 41 func (t *task) stop() { 42 t.once.Do(func() { close(t.done) }) 43 } 44 45 func (t *task) isStopped() bool { 46 select { 47 case <-t.done: 48 return true 49 default: 50 return false 51 } 52 } 53 54 func (t *task) isRunning() bool { 55 select { 56 case <-t.running: 57 return false 58 default: 59 return true 60 } 61 }