mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
41
vendor/github.com/moby/buildkit/util/appcontext/appcontext.go
generated
vendored
Normal file
41
vendor/github.com/moby/buildkit/util/appcontext/appcontext.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
package appcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var appContextCache context.Context
|
||||
var appContextOnce sync.Once
|
||||
|
||||
// Context returns a static context that reacts to termination signals of the
|
||||
// running process. Useful in CLI tools.
|
||||
func Context() context.Context {
|
||||
appContextOnce.Do(func() {
|
||||
signals := make(chan os.Signal, 2048)
|
||||
signal.Notify(signals, terminationSignals...)
|
||||
|
||||
const exitLimit = 3
|
||||
retries := 0
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
appContextCache = ctx
|
||||
|
||||
go func() {
|
||||
for {
|
||||
<-signals
|
||||
cancel()
|
||||
retries++
|
||||
if retries >= exitLimit {
|
||||
logrus.Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
return appContextCache
|
||||
}
|
11
vendor/github.com/moby/buildkit/util/appcontext/appcontext_unix.go
generated
vendored
Normal file
11
vendor/github.com/moby/buildkit/util/appcontext/appcontext_unix.go
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
// +build !windows
|
||||
|
||||
package appcontext
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var terminationSignals = []os.Signal{unix.SIGTERM, unix.SIGINT}
|
7
vendor/github.com/moby/buildkit/util/appcontext/appcontext_windows.go
generated
vendored
Normal file
7
vendor/github.com/moby/buildkit/util/appcontext/appcontext_windows.go
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
package appcontext
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var terminationSignals = []os.Signal{os.Interrupt}
|
Reference in New Issue
Block a user