perf: expand gateway capacity and prevent receipt replay

This commit is contained in:
hectorzhao
2026-08-25 16:08:30 +08:00
parent 761c123b65
commit 9292352be1
48 changed files with 2001 additions and 144 deletions
+49 -2
View File
@@ -2,7 +2,9 @@ package upstream
import (
"context"
cmpp "github.com/bigwhite/gocmpp"
"testing"
"time"
"cmpp-platform/gateway/internal/queue"
)
@@ -10,8 +12,8 @@ import (
func TestConnectionPoolAcquiresAcrossConnections(t *testing.T) {
pool := &connectionPool{
conns: []*connection{
{window: make(chan struct{}, 1)},
{window: make(chan struct{}, 1)},
{client: &cmpp.Client{}, window: make(chan struct{}, 64), windowLimit: 1},
{client: &cmpp.Client{}, window: make(chan struct{}, 64), windowLimit: 1},
},
}
@@ -40,6 +42,51 @@ func TestConnectionPoolAcquiresAcrossConnections(t *testing.T) {
releaseSecond()
}
func TestPoolContinuesOnHealthyConnectionWhenPeerIsDraining(t *testing.T) {
draining := &connection{client: &cmpp.Client{}, window: make(chan struct{}, 64), windowLimit: 16, draining: true}
healthy := &connection{client: &cmpp.Client{}, window: make(chan struct{}, 64), windowLimit: 16}
pool := &connectionPool{conns: []*connection{draining, healthy}}
selected, release := pool.tryAcquireConnection()
if selected != healthy {
t.Fatal("expected healthy peer connection")
}
release()
}
func TestRuntimeCapacityMatrixAndSmoothScaleDown(t *testing.T) {
for _, connections := range []int{1, 2, 4, 8} {
for _, window := range []int{1, 16, 32, 64} {
config := normalizeUpstreamConfig(queue.UpstreamConfig{GatewayHost: "127.0.0.1", GatewayPort: 17890, Account: "a", PasswordCipher: "p", CMPPVersion: "3.0", DesiredConnections: connections, WindowSize: window})
if config.DesiredConnections != connections || config.WindowSize != window {
t.Fatalf("matrix normalized incorrectly: %+v", config)
}
}
}
pool := &connectionPool{channelID: "channel-1", connectionID: "primary", config: normalizeUpstreamConfig(queue.UpstreamConfig{DesiredConnections: 2, WindowSize: 16, ConnectionDrainSeconds: 1}), stopCh: make(chan struct{}), reconnectSignal: make(chan struct{}, 1)}
pool.conns = []*connection{{pool: pool, window: make(chan struct{}, 64), windowLimit: 16}, {pool: pool, window: make(chan struct{}, 64), windowLimit: 16}}
pool.reconfigure(queue.UpstreamConfig{DesiredConnections: 1, WindowSize: 32, ConnectionDrainSeconds: 1})
deadline := time.Now().Add(time.Second)
for {
pool.mu.Lock()
count := len(pool.conns)
first := pool.conns[0]
pool.mu.Unlock()
if count == 1 {
first.mu.Lock()
limit := first.windowLimit
first.mu.Unlock()
if limit != 32 {
t.Fatalf("window limit=%d want32", limit)
}
break
}
if time.Now().After(deadline) {
t.Fatal("scale down did not complete")
}
time.Sleep(time.Millisecond)
}
}
func TestManagerDisconnectChannelRemovesPoolAndStopsReconnects(t *testing.T) {
pool := &connectionPool{
channelID: "channel-1",