util: Add an easy ACK sync primitive

This commit is contained in:
James Shubin
2019-02-21 18:44:01 -05:00
parent 88fcda2c99
commit 450d5c1a59
2 changed files with 85 additions and 0 deletions

View File

@@ -21,6 +21,28 @@ import (
"sync"
)
// EasyAck is a wrapper to build ack functionality into a simple interface.
type EasyAck struct {
done chan struct{}
}
// NewEasyAck builds the object. This must be called before use.
func NewEasyAck() *EasyAck {
return &EasyAck{
done: make(chan struct{}),
}
}
// Ack sends the acknowledgment message. This can only be called once.
func (obj *EasyAck) Ack() {
close(obj.done)
}
// Wait returns a channel that you can wait on for the ack message.
func (obj *EasyAck) Wait() <-chan struct{} {
return obj.done
}
// EasyOnce is a wrapper for the sync.Once functionality which lets you define
// and register the associated `run once` function at declaration time. It may
// be copied at any time.