safe_close

package
v4.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2022 License: GPL-3.0 Imports: 1 Imported by: 0

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.

  1. Main service goroutine starts and wait on ReceiveCloseSignal and call Done before returns.
  2. Any service's sub goroutine should be started by Attach and wait on ReceiveCloseSignal.
  3. 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.
  4. 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) Err

func (s *SafeClose) Err() error

Err returns the first SendCloseSignal error.

func (*SafeClose) ReceiveCloseSignal

func (s *SafeClose) ReceiveCloseSignal() <-chan struct{}

func (*SafeClose) SendCloseSignal

func (s *SafeClose) SendCloseSignal(err error)

SendCloseSignal sends a close signal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL