safe_close

package
v5.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 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 WaitClosed returns only after all sub goroutines exited.

  1. Main service goroutine starts and wait on ReceiveCloseSignal.
  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.
  4. Any third party caller can call SendCloseSignal 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 WaitClosed. f must receive closeSignal and call done when it is done. If s was closed, f will not run.

func (*SafeClose) ReceiveCloseSignal

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

func (*SafeClose) SendCloseSignal

func (s *SafeClose) SendCloseSignal(err error)

SendCloseSignal sends a close signal. Unblock WaitClosed. The given error will be read by WaitClosed. Once SendCloseSignal is called, following calls are noop.

func (*SafeClose) WaitClosed

func (s *SafeClose) WaitClosed() error

WaitClosed waits until all SendCloseSignal is called and all attached funcs in SafeClose are done.

Jump to

Keyboard shortcuts

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