Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SafeClose ¶
type SafeClose struct {
// contains filtered or unexported fields
}
SafeClose can achieve safe close where CloseWait returns only after all sub goroutines exited.
- Main service goroutine starts and wait on ReceiveCloseSignal and call Done before returns.
- Any service's sub goroutine should be started by Attach and wait on ReceiveCloseSignal.
- If any fatal err occurs, any service goroutine can call SendCloseSignal to close the service. Note that CloseWait cannot be called in the service, otherwise it will be deadlocked.
- Any third party caller can call CloseWait to close the service.
func NewSafeClose ¶
func NewSafeClose() *SafeClose
func (*SafeClose) Attach ¶
func (s *SafeClose) Attach(f func(done func(), closeSignal <-chan struct{}))
Attach add this goroutine to s.wg CloseWait. f must receive closeSignal and call done when it is done. If s was closed, f will not run.
func (*SafeClose) CloseWait ¶
func (s *SafeClose) CloseWait()
CloseWait sends a close signal to SafeClose and wait until it is closed. It is concurrent safe and can be called multiple times. CloseWait blocks until s.Done() is called and all Attach-ed goroutines is done.
func (*SafeClose) Done ¶
func (s *SafeClose) Done()
Done notifies CloseWait that is done. It is concurrent safe and can be called multiple times.
func (*SafeClose) ReceiveCloseSignal ¶
func (s *SafeClose) ReceiveCloseSignal() <-chan struct{}
func (*SafeClose) SendCloseSignal ¶
SendCloseSignal sends a close signal.
Click to show internal directories.
Click to hide internal directories.