Skip to content

Commit bcf1ea2

Browse files
committed
chore: use sig 0 for user shutdown
1 parent fd61b1d commit bcf1ea2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

rolling-shutter/keyper/kprapi/kprapi.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package kprapi
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"net/http"
87
"os"
98
"time"
@@ -18,6 +17,7 @@ import (
1817
"github.com/rs/zerolog/log"
1918

2019
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/kproapi"
20+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
2121
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/retry"
2222
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
2323
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
@@ -40,13 +40,12 @@ type server struct {
4040
shutdownSig chan struct{}
4141
}
4242

43-
var ErrShutdownRequested = errors.New("shutdown requested from API")
44-
4543
func NewHTTPService(dbpool *pgxpool.Pool, config Config, p2p P2PMessageSender) service.Service {
4644
return &server{
47-
dbpool: dbpool,
48-
config: config,
49-
p2p: p2p,
45+
dbpool: dbpool,
46+
config: config,
47+
p2p: p2p,
48+
shutdownSig: make(chan struct{}),
5049
}
5150
}
5251

@@ -94,7 +93,6 @@ func (srv *server) Start(ctx context.Context, runner service.Runner) error {
9493
Handler: srv.setupRouter(),
9594
ReadHeaderTimeout: 5 * time.Second,
9695
}
97-
srv.shutdownSig = make(chan struct{})
9896
runner.Defer(func() { close(srv.shutdownSig) })
9997

10098
runner.Go(httpServer.ListenAndServe)
@@ -120,7 +118,7 @@ func (srv *server) waitShutdown(ctx context.Context) error {
120118
// but not stop execution
121119
return nil
122120
}
123-
return ErrShutdownRequested
121+
return medley.ErrShutdownRequested
124122
case <-ctx.Done():
125123
// we canceled somewhere else
126124
return nil

rolling-shutter/medley/medley.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const receiptPollInterval = 500 * time.Millisecond
2626

2727
var errAddressNotFound = errors.New("address not found")
2828

29+
var ErrShutdownRequested = errors.New("shutdown requested from user")
30+
2931
// FindAddressIndex returns the index of the given address inside the slice of addresses or returns
3032
// an error, if the slice does not contain the given address.
3133
func FindAddressIndex(addresses []common.Address, addr common.Address) (int, error) {

rolling-shutter/medley/service/service.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package service
33

44
import (
55
"context"
6+
"errors"
67
"os"
78
"os/signal"
89
"sync"
910
"syscall"
1011

1112
"github.com/rs/zerolog/log"
1213
"golang.org/x/sync/errgroup"
14+
15+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
1316
)
1417

1518
type Runner interface {
@@ -100,6 +103,11 @@ func RunWithSighandler(ctx context.Context, services ...Service) error {
100103
log.Info().Msg("bye")
101104
return nil
102105
}
106+
if errors.Is(err, medley.ErrShutdownRequested) {
107+
log.Info().Msg("user shut down service")
108+
log.Info().Msg("bye")
109+
return nil
110+
}
103111

104112
return err
105113
}

0 commit comments

Comments
 (0)