fix(server): gracefully shut down HTTP proxy to drain in-flight requests

On signal, main waited on wg for StartHTTPProxy's server.Listen, which never
returns because nothing called server.Shutdown(); it timed out after 10s and
process-exited, abruptly killing in-flight GraphQL requests.

Expose the proxy Fiber app and register an http-proxy Shutdown component so
server.Listen returns after Shutdown, letting connections drain. Guard the
shared app handle with a mutex (written by the StartHTTPProxy goroutine, read
by the shutdown component). Verified with build, vet, full test and full
race suite.
This commit is contained in:
2026-08-13 02:37:14 +01:00
parent d04ab329a1
commit db2216973e
3 changed files with 92 additions and 0 deletions
+27
View File
@@ -1,8 +1,10 @@
package main
import (
"context"
"fmt"
"strconv"
"sync"
"time"
"github.com/goccy/go-json"
@@ -36,6 +38,28 @@ type DependencyStatus struct {
}
// StartHTTPProxy initializes and starts the HTTP proxy server.
// httpProxyServer holds the live proxy Fiber app so the shutdown sequence can
// call Shutdown() on it, letting in-flight requests drain instead of being
// cut off by process exit. It is written by the StartHTTPProxy goroutine and
// read by the shutdown component, hence guarding with a mutex.
var (
httpProxyMu sync.RWMutex
httpProxyApp *fiber.App
)
// shutdownHTTPProxy gracefully stops the HTTP proxy server. It is invoked from
// the ShutdownManager during shutdown so server.Listen returns and the proxy
// can finish in-flight requests.
func shutdownHTTPProxy(_ context.Context) error {
httpProxyMu.RLock()
app := httpProxyApp
httpProxyMu.RUnlock()
if app != nil {
return app.Shutdown()
}
return nil
}
func StartHTTPProxy() error {
cfg.Logger.Debug(&libpack_logger.LogMessage{
Message: "Starting the HTTP proxy",
@@ -51,6 +75,9 @@ func StartHTTPProxy() error {
}
server := fiber.New(serverConfig)
httpProxyMu.Lock()
httpProxyApp = server
httpProxyMu.Unlock()
server.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},