You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/exegroup/signal.go

28 lines
522 B
Go

package exegroup
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
)
// HandleSignal 提供信号处理函数;
func HandleSignal(signals ...os.Signal) func(ctx context.Context) error {
c := make(chan os.Signal, 1)
if len(signals) == 0 {
signals = []os.Signal{syscall.SIGTERM, syscall.SIGINT}
}
signal.Notify(c, signals...)
return func(ctx context.Context) error {
defer signal.Stop(c)
select {
case sig := <-c:
return fmt.Errorf("signal %s", sig)
case <-ctx.Done():
return ctx.Err()
}
}
}