grace

package
v0.0.0-...-e9c3580 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Grace

type Grace struct {
	// contains filtered or unexported fields
}

func New

func New() *Grace

func (*Grace) Notify

func (g *Grace) Notify(signals ...os.Signal)
Example
if os.Getenv("TESTING_SIGNAL") != "1" {
	fmt.Println(4, true)
	return
}

continueCnt := int64(0)
stoppedCnt := int64(0)
f1 := func(c context.Context) {
	for {
		select {

		case <-c.Done():
			log.Println("done...")
			atomic.AddInt64(&stoppedCnt, 1)
			return

		default:
			time.Sleep(time.Millisecond)
			atomic.AddInt64(&continueCnt, 1)
		}
	}
}

g := New()
g.Notify(syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2)
g.Start(f1)
g.Start(f1)
g.Start(f1)
g.Start(f1)

g.Wait()

fmt.Println(stoppedCnt, continueCnt > 0)
Output:

4 true

func (*Grace) Start

func (g *Grace) Start(f func(context.Context))

func (*Grace) StopAndWait

func (g *Grace) StopAndWait()
Example
continueCnt := int64(0)
stoppedCnt := int64(0)
f1 := func(c context.Context) {
	for {
		select {

		case <-c.Done():
			log.Println("done...")
			atomic.AddInt64(&stoppedCnt, 1)
			return

		default:
			time.Sleep(time.Millisecond)
			atomic.AddInt64(&continueCnt, 1)
		}
	}
}

g := New()
g.Start(f1)
g.Start(f1)
g.Start(f1)
g.Start(f1)

time.Sleep(time.Second)
g.StopAndWait()

fmt.Println(stoppedCnt, continueCnt > 0)
Output:

4 true

func (*Grace) Wait

func (g *Grace) Wait()

Jump to

Keyboard shortcuts

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