Skip to content

Commit 76c6281

Browse files
authored
Move pkg/signals from dapr/dapr to kit (dapr#70)
No code changes Signed-off-by: ItalyPaleAle <[email protected]>
1 parent 49532df commit 76c6281

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

signals/signals.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package signals
15+
16+
import (
17+
"context"
18+
"os"
19+
"os/signal"
20+
21+
"github.com/dapr/kit/logger"
22+
)
23+
24+
var (
25+
log = logger.NewLogger("dapr.signals")
26+
27+
// Inspired by
28+
// https://github.com/kubernetes-sigs/controller-runtime/blob/8499b67e316a03b260c73f92d0380de8cd2e97a1/pkg/manager/signals/signal.go#L25
29+
onlyOneSignalHandler = make(chan struct{})
30+
)
31+
32+
// Context returns a context which will be canceled when either the SIGINT
33+
// (windows and non windows) or SIGTERM (non windows) signal is caught. If
34+
// either signal is caught a second time, the program is terminated immediately
35+
// with exit code 1.
36+
func Context() context.Context {
37+
// panics when called twice
38+
close(onlyOneSignalHandler)
39+
40+
ctx, cancel := context.WithCancel(context.Background())
41+
sigCh := make(chan os.Signal, 1)
42+
signal.Notify(sigCh, shutdownSignals...)
43+
44+
go func() {
45+
sig := <-sigCh
46+
log.Infof(`Received signal '%s'; beginning shutdown`, sig)
47+
cancel()
48+
sig = <-sigCh
49+
log.Fatalf(
50+
`Received signal '%s' during shutdown; exiting immediately`,
51+
sig,
52+
)
53+
}()
54+
55+
return ctx
56+
}

signals/signals_posix.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2023 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package signals
18+
19+
import (
20+
"os"
21+
"syscall"
22+
)
23+
24+
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}

signals/signals_posix_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2023 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package signals
18+
19+
// Note this file is not built on Windows, as we depend on syscall methods not available on Windows.
20+
21+
import (
22+
"os/signal"
23+
"syscall"
24+
"testing"
25+
"time"
26+
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
func TestContext(t *testing.T) {
31+
signal.Reset()
32+
33+
t.Run("if receive signal, should cancel context", func(t *testing.T) {
34+
defer signal.Reset()
35+
onlyOneSignalHandler = make(chan struct{})
36+
37+
ctx := Context()
38+
assert.NoError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT))
39+
select {
40+
case <-ctx.Done():
41+
case <-time.After(1 * time.Second):
42+
t.Error("context should be cancelled in time")
43+
}
44+
})
45+
}

signals/signals_windows.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package signals
15+
16+
import (
17+
"os"
18+
)
19+
20+
var shutdownSignals = []os.Signal{os.Interrupt}

0 commit comments

Comments
 (0)