diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-12-08 15:16:12 +0000 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-12-08 15:16:12 +0000 |
| commit | e470159911c0b218ddb760fcb253c2b3523a0d27 (patch) | |
| tree | be42eb4677b3f46619c85ead7dbe007bf6f70d67 | |
| parent | fcf741f6adb5548b74d0119e196c2affcadb59cd (diff) | |
add done
| -rw-r--r-- | internal/done.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/internal/done.go b/internal/done.go new file mode 100644 index 0000000..54e5e8e --- /dev/null +++ b/internal/done.go @@ -0,0 +1,36 @@ +package internal + +import ( + "sync" +) + +// Done is a cleanup/shutdown helper. +type Done struct { + ch chan struct{} + mutex sync.Mutex +} + +// NewDone returns a new cleanup/shutdown helper. +func NewDone() *Done { + return &Done{ + ch: make(chan struct{}), + } +} + +// Done returns the done channel (closed when done) +func (d *Done) Done() <-chan struct{} { + return d.ch +} + +// Shutdown closes the done channel. It can be called multiple times. +func (d *Done) Shutdown() { + d.mutex.Lock() + defer d.mutex.Unlock() + + select { + case <-d.ch: + return + default: + close(d.ch) + } +} |
