Skip to content

Commit

Permalink
feat: add shutdown endpoint to internal keyper API
Browse files Browse the repository at this point in the history
  • Loading branch information
ezdac committed Jan 12, 2024
1 parent 19a562d commit fd61b1d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 17 deletions.
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
34 changes: 31 additions & 3 deletions rolling-shutter/keyper/kprapi/kprapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kprapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -33,11 +34,14 @@ type Config interface {
}

type server struct {
dbpool *pgxpool.Pool
config Config
p2p P2PMessageSender
dbpool *pgxpool.Pool
config Config
p2p P2PMessageSender
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,
Expand Down Expand Up @@ -90,7 +94,13 @@ 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)
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 +110,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 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

0 comments on commit fd61b1d

Please sign in to comment.