kilianpaquier / exithandler

Easily handle exit signals in golang

Home Page:https://pkg.go.dev/github.com/kilianpaquier/exithandler/pkg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

exithandler

Archive note: Just copy/paste the related code listening to context signal 😉.

GitHub Actions GitHub Release GitHub Issues GitHub License Coverage Go Version Go Report Card


How to use ?

go get -u github.com/kilianpaquier/exithandler@latest

Features

The exithandler package exposes two useful functions to handle program terminations:

The first one is Handle which will blocked on SIGINT and SIGTERM signals until one of those are sent and then executes the provided function.

func main() {
    // some things to be defined

    go exithandler.Handle(ctx, func(context.Context) {
        // some things to close or execute when the program terminates
    })
}

The second one is HandleFunc which does the exact same thing, the only difference is that it returns the function which will wait and does not wait directly (as provided in below example).

func main() {
    // some things to be defined

    exithandler := exithandler.HandleFunc(ctx, func(context.Context) {
        // some things to close or execute when the program terminates
    })

    // other things to do

    exithandler()
}

About

Easily handle exit signals in golang

https://pkg.go.dev/github.com/kilianpaquier/exithandler/pkg

License:MIT License


Languages

Language:Go 73.5%Language:Makefile 26.5%