Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rolling-shutter/keyper/kprapi/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func (srv *server) Ping(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("pong"))
}

func (srv *server) Shutdown(_ http.ResponseWriter, _ *http.Request) {
srv.shutdownSig <- struct{}{}
// We still want to return here and thus return 200 to the caller after this.
// Not immediately closing open connctions is taken care
// of by the graceful shutdown of the http server.
}

func (srv *server) GetDecryptionKey(w http.ResponseWriter, r *http.Request, eon int, epochID kproapi.EpochID) {
ctx := r.Context()
db := database.New(srv.dbpool)
Expand Down
38 changes: 32 additions & 6 deletions rolling-shutter/keyper/kprapi/kprapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,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 @@ -33,16 +34,18 @@ type Config interface {
}

type server struct {
dbpool *pgxpool.Pool
config Config
p2p P2PMessageSender
dbpool *pgxpool.Pool
config Config
p2p P2PMessageSender
shutdownSig chan struct{}
}

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 @@ -90,7 +93,12 @@ func (srv *server) Start(ctx context.Context, runner service.Runner) error {
Handler: srv.setupRouter(),
ReadHeaderTimeout: 5 * time.Second,
}
runner.Defer(func() { close(srv.shutdownSig) })

runner.Go(httpServer.ListenAndServe)
runner.Go(func() error {
return srv.waitShutdown(ctx)
})
runner.Go(func() error {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
Expand All @@ -100,6 +108,24 @@ func (srv *server) Start(ctx context.Context, runner service.Runner) error {
return nil
}

func (srv *server) waitShutdown(ctx context.Context) error {
for {
select {
case _, ok := <-srv.shutdownSig:
if !ok {
// channel close without a send
// means we want to stop the shutdown waiter
// but not stop execution
return nil
}
return medley.ErrShutdownRequested
case <-ctx.Done():
// we canceled somewhere else
return nil
}
}
}

func (srv *server) setupAPIRouter(swagger *openapi3.T) http.Handler {
router := chi.NewRouter()

Expand Down
50 changes: 36 additions & 14 deletions rolling-shutter/keyper/kproapi/oapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions rolling-shutter/keyper/kproapi/oapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ paths:
operationId: ping
parameters:

/shutdown:
post:
description: |
Shut down the served
operationId: shutdown
parameters:

/eons:
get:
description: |
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