feat(platform): add postgres, redis, logging and graceful http serving
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
9662a3cea2
commit
1a6fadb030
6 files changed
+231
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Serve blocks until ctx is cancelled, then marks the instance as draining,
|
||||
// waits drainDelay for load balancers to notice and shuts down gracefully.
|
||||
func Serve(ctx context.Context, log *slog.Logger, srv *http.Server, health *Health, drainDelay, timeout time.Duration) error {
|
||||
errc := make(chan error, 1)
|
||||
go func() {
|
||||
log.Info("http listening", "addr", srv.Addr)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
errc <- err
|
||||
}
|
||||
close(errc)
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errc:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
health.Drain()
|
||||
time.Sleep(drainDelay)
|
||||
|
||||
sctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
return srv.Shutdown(sctx)
|
||||
}
|
||||
|
||||
func NewHTTPServer(addr string, h http.Handler) *http.Server {
|
||||
return &http.Server{
|
||||
Addr: addr,
|
||||
Handler: h,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user