feat: improve channel resilience and operations

This commit is contained in:
hectorzhao
2026-07-24 08:17:33 +08:00
parent 2f781ebb8a
commit afd3c96070
43 changed files with 1969 additions and 299 deletions
+36
View File
@@ -1,6 +1,7 @@
package upstream
import (
"context"
"testing"
"cmpp-platform/gateway/internal/queue"
@@ -39,6 +40,38 @@ func TestConnectionPoolAcquiresAcrossConnections(t *testing.T) {
releaseSecond()
}
func TestManagerDisconnectChannelRemovesPoolAndStopsReconnects(t *testing.T) {
pool := &connectionPool{
channelID: "channel-1",
connectionID: "channel-1:primary",
config: normalizeUpstreamConfig(queueUpstreamConfigForTest()),
reconnectSignal: make(chan struct{}, 1),
stopCh: make(chan struct{}),
}
manager := &Manager{conns: map[string]*connectionPool{"channel-1": pool}}
state, err := manager.DisconnectChannel(context.Background(), queue.DisconnectChannelCommand{
MessageType: queue.MessageTypeDisconnectChannel,
ChannelID: "channel-1",
ConnectionID: "channel-1:primary",
})
if err != nil {
t.Fatalf("disconnect channel: %v", err)
}
if state.Status != "disconnected" || state.CurrentConnections != 0 {
t.Fatalf("unexpected state: %+v", state)
}
if _, exists := manager.conns["channel-1"]; exists {
t.Fatal("expected channel pool to be removed")
}
select {
case <-pool.stopCh:
default:
t.Fatal("expected reconnect supervisor to be stopped")
}
}
func TestNormalizeUpstreamConfigDefaults(t *testing.T) {
config := normalizeUpstreamConfig(queueUpstreamConfigForTest())
if config.DesiredConnections != 1 {
@@ -47,6 +80,9 @@ func TestNormalizeUpstreamConfigDefaults(t *testing.T) {
if config.WindowSize != defaultWindowSize {
t.Fatalf("WindowSize = %d, want %d", config.WindowSize, defaultWindowSize)
}
if config.HeartbeatIntervalSeconds != 30 || config.HeartbeatMissThreshold != 3 {
t.Fatalf("unexpected heartbeat defaults: interval=%d threshold=%d", config.HeartbeatIntervalSeconds, config.HeartbeatMissThreshold)
}
}
func queueUpstreamConfigForTest() queue.UpstreamConfig {