go-zero/core/syncx/donechan.go

33 lines
636 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package syncx
import (
"sync"
"github.com/zeromicro/go-zero/core/lang"
2020-07-26 17:09:05 +08:00
)
2021-02-28 16:16:22 +08:00
// A DoneChan is used as a channel that can be closed multiple times and wait for done.
2020-07-26 17:09:05 +08:00
type DoneChan struct {
done chan lang.PlaceholderType
once sync.Once
}
2021-02-28 16:16:22 +08:00
// NewDoneChan returns a DoneChan.
2020-07-26 17:09:05 +08:00
func NewDoneChan() *DoneChan {
return &DoneChan{
done: make(chan lang.PlaceholderType),
}
}
2021-02-28 16:16:22 +08:00
// Close closes dc, it's safe to close more than once.
2020-07-26 17:09:05 +08:00
func (dc *DoneChan) Close() {
dc.once.Do(func() {
close(dc.done)
})
}
2021-02-28 16:16:22 +08:00
// Done returns a channel that can be notified on dc closed.
2020-07-26 17:09:05 +08:00
func (dc *DoneChan) Done() chan lang.PlaceholderType {
return dc.done
}