http and common
This commit is contained in:
+11
-32
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
@@ -26,6 +25,7 @@ import (
|
||||
"git.k6n.net/mats/platform/catalog"
|
||||
"git.k6n.net/mats/platform/config"
|
||||
"git.k6n.net/mats/platform/event"
|
||||
"git.k6n.net/mats/platform/httpserver"
|
||||
"git.k6n.net/mats/platform/rabbit"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -524,9 +524,11 @@ func main() {
|
||||
httpAddr := normalizeListenAddr(cartPort)
|
||||
debugAddr := normalizeListenAddr(config.EnvString("CART_DEBUG_PORT", "8081"))
|
||||
|
||||
// Debug mux on separate port (metrics + pprof).
|
||||
go http.ListenAndServe(debugAddr, debugMux)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: httpAddr,
|
||||
BaseContext: func(net.Listener) context.Context { return ctx },
|
||||
ReadTimeout: 10 * time.Second,
|
||||
// Close idle keep-alive connections so a load test with many short-lived
|
||||
// clients doesn't pin file handles open indefinitely.
|
||||
@@ -535,38 +537,15 @@ func main() {
|
||||
Handler: otelhttp.NewHandler(mux, "/"),
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
||||
fmt.Println("Shutting down due to signal")
|
||||
otelShutdown(context.Background())
|
||||
diskStorage.Close()
|
||||
pool.Close()
|
||||
|
||||
}()
|
||||
|
||||
srvErr := make(chan error, 1)
|
||||
go func() {
|
||||
srvErr <- srv.ListenAndServe()
|
||||
}()
|
||||
|
||||
// Inventory change consumption used to live here over the bare Redis
|
||||
// `inventory_changed` channel; it is now owned by the cart-inventory
|
||||
// service, which translates quantity changes into inventory.level_changed
|
||||
// bus crossings. The cart reads exact stock synchronously when it needs it.
|
||||
// See docs/inventory-shape.md.
|
||||
|
||||
log.Printf("Server started at %s (debug %s)", httpAddr, debugAddr)
|
||||
|
||||
go http.ListenAndServe(debugAddr, debugMux)
|
||||
|
||||
select {
|
||||
case err = <-srvErr:
|
||||
// Error when starting HTTP server.
|
||||
log.Fatalf("Unable to start server: %v", err)
|
||||
case <-ctx.Done():
|
||||
// Wait for first CTRL+C.
|
||||
// Stop receiving signal notifications as soon as possible.
|
||||
stop()
|
||||
if err := httpserver.RunServerWithShutdown(srv, "cart", 15*time.Second, 5*time.Second,
|
||||
func(context.Context) error { stop(); return nil },
|
||||
func(context.Context) error { otelShutdown(context.Background()); return nil },
|
||||
func(context.Context) error { diskStorage.Close(); return nil },
|
||||
func(context.Context) error { pool.Close(); return nil },
|
||||
); err != nil {
|
||||
log.Fatalf("server: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user