summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/vsphere/task.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/go/collectors/go.d.plugin/modules/vsphere/task.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/vsphere/task.go b/src/go/collectors/go.d.plugin/modules/vsphere/task.go
new file mode 100644
index 000000000..103ca1ed6
--- /dev/null
+++ b/src/go/collectors/go.d.plugin/modules/vsphere/task.go
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package vsphere
+
+import (
+ "sync"
+ "time"
+)
+
+func newTask(doWork func(), doEvery time.Duration) *task {
+ task := task{
+ done: make(chan struct{}),
+ running: make(chan struct{}),
+ }
+
+ go func() {
+ t := time.NewTicker(doEvery)
+ defer func() {
+ t.Stop()
+ close(task.running)
+ }()
+ for {
+ select {
+ case <-task.done:
+ return
+ case <-t.C:
+ doWork()
+ }
+ }
+ }()
+
+ return &task
+}
+
+type task struct {
+ once sync.Once
+ done chan struct{}
+ running chan struct{}
+}
+
+func (t *task) stop() {
+ t.once.Do(func() { close(t.done) })
+}
+
+func (t *task) isStopped() bool {
+ select {
+ case <-t.done:
+ return true
+ default:
+ return false
+ }
+}
+
+func (t *task) isRunning() bool {
+ select {
+ case <-t.running:
+ return false
+ default:
+ return true
+ }
+}