Skip to content

Commit c86f178

Browse files
committed
update metrics feature
1 parent 6f3fc30 commit c86f178

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

api-service/controller/mcp_proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewMCPGateway(router *gin.RouterGroup) *MCPGateway {
6161
gateway := &MCPGateway{
6262
router: router,
6363
transport: &http.Transport{
64-
MaxIdleConns: 100,
64+
MaxIdleConns: 2000,
6565
MaxIdleConnsPerHost: 10,
6666
IdleConnTimeout: 90 * time.Second,
6767
},

api-service/middleware/metrics.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,33 @@ func MCPMetricsMiddleware() gin.HandlerFunc {
9090

9191
var rpcMethod string
9292
if c.Request.Method == "POST" && c.Request.Body != nil {
93-
// LimitReader: only read up to 8KB for JSON-RPC header extraction
94-
limited := io.LimitReader(c.Request.Body, 8192)
93+
// Read up to 8KB+1 to detect oversized bodies; JSON-RPC method
94+
// field is always in the first few dozen bytes.
95+
const maxPeek = 8192
96+
limited := io.LimitReader(c.Request.Body, maxPeek+1)
9597
if body, err := io.ReadAll(limited); err == nil && len(body) > 0 {
9698
var rpc struct {
9799
Method string `json:"method"`
98100
}
99101
if json.Unmarshal(body, &rpc) == nil {
100102
rpcMethod = rpc.Method
101103
}
102-
// Rebuild full body: read portion + any unread remainder
103-
remaining, _ := io.ReadAll(c.Request.Body)
104-
fullBody := append(body, remaining...)
104+
105+
// Fast path (99%+ requests): body fits within limit, skip
106+
// second ReadAll and append entirely.
107+
var fullBody []byte
108+
if len(body) <= maxPeek {
109+
fullBody = body
110+
} else {
111+
remaining, _ := io.ReadAll(c.Request.Body)
112+
fullBody = append(body, remaining...)
113+
}
114+
105115
c.Request.Body = io.NopCloser(bytes.NewReader(fullBody))
106116
c.Request.GetBody = func() (io.ReadCloser, error) {
107117
return io.NopCloser(bytes.NewReader(fullBody)), nil
108118
}
109119
c.Request.ContentLength = int64(len(fullBody))
110-
// Store body in context for LoggingMiddleware reuse
111120
c.Set("_req_body", fullBody)
112121
}
113122
}

go.work.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZY
301301
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
302302
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
303303
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
304+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
304305
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
305306
github.com/lyft/protoc-gen-star/v2 v2.0.3/go.mod h1:amey7yeodaJhXSbf/TlLvWiqQfLOSpEk//mLlc+axEk=
306307
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
@@ -318,9 +319,11 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h
318319
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
319320
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
320321
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
322+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
321323
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
322324
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
323325
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
326+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
324327
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
325328
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
326329
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=

0 commit comments

Comments
 (0)