2021-03-25 08:27:59 +00:00
|
|
|
package global
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-30 22:46:33 +00:00
|
|
|
globalContext context.Context
|
|
|
|
globalContextOnce sync.Once
|
|
|
|
globalContextBarrier = make(chan struct{})
|
2021-03-25 08:27:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Context() context.Context {
|
2021-03-30 22:46:33 +00:00
|
|
|
globalContextOnce.Do(func() {
|
2021-03-25 08:27:59 +00:00
|
|
|
globalContext, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
2021-03-30 22:46:33 +00:00
|
|
|
close(globalContextBarrier)
|
2021-03-25 08:27:59 +00:00
|
|
|
})
|
2021-03-30 22:46:33 +00:00
|
|
|
<-globalContextBarrier
|
2021-03-25 08:27:59 +00:00
|
|
|
return globalContext
|
|
|
|
}
|