Skip to content

Commit 6b3836c

Browse files
committed
Initial commit
0 parents  commit 6b3836c

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
.DS_STORE

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Vivan Kumar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: fmt lint test
2+
3+
lint:
4+
go vet ./...
5+
golint ./...
6+
7+
fmt:
8+
go fmt -s -w .
9+
10+
test:
11+
go test ./... -v

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## signal
2+
3+
Reduce OS signal handling boilerplate for Go programs.
4+
5+
This is mostly written for my own use, really.
6+
Found myself reaching for this bit of code quite often across a few codebases.
7+
8+
## Rationale
9+
10+
This library is intended to be used with long running applications that often rely on OS signals being handled.
11+
12+
It is built on the common Go idiom of propagating context through the function call chain.
13+
14+
## Usage
15+
16+
signal provides a single function that wraps the application with signal handling code, returning a context that is cancelled when a OS signal is sent.
17+
18+
It allows all parts of an application relying on context cancellation to be cancelled together.
19+
20+
It reduces code like this (assuming that a `Run` method is implemented)
21+
22+
```go
23+
ctx, cancel := context.WithCancel(context.Background())
24+
go srv.Run(ctx)
25+
26+
sigint := make(chan os.Signal, 1)
27+
signal.Notify(sigint, os.Interrupt)
28+
<-sigint
29+
30+
cancel()
31+
32+
// and so on..
33+
```
34+
35+
into
36+
37+
```go
38+
ctx := context.Background()
39+
err := signal.Wrap(ctx, srv, os.Interrupt)
40+
if err != nil {
41+
fmt.Println(err.Error())
42+
}
43+
```
44+
45+
## Linting, formatting & tests
46+
47+
```
48+
make lint
49+
make fmt
50+
make test
51+
```

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/vivangkumar/signal
2+
3+
go 1.20

signal.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Package signal provides context cancellation by handling OS signals.
2+
//
3+
// It provides a minimal interface to handle commonly occurring repeated
4+
// go code when building services.
5+
//
6+
// It uses the same package name as the go std lib in an effort to ensure
7+
// that callers don't have to rely on it to implement OS signal handling.
8+
package signal
9+
10+
import (
11+
"context"
12+
"fmt"
13+
"os"
14+
"os/signal"
15+
)
16+
17+
// Runner represents a runnable entity.
18+
type Runner interface {
19+
// Run runs the entity with the provided context.
20+
Run(ctx context.Context) error
21+
}
22+
23+
// Wrap provides context cancellation by gracefully handling OS signals.
24+
//
25+
// It accepts any type that implements the Runner interface.
26+
//
27+
// Typically, this should be used in conjunction with a long running
28+
// process that relies on contexts for cancellation, ensuring that the
29+
// chain of function calls that propagate the same context are also
30+
// cancelled.
31+
func Wrap(ctx context.Context, r Runner, sig ...os.Signal) error {
32+
err := r.Run(signalCtx(ctx, sig...))
33+
if err != nil {
34+
return fmt.Errorf("signal: %w", err)
35+
}
36+
37+
return nil
38+
}
39+
40+
// signalCtx returns a context that is cancelled when encountering an OS signal.
41+
func signalCtx(ctx context.Context, sig ...os.Signal) context.Context {
42+
ctx, cancel := context.WithCancel(ctx)
43+
44+
go func() {
45+
c := make(chan os.Signal, len(sig))
46+
47+
signal.Notify(c, sig...)
48+
defer signal.Stop(c)
49+
50+
select {
51+
case <-ctx.Done():
52+
case <-c:
53+
cancel()
54+
}
55+
}()
56+
57+
return ctx
58+
}

signal_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package signal_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"syscall"
7+
"testing"
8+
"time"
9+
10+
"github.com/vivangkumar/signal"
11+
)
12+
13+
type app struct{}
14+
15+
func (a app) Run(ctx context.Context) error {
16+
// Wait on context to be cancelled.
17+
<-ctx.Done()
18+
return ctx.Err()
19+
}
20+
21+
func TestWrap(t *testing.T) {
22+
a := app{}
23+
ctx := context.Background()
24+
25+
go func() {
26+
// Kill the current process after a second.
27+
<-time.After(1 * time.Second)
28+
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
29+
}()
30+
31+
err := signal.Wrap(ctx, a, syscall.SIGINT)
32+
if !errors.Is(err, context.Canceled) {
33+
t.Errorf("expected context cancelled, but got %s", err.Error())
34+
t.Fail()
35+
}
36+
}

0 commit comments

Comments
 (0)