XnLemon commented on code in PR #3579:
URL: https://github.com/apache/dubbo-go/pull/3579#discussion_r3764729014


##########
protocol/jsonrpc/server.go:
##########
@@ -151,48 +145,139 @@ func (s *Server) handlePkg(conn net.Conn) {
                reqHeader["HttpMethod"] = r.Method
 
                httpTimeout := s.timeout
-               contentType := reqHeader["Content-Type"]
+               contentType := reqHeader[ContentTypeHeader]
                mediaType, _, parseErr := mime.ParseMediaType(contentType)
-               if parseErr != nil || (mediaType != "application/json" && 
mediaType != "application/json-rpc") {
-                       setTimeout(conn, httpTimeout)
-                       errMsg := "unsupported content type: " + contentType
-                       if errRsp := sendErrorResp(r.Header, []byte(errMsg)); 
errRsp != nil {
-                               logger.Warnf("[Jsonrpc][Server] sendErrorResp 
failed, header=%v, err_msg=%v, send_err=%v",
-                                       r.Header, errMsg, errRsp)
-                       }
-                       return
-               }
-
-               ctx := context.Background()
+               unsupportedContentType := parseErr != nil || (mediaType != 
"application/json" && mediaType != "application/json-rpc")
 
-               spanCtx, err := 
opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders,
-                       opentracing.HTTPHeadersCarrier(r.Header))
-               if err == nil {
-                       ctx = context.WithValue(ctx, 
constant.TracingRemoteSpanCtx, spanCtx)
-               }
+               requestCtx, requestCancel := context.WithCancel(connectionCtx)
+               r = r.WithContext(requestCtx)
+               ctx := contextFromRequest(r)
+               var timeoutCancel context.CancelFunc
 
                if len(reqHeader["Timeout"]) > 0 {
                        timeout, err := time.ParseDuration(reqHeader["Timeout"])
                        if err == nil {
                                httpTimeout = timeout
-                               var cancel context.CancelFunc
-                               ctx, cancel = context.WithTimeout(ctx, 
httpTimeout)
-                               defer cancel()
+                               ctx, timeoutCancel = context.WithTimeout(ctx, 
httpTimeout)
                        }
                        delete(reqHeader, "Timeout")
                }
                setTimeout(conn, httpTimeout)
 
-               if err := serveRequest(ctx, reqHeader, reqBody, conn); err != 
nil {
-                       if errRsp := sendErrorResp(r.Header, 
[]byte(perrors.WithStack(err).Error())); errRsp != nil {
-                               logger.Warnf("[Jsonrpc][Server] sendErrorResp 
failed, header=%v, err=%v, send_err=%v",
-                                       r.Header, perrors.WithStack(err), 
errRsp)
+               requestSequence := sequence
+               sequence++
+               requestWG.Add(1)
+               go func(ctx context.Context, requestCancel, timeoutCancel 
context.CancelFunc, header map[string]string, body []byte,

Review Comment:
   Fixed in `fb91f6c9`. JSON-RPC request `Timeout` headers now apply only to 
each request's `context.WithTimeout` and no longer update the shared connection 
deadline, so a short pipelined request cannot cancel an unrelated long-running 
request. Added `TestHandlePkgIsolatesPipelinedRequestTimeouts`, covering a 5s 
blocking request followed by a 50ms request on the same connection while 
preserving response order and disconnect cancellation behavior. Validation: `go 
test ./protocol/jsonrpc -count=1`; regression test repeated 20 times; related 
timeout/disconnect/order tests passed with `-race`; `go vet ./protocol/jsonrpc` 
passed.
   
   <details>
   <summary>中文</summary>
   
   已在 `fb91f6c9` 中修复。JSON-RPC 请求的 `Timeout` 现在只作用于各请求自己的 
`context.WithTimeout`,不再修改共享连接的 deadline,因此短超时的流水线请求不会取消另一个长时间运行的请求。新增了 
`TestHandlePkgIsolatesPipelinedRequestTimeouts`,覆盖同一连接上先发送 5 秒阻塞请求、再发送 50 
毫秒请求的场景,同时验证响应顺序和断连取消行为保持不变。验证结果:`go test ./protocol/jsonrpc -count=1` 
通过;回归测试重复运行 20 次通过;timeout/disconnect/order 相关测试在 `-race` 下通过;`go vet 
./protocol/jsonrpc` 通过。
   
   </details>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to