Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Migrate to go1.16
Browse files Browse the repository at this point in the history
- fixes for tests
- graceful for go1.15 and below
- graceful for go1.16 and above
  • Loading branch information
im-kulikov committed Feb 22, 2021
1 parent 4be16b5 commit 4aec20f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
20 changes: 0 additions & 20 deletions grace/grace.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
package grace

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/im-kulikov/helium/module"
"go.uber.org/zap"
)

// Module graceful context
var Module = module.Module{
{Constructor: NewGracefulContext},
}

// NewGracefulContext returns graceful context
func NewGracefulContext(l *zap.Logger) context.Context {
ctx, cancel := context.WithCancel(context.Background())
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
sig := <-ch
l.Info("received signal",
zap.String("signal", sig.String()))
cancel()
}()
return ctx
}
26 changes: 26 additions & 0 deletions grace/grace_go1.15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// +build !go1.16

package grace

import (
"context"
"os"
"os/signal"
"syscall"

"go.uber.org/zap"
)

// NewGracefulContext returns graceful context
func NewGracefulContext(l *zap.Logger) context.Context {
ctx, cancel := context.WithCancel(context.Background())
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
sig := <-ch
l.Info("received stop signal",
zap.String("signal", sig.String()))
cancel()
}()
return ctx
}
24 changes: 24 additions & 0 deletions grace/grace_go1.16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build go1.16

package grace

import (
"context"
"os/signal"
"syscall"

"go.uber.org/zap"
)

// NewGracefulContext returns graceful context
func NewGracefulContext(l *zap.Logger) context.Context {
ctx, _ := signal.NotifyContext(context.Background(),
syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

go func() {
<-ctx.Done()
l.Info("receive stop signal")
}()

return ctx
}
16 changes: 7 additions & 9 deletions grace/grace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ import (
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

func TestGrace(t *testing.T) {
var (
log = zap.L()
ctx = NewGracefulContext(log)
)

// waiting to run the goroutine and channel of signals
<-time.Tick(100 * time.Millisecond)

signals := []syscall.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP}
for i := range signals {
sig := signals[i]
t.Run(fmt.Sprintf("should cancel context on %s signal", sig), func(t *testing.T) {
is := assert.New(t)

log := zaptest.NewLogger(t)
ctx := NewGracefulContext(log)

// waiting to run the goroutine and channel of signals
<-time.After(time.Millisecond)

err := syscall.Kill(syscall.Getpid(), sig)
is.NoError(err)

Expand Down

0 comments on commit 4aec20f

Please sign in to comment.