Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up interrupt package #3379

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions private/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,11 @@ func Main(ctx context.Context, f func(context.Context, Container) error) {
// The run will be stopped on interrupt signal.
// The exit code can be determined using GetExitCode.
func Run(ctx context.Context, container Container, f func(context.Context, Container) error) error {
ctx, cancel := interrupt.WithCancel(ctx)
defer cancel()
ctx, cancel := interrupt.NotifyContext(ctx)
go func() {
<-ctx.Done()
cancel()
}()
if err := f(ctx, container); err != nil {
printError(container, err)
return err
Expand Down
33 changes: 5 additions & 28 deletions private/pkg/interrupt/interrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,10 @@ import (
"os/signal"
)

var signals = append(
[]os.Signal{
os.Interrupt,
},
extraSignals...,
)

// WithCancel returns a context that is cancelled if interrupt signals are sent.
func WithCancel(ctx context.Context) (context.Context, context.CancelFunc) {
signalC, closer := NewSignalChannel()
ctx, cancel := context.WithCancel(ctx)
go func() {
<-signalC
closer()
cancel()
}()
return ctx, cancel
}
var interruptSignals = append([]os.Signal{os.Interrupt}, extraSignals...)

// NewSignalChannel returns a new channel for interrupt signals.
//
// Call the returned function to cancel sending to this channel.
func NewSignalChannel() (<-chan os.Signal, func()) {
signalC := make(chan os.Signal, 1)
signal.Notify(signalC, signals...)
return signalC, func() {
signal.Stop(signalC)
close(signalC)
}
// NotifyContext returns a new [context.Context] from [signal.NotifyContext]
// with the appropriate interrupt signals.
func NotifyContext(ctx context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(ctx, interruptSignals...)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build windows
// +build windows
//go:build !unix

package interrupt

Expand All @@ -22,7 +21,6 @@ import "os"
// extraSignals are signals beyond os.Interrupt that we want to be handled
// as interrupts.
//
// For unix-like platforms, this adds syscall.SIGTERM, although this is only
// tested on darwin and linux, which buf officially supports. Other unix-like
// platforms should have this as well, however.
// For unix-like platforms, this adds syscall.SIGTERM. No other signals
// are added for other platforms.
var extraSignals = []os.Signal{}
14 changes: 4 additions & 10 deletions private/pkg/interrupt/interrupt_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Matching the unix-like build tags in the Golang source i.e. https://github.com/golang/go/blob/912f0750472dd4f674b69ca1616bfaf377af1805/src/os/file_unix.go#L6

//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
//go:build unix

package interrupt

Expand All @@ -27,9 +24,6 @@ import (
// extraSignals are signals beyond os.Interrupt that we want to be handled
// as interrupts.
//
// For unix-like platforms, this adds syscall.SIGTERM, although this is only
// tested on darwin and linux, which buf officially supports. Other unix-like
// platforms should have this as well, however.
var extraSignals = []os.Signal{
syscall.SIGTERM,
}
// For unix-like platforms, this adds syscall.SIGTERM. No other signals
// are added for other platforms.
var extraSignals = []os.Signal{syscall.SIGTERM}
Loading