Skip to content

Latest commit

 

History

History
38 lines (26 loc) · 894 Bytes

README.md

File metadata and controls

38 lines (26 loc) · 894 Bytes

goawaysignals

Go OS signals handler

About

goawaysignals handles SIGHUP, SIGINT, SIGTERM, SIGQUIT and then waits for the registered async functions to return before handing back the control to the callee. This helps you to achieve graceful shutdown on many kinds of services.

How to use

package main

import (
	"fmt"

	"github.com/eventials/goawaysignals"
)

func main() {
	// fake services that need graceful shutdown.
	producer := NewExampleProducer()
	consumer := NewExampleConsumer()

	goawaysignals.Async(func() { <-producer.NotifyClose() })
	goawaysignals.Async(func() { <-consumer.NotifyClose() })

	goawaysignals.AfterSignal(func() {
		producer.Close()
		consumer.Close()
	})

	goawaysignals.Wait()
	fmt.Println("Exiting...")
}

If you need to exit before an OS signal, you can call goawaysignals.Close() and still provide graceful shutdown to your services.