perf(cmpp): reserve transport for inbound submit

This commit is contained in:
hectorzhao
2026-08-20 18:23:01 +08:00
parent 6708f1f7c5
commit 229a0b28fd
10 changed files with 61 additions and 9 deletions
@@ -1,7 +1,10 @@
package inbound
import (
"io"
"net"
"net/http"
"strings"
"testing"
)
@@ -26,3 +29,32 @@ func TestNewAPIHTTPClientUsesSafeDefault(t *testing.T) {
t.Fatalf("expected default max connections 64, got %d", transport.MaxConnsPerHost)
}
}
func TestSubmitUsesDedicatedHTTPClient(t *testing.T) {
background := &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
t.Fatal("background client must not carry inbound Submit")
return nil, nil
})}
submit := &http.Client{Transport: roundTripFunc(func(request *http.Request) (*http.Response, error) {
if request.URL.Path != "/api/gateway/events/inbound/submit" {
t.Fatalf("unexpected submit path %s", request.URL.Path)
}
return &http.Response{
StatusCode: http.StatusCreated,
Status: "201 Created",
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(`{"accepted":true,"messageId":"MSG-1"}`)),
}, nil
})}
server := Server{APIBaseURL: "http://api.test/api", HTTPClient: background, SubmitHTTPClient: submit}
result, err := server.submit(&net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 12000}, submitRequest{Account: "test"})
if err != nil || !result.Accepted {
t.Fatalf("expected dedicated submit success, result=%+v err=%v", result, err)
}
}
type roundTripFunc func(*http.Request) (*http.Response, error)
func (fn roundTripFunc) RoundTrip(request *http.Request) (*http.Response, error) {
return fn(request)
}
+1
View File
@@ -15,6 +15,7 @@ type Server struct {
APIBaseURL string
SecurityEventToken string
HTTPClient *http.Client
SubmitHTTPClient *http.Client
LogWriter io.Writer
PendingFlushInterval time.Duration
PresenceStore PresenceStore
+7 -1
View File
@@ -290,7 +290,13 @@ func setInboundSubmitResponse(packet any, messageID uint64, result uint32) {
func (s Server) submit(remote net.Addr, payload submitRequest) (submitResponse, error) {
payload.RemoteIP = remoteIP(remote)
var result submitResponse
err := s.post(context.Background(), "/gateway/events/inbound/submit", payload, &result)
client := s.SubmitHTTPClient
if client == nil {
client = s.HTTPClient
}
// Submit has a dedicated transport so protocol logs, receipt recovery and
// presence traffic cannot occupy the connections needed for SubmitResp.
err := s.postWithClient(context.Background(), client, "/gateway/events/inbound/submit", payload, &result)
return result, err
}
+4 -1
View File
@@ -15,7 +15,10 @@ import (
const maxAPIResponseBodyBytes int64 = 4 * 1024 * 1024
func (s Server) post(ctx context.Context, path string, payload any, result any) error {
client := s.HTTPClient
return s.postWithClient(ctx, s.HTTPClient, path, payload, result)
}
func (s Server) postWithClient(ctx context.Context, client *http.Client, path string, payload any, result any) error {
if client == nil {
client = &http.Client{Timeout: defaultHTTPTimeout}
}