Skip to content

Commit

Permalink
chore: use sig 0 for user shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ezdac committed Jan 12, 2024
1 parent fd61b1d commit bcf1ea2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
14 changes: 6 additions & 8 deletions rolling-shutter/keyper/kprapi/kprapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kprapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"os"
"time"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/kproapi"
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/retry"
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
Expand All @@ -40,13 +40,12 @@ type server struct {
shutdownSig chan struct{}
}

var ErrShutdownRequested = errors.New("shutdown requested from API")

func NewHTTPService(dbpool *pgxpool.Pool, config Config, p2p P2PMessageSender) service.Service {
return &server{
dbpool: dbpool,
config: config,
p2p: p2p,
dbpool: dbpool,
config: config,
p2p: p2p,
shutdownSig: make(chan struct{}),
}
}

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

runner.Go(httpServer.ListenAndServe)
Expand All @@ -120,7 +118,7 @@ func (srv *server) waitShutdown(ctx context.Context) error {
// but not stop execution
return nil
}
return ErrShutdownRequested
return medley.ErrShutdownRequested
case <-ctx.Done():
// we canceled somewhere else
return nil
Expand Down
2 changes: 2 additions & 0 deletions rolling-shutter/medley/medley.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const receiptPollInterval = 500 * time.Millisecond

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

var ErrShutdownRequested = errors.New("shutdown requested from user")

// FindAddressIndex returns the index of the given address inside the slice of addresses or returns
// an error, if the slice does not contain the given address.
func FindAddressIndex(addresses []common.Address, addr common.Address) (int, error) {
Expand Down
8 changes: 8 additions & 0 deletions rolling-shutter/medley/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package service

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

"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"

"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
)

type Runner interface {
Expand Down Expand Up @@ -100,6 +103,11 @@ func RunWithSighandler(ctx context.Context, services ...Service) error {
log.Info().Msg("bye")
return nil
}
if errors.Is(err, medley.ErrShutdownRequested) {
log.Info().Msg("user shut down service")
log.Info().Msg("bye")
return nil
}

return err
}
Expand Down

0 comments on commit bcf1ea2

Please sign in to comment.