Skip to content

Commit

Permalink
Pull request 107: svc-windows
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 6c90850
Author: Ainar Garipov <[email protected]>
Date:   Fri Apr 12 19:01:08 2024 +0300

    osutil, service: windows support
  • Loading branch information
ainar-g committed Apr 12, 2024
1 parent 3de5106 commit 9d5cd39
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 17 deletions.
10 changes: 10 additions & 0 deletions osutil/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ func (n DefaultSignalNotifier) Notify(c chan<- os.Signal, sig ...os.Signal) {
func (n DefaultSignalNotifier) Stop(c chan<- os.Signal) {
signal.Stop(c)
}

// IsShutdownSignal returns true if sig is a shutdown signal.
func IsShutdownSignal(sig os.Signal) (ok bool) {
return isShutdownSignal(sig)
}

// NotifyShutdownSignal notifies c on receiving shutdown signals using n.
func NotifyShutdownSignal(n SignalNotifier, c chan<- os.Signal) {
notifyShutdownSignal(n, c)
}
27 changes: 27 additions & 0 deletions osutil/signal_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build unix

package osutil

import (
"os"

"golang.org/x/sys/unix"
)

// isShutdownSignal returns true if sig is a Unix shutdown signal.
func isShutdownSignal(sig os.Signal) (ok bool) {
switch sig {
case
unix.SIGINT,
unix.SIGQUIT,
unix.SIGTERM:
return true
default:
return false
}
}

// notifyShutdownSignal notifies c on receiving Unix shutdown signals using n.
func notifyShutdownSignal(n SignalNotifier, c chan<- os.Signal) {
n.Notify(c, unix.SIGINT, unix.SIGQUIT, unix.SIGTERM)
}
26 changes: 26 additions & 0 deletions osutil/signal_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build windows

package osutil

import (
"os"
"syscall"
)

// isShutdownSignal returns true if sig is a Windows shutdown signal.
func isShutdownSignal(sig os.Signal) (ok bool) {
switch sig {
case os.Interrupt, syscall.SIGTERM:
return true
default:
return false
}
}

// notifyShutdownSignal notifies c on receiving Windows shutdown signals using
// n.
func notifyShutdownSignal(n SignalNotifier, c chan<- os.Signal) {
// syscall.SIGTERM is processed automatically. See go doc os/signal,
// section Windows.
n.Notify(c, os.Interrupt)
}
2 changes: 0 additions & 2 deletions service/service_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build unix

package service_test

import "time"
Expand Down
15 changes: 2 additions & 13 deletions service/signal.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build unix

package service

import (
Expand All @@ -11,12 +9,9 @@ import (

"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/osutil"
"golang.org/x/sys/unix"
)

// SignalHandler processes incoming signals and shuts services down.
//
// TODO(a.garipov): Expand to Windows.
type SignalHandler struct {
logger *slog.Logger
signal chan os.Signal
Expand Down Expand Up @@ -73,9 +68,8 @@ func NewSignalHandler(c *SignalHandlerConfig) (h *SignalHandler) {
shutdownTimeout: cmp.Or(c.ShutdownTimeout, defaultSignalHandlerConf.ShutdownTimeout),
}

// TODO(a.garipov): Expand these to Windows.
notifier := cmp.Or(c.SignalNotifier, defaultSignalHandlerConf.SignalNotifier)
notifier.Notify(h.signal, unix.SIGINT, unix.SIGQUIT, unix.SIGTERM)
osutil.NotifyShutdownSignal(notifier, h.signal)

return h
}
Expand All @@ -100,12 +94,7 @@ func (h *SignalHandler) Handle(ctx context.Context) (status osutil.ExitCode) {
for sig := range h.signal {
h.logger.InfoContext(ctx, "received", "signal", sig)

switch sig {
case
unix.SIGINT,
unix.SIGQUIT,
unix.SIGTERM:

if osutil.IsShutdownSignal(sig) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, h.shutdownTimeout)
defer cancel()
Expand Down
2 changes: 0 additions & 2 deletions service/signal_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build unix

package service_test

import (
Expand Down
18 changes: 18 additions & 0 deletions testutil/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testutil

import (
"context"
"testing"
"time"
)

// ContextWithTimeout is a helper that creates a new context with timeout and
// registers ctx's cleanup with tb.Cleanup.
func ContextWithTimeout(tb testing.TB, timeout time.Duration) (ctx context.Context) {
tb.Helper()

ctx, cancel := context.WithTimeout(context.Background(), timeout)
tb.Cleanup(cancel)

return ctx
}

0 comments on commit 9d5cd39

Please sign in to comment.