feat: complete cmpp gateway delivery recovery workflows

This commit is contained in:
hectorzhao
2026-07-08 16:30:06 +08:00
parent cc628d0214
commit 8144f08652
60 changed files with 8901 additions and 94 deletions
+66 -2
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"log"
"net/http"
"os"
@@ -8,6 +9,8 @@ import (
"cmpp-platform/gateway/internal/control"
"cmpp-platform/gateway/internal/health"
"cmpp-platform/gateway/internal/inbound"
"cmpp-platform/gateway/internal/submitworker"
"cmpp-platform/gateway/internal/upstream"
)
func main() {
@@ -20,20 +23,81 @@ func main() {
cmppAddr = ":17890"
}
apiBaseURL := os.Getenv("API_BASE_URL")
upstreamManager := &upstream.Manager{APIBaseURL: apiBaseURL}
presenceStore, err := inbound.NewRedisPresenceStore(os.Getenv("REDIS_URL"))
if err != nil {
log.Printf("gateway downstream presence store init failed: %v", err)
}
recoveryStore, err := inbound.NewRedisRecoveryStore(os.Getenv("REDIS_URL"))
if err != nil {
log.Printf("gateway downstream recovery store init failed: %v", err)
}
go func() {
log.Printf("cmpp gateway inbound server listening on %s", cmppAddr)
if err := (inbound.Server{Addr: cmppAddr, APIBaseURL: apiBaseURL}).ListenAndServe(); err != nil {
if err := (inbound.Server{
Addr: cmppAddr,
APIBaseURL: apiBaseURL,
PresenceStore: presenceStore,
RecoveryStore: recoveryStore,
GatewayInstanceID: getenv("GATEWAY_INSTANCE_ID", hostname()),
}).ListenAndServe(); err != nil {
log.Fatalf("gateway inbound server stopped: %v", err)
}
}()
if os.Getenv("GATEWAY_SUBMIT_WORKER_DISABLED") != "true" {
worker, err := submitworker.New(os.Getenv("REDIS_URL"), upstreamManager)
if err != nil {
log.Printf("gateway submit worker init failed: %v", err)
} else {
worker.Stream = getenv("GATEWAY_SUBMIT_STREAM", "gateway.submit.commands")
worker.Group = getenv("GATEWAY_SUBMIT_GROUP", "cmpp-gateway")
worker.Consumer = getenv("GATEWAY_SUBMIT_CONSUMER", "gateway-1")
worker.APIBaseURL = apiBaseURL
go func() {
log.Printf("cmpp gateway submit worker consuming stream=%s group=%s consumer=%s", worker.Stream, worker.Group, worker.Consumer)
if err := worker.Run(context.Background()); err != nil {
log.Printf("gateway submit worker stopped: %v", err)
}
}()
}
}
mux := http.NewServeMux()
mux.Handle("/health", health.Handler())
control.Register(mux, control.Server{APIBaseURL: apiBaseURL})
control.Register(mux, control.Server{
APIBaseURL: apiBaseURL,
Upstream: upstreamManager,
RecoveryCandidates: func(ctx context.Context) ([]inbound.DownstreamPresence, error) {
return inbound.ListRecoveryCandidates(ctx, presenceStore)
},
RecoveryStatuses: func(ctx context.Context) ([]inbound.DownstreamRecoveryStatus, error) {
if recoveryStore == nil {
return nil, nil
}
return recoveryStore.ListRecoveryStatuses(ctx)
},
})
log.Printf("cmpp gateway control server listening on %s", addr)
if err := http.ListenAndServe(addr, mux); err != nil {
log.Fatalf("gateway control server stopped: %v", err)
}
}
func getenv(key string, fallback string) string {
value := os.Getenv(key)
if value == "" {
return fallback
}
return value
}
func hostname() string {
name, err := os.Hostname()
if err != nil || name == "" {
return "gateway-1"
}
return name
}