mempool: p2p refactor (#5919)

This commit is contained in:
Aleksandr Bezobchuk
2021-01-22 09:34:12 -05:00
committed by GitHub
parent 670e9b427b
commit 68bd2116f0
12 changed files with 911 additions and 593 deletions
+28
View File
@@ -13,3 +13,31 @@ type Mutex struct {
type RWMutex struct {
sync.RWMutex
}
// Closer implements a primitive to close a channel that signals process
// termination while allowing a caller to call Close multiple times safely. It
// should be used in cases where guarantees cannot be made about when and how
// many times closure is executed.
type Closer struct {
closeOnce sync.Once
doneCh chan struct{}
}
// NewCloser returns a reference to a new Closer.
func NewCloser() *Closer {
return &Closer{doneCh: make(chan struct{})}
}
// Done returns the internal done channel allowing the caller either block or wait
// for the Closer to be terminated/closed.
func (c *Closer) Done() <-chan struct{} {
return c.doneCh
}
// Close gracefully closes the Closer. A caller should only call Close once, but
// it is safe to call it successive times.
func (c *Closer) Close() {
c.closeOnce.Do(func() {
close(c.doneCh)
})
}