Skip to content

Commit fd61b1d

Browse files
committed
feat: add shutdown endpoint to internal keyper API
1 parent 19a562d commit fd61b1d

File tree

4 files changed

+81
-17
lines changed

4 files changed

+81
-17
lines changed

rolling-shutter/keyper/kprapi/http.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func (srv *server) Ping(w http.ResponseWriter, _ *http.Request) {
3131
_, _ = w.Write([]byte("pong"))
3232
}
3333

34+
func (srv *server) Shutdown(_ http.ResponseWriter, _ *http.Request) {
35+
srv.shutdownSig <- struct{}{}
36+
// We still want to return here and thus return 200 to the caller after this.
37+
// Not immediately closing open connctions is taken care
38+
// of by the graceful shutdown of the http server.
39+
}
40+
3441
func (srv *server) GetDecryptionKey(w http.ResponseWriter, r *http.Request, eon int, epochID kproapi.EpochID) {
3542
ctx := r.Context()
3643
db := database.New(srv.dbpool)

rolling-shutter/keyper/kprapi/kprapi.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kprapi
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"net/http"
78
"os"
89
"time"
@@ -33,11 +34,14 @@ type Config interface {
3334
}
3435

3536
type server struct {
36-
dbpool *pgxpool.Pool
37-
config Config
38-
p2p P2PMessageSender
37+
dbpool *pgxpool.Pool
38+
config Config
39+
p2p P2PMessageSender
40+
shutdownSig chan struct{}
3941
}
4042

43+
var ErrShutdownRequested = errors.New("shutdown requested from API")
44+
4145
func NewHTTPService(dbpool *pgxpool.Pool, config Config, p2p P2PMessageSender) service.Service {
4246
return &server{
4347
dbpool: dbpool,
@@ -90,7 +94,13 @@ func (srv *server) Start(ctx context.Context, runner service.Runner) error {
9094
Handler: srv.setupRouter(),
9195
ReadHeaderTimeout: 5 * time.Second,
9296
}
97+
srv.shutdownSig = make(chan struct{})
98+
runner.Defer(func() { close(srv.shutdownSig) })
99+
93100
runner.Go(httpServer.ListenAndServe)
101+
runner.Go(func() error {
102+
return srv.waitShutdown(ctx)
103+
})
94104
runner.Go(func() error {
95105
<-ctx.Done()
96106
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
@@ -100,6 +110,24 @@ func (srv *server) Start(ctx context.Context, runner service.Runner) error {
100110
return nil
101111
}
102112

113+
func (srv *server) waitShutdown(ctx context.Context) error {
114+
for {
115+
select {
116+
case _, ok := <-srv.shutdownSig:
117+
if !ok {
118+
// channel close without a send
119+
// means we want to stop the shutdown waiter
120+
// but not stop execution
121+
return nil
122+
}
123+
return ErrShutdownRequested
124+
case <-ctx.Done():
125+
// we canceled somewhere else
126+
return nil
127+
}
128+
}
129+
}
130+
103131
func (srv *server) setupAPIRouter(swagger *openapi3.T) http.Handler {
104132
router := chi.NewRouter()
105133

rolling-shutter/keyper/kproapi/oapi.gen.go

Lines changed: 36 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rolling-shutter/keyper/kproapi/oapi.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ paths:
1616
operationId: ping
1717
parameters:
1818

19+
/shutdown:
20+
post:
21+
description: |
22+
Shut down the served
23+
operationId: shutdown
24+
parameters:
25+
1926
/eons:
2027
get:
2128
description: |

0 commit comments

Comments
 (0)