perf(cmpp): decouple supplier result callbacks

This commit is contained in:
hectorzhao
2026-08-20 14:19:42 +08:00
parent 485af688d2
commit 67b760a599
27 changed files with 1000 additions and 66 deletions
+48
View File
@@ -13,7 +13,9 @@ import (
"cmpp-platform/gateway/internal/health"
"cmpp-platform/gateway/internal/inbound"
platformmetrics "cmpp-platform/gateway/internal/metrics"
"cmpp-platform/gateway/internal/queue"
"cmpp-platform/gateway/internal/ratelimit"
"cmpp-platform/gateway/internal/resultoutbox"
"cmpp-platform/gateway/internal/submitworker"
"cmpp-platform/gateway/internal/upstream"
@@ -32,6 +34,7 @@ func main() {
apiBaseURL := os.Getenv("API_BASE_URL")
upstreamManager := &upstream.Manager{APIBaseURL: apiBaseURL}
var worker *submitworker.Worker
var resultOutbox *resultoutbox.Outbox
channelLimiter, err := ratelimit.New(os.Getenv("REDIS_URL"))
if err != nil {
log.Fatalf("gateway channel rate limiter init failed: %v", err)
@@ -70,12 +73,26 @@ func main() {
worker.Consumer = getenv("GATEWAY_SUBMIT_CONSUMER", "gateway-1")
worker.Concurrency = positiveEnvInt("GATEWAY_SUBMIT_WORKER_CONCURRENCY", 64)
worker.APIBaseURL = apiBaseURL
resultOutbox = resultoutbox.New(worker.Redis)
resultOutbox.Stream = getenv("GATEWAY_SUBMIT_RESULT_STREAM", "gateway.submit.results")
resultOutbox.Group = getenv("GATEWAY_SUBMIT_RESULT_GROUP", "cmpp-api-callback")
resultOutbox.Consumer = getenv("GATEWAY_SUBMIT_RESULT_CONSUMER", "gateway-1")
resultOutbox.APIBaseURL = apiBaseURL
resultOutbox.Concurrency = positiveEnvInt("GATEWAY_SUBMIT_RESULT_WORKER_CONCURRENCY", 8)
worker.ResultOutbox = resultOutbox
upstreamManager.SubmitSegmentPublisher = resultOutbox
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)
}
}()
go func() {
log.Printf("cmpp gateway result Outbox consuming stream=%s group=%s consumer=%s", resultOutbox.StreamName(), resultOutbox.GroupName(), resultOutbox.Consumer)
if err := resultOutbox.Run(context.Background()); err != nil {
log.Printf("gateway result Outbox worker stopped: %v", err)
}
}()
}
}
@@ -91,6 +108,11 @@ func main() {
snapshot.SubmitWorkerConcurrency = worker.ConfiguredConcurrency()
snapshot.SubmitWorkerInFlight = worker.InFlight()
}
if resultOutbox != nil {
snapshot.ResultWorkerUp = true
snapshot.ResultWorkerConcurrency = resultOutbox.ConfiguredConcurrency()
snapshot.ResultWorkerInFlight = resultOutbox.InFlight()
}
snapshot.InboundSubmitConcurrency, snapshot.InboundSubmitInFlight = inbound.SubmitSlotSnapshot()
if worker == nil || worker.Redis == nil {
return snapshot
@@ -117,12 +139,38 @@ func main() {
snapshot.QueueOldestAgeSeconds = max(0, time.Since(time.UnixMilli(milliseconds)).Seconds())
}
}
if resultOutbox != nil {
resultPending, resultErr := worker.Redis.XPending(ctx, resultOutbox.StreamName(), resultOutbox.GroupName()).Result()
if resultErr == nil {
snapshot.ResultQueueAvailable = true
snapshot.ResultQueuePending = resultPending.Count
}
resultGroups, resultErr := worker.Redis.XInfoGroups(ctx, resultOutbox.StreamName()).Result()
if resultErr == nil {
for _, group := range resultGroups {
if group.Name == resultOutbox.GroupName() {
snapshot.ResultQueueLag = group.Lag
break
}
}
}
}
return snapshot
}))
control.Register(mux, control.Server{
APIBaseURL: apiBaseURL,
Upstream: upstreamManager,
Limiter: channelLimiter,
Submit: func(ctx context.Context, command queue.SubmitCommand) (queue.SubmitResult, error) {
result, submitErr := upstreamManager.Submit(ctx, command)
if resultOutbox == nil {
return result, submitErr
}
if publishErr := resultOutbox.PublishSubmitResult(ctx, command, result); publishErr != nil {
return result, publishErr
}
return result, submitErr
},
RecoveryCandidates: func(ctx context.Context) ([]inbound.DownstreamPresence, error) {
return inbound.ListRecoveryCandidates(ctx, presenceStore)
},