Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(jsonrpc): bound pipelined request window
  • Loading branch information
XnLemon committed Aug 14, 2026
commit 19d0150861ce1b9823611bd394df5ef2cee78097
18 changes: 16 additions & 2 deletions protocol/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
MaxHeaderSize = 8 * 1024 * 1024
// ContentTypeHeader is the HTTP Content-Type header name.
ContentTypeHeader = "Content-Type"
// maxRequestWindowPerConnection bounds requests whose responses have not been written yet.
maxRequestWindowPerConnection = 64
)

// Server is JSON RPC server wrapper
Expand All @@ -83,10 +85,11 @@
func (s *Server) handlePkg(conn net.Conn) {
connectionCtx, connectionCancel := context.WithCancel(context.Background())
responses := make(chan orderedResponse)
requestWindow := make(chan struct{}, maxRequestWindowPerConnection)
responseWriterDone := make(chan struct{})
go func() {
defer close(responseWriterDone)
writeResponsesInOrder(connectionCtx, connectionCancel, conn, responses)
writeResponsesInOrder(connectionCtx, connectionCancel, conn, responses, requestWindow)
}()
var requestWG sync.WaitGroup
defer func() {
Expand All @@ -106,19 +109,28 @@
bufReader := bufio.NewReader(limitedReader)
var sequence uint64
for {
select {
case requestWindow <- struct{}{}:
case <-connectionCtx.Done():
return
}

limitedReader.N = int64(MaxHeaderSize - bufReader.Buffered())
if _, err := bufReader.Peek(1); errors.Is(err, io.EOF) {
<-requestWindow
return
}
r, err := http.ReadRequest(bufReader)
if err != nil {
<-requestWindow
logger.Warnf("[Jsonrpc][Server] read request failed, err=%v", err)
return
}

reqBody, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
<-requestWindow
return
}

Expand Down Expand Up @@ -152,21 +164,21 @@
requestSequence := sequence
sequence++
requestWG.Add(1)
go func(ctx context.Context, requestCancel, timeoutCancel context.CancelFunc, header map[string]string, body []byte,
Comment thread
XnLemon marked this conversation as resolved.
Resolved
Comment thread
XnLemon marked this conversation as resolved.
Resolved
responseHeader http.Header, contentType string, unsupportedContentType bool, requestSequence uint64) {
defer requestWG.Done()
defer requestCancel()
if timeoutCancel != nil {
defer timeoutCancel()
}

response := buildOrderedResponse(ctx, header, body, responseHeader, contentType,
unsupportedContentType, requestSequence)
select {
case responses <- response:
case <-connectionCtx.Done():
}
}(ctx, requestCancel, timeoutCancel, reqHeader, reqBody, r.Header, contentType, unsupportedContentType, requestSequence)

Check warning on line 181 in protocol/jsonrpc/server.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This function has 9 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ_0X3ucnthQh2MXPQqQ&open=AZ_0X3ucnthQh2MXPQqQ&pullRequest=3579
}
}

Expand Down Expand Up @@ -222,7 +234,8 @@
return perrors.WithStack(err)
}

func writeResponsesInOrder(ctx context.Context, cancel context.CancelFunc, conn net.Conn, responses <-chan orderedResponse) {
func writeResponsesInOrder(ctx context.Context, cancel context.CancelFunc, conn net.Conn, responses <-chan orderedResponse,
requestWindow <-chan struct{}) {
nextSequence := uint64(0)
pending := make(map[uint64]orderedResponse)
for {
Expand Down Expand Up @@ -251,6 +264,7 @@
return
}
nextSequence++
<-requestWindow
}
}
}
Expand Down Expand Up @@ -340,7 +354,7 @@
})
}

func serveRequest(ctx context.Context, header map[string]string, body []byte, writer io.Writer) error {

Check failure on line 357 in protocol/jsonrpc/server.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ_0X3ucnthQh2MXPQqR&open=AZ_0X3ucnthQh2MXPQqR&pullRequest=3579
Comment thread
XnLemon marked this conversation as resolved.
Resolved
sendErrorResp := func(header map[string]string, body []byte) error {
rsp := &http.Response{
Header: make(http.Header),
Expand Down
123 changes: 123 additions & 0 deletions protocol/jsonrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,129 @@ func TestHandlePkgPreservesPipelinedResponseOrder(t *testing.T) {
require.Equal(t, "fast", second.result)
}

type boundedWindowInvoker struct {
base.BaseInvoker
started chan struct{}
encoded chan struct{}
releaseFirst chan struct{}
}

type boundedWindowResult struct {
encoded chan struct{}
}

func (r boundedWindowResult) MarshalJSON() ([]byte, error) {
r.encoded <- struct{}{}
return json.Marshal("done")
}

func (i *boundedWindowInvoker) Invoke(_ context.Context, invocation base.Invocation) result.Result {
i.started <- struct{}{}
if invocation.MethodName() == "Blocked" {
<-i.releaseFirst
}
return &result.RPCResult{Rest: boundedWindowResult{encoded: i.encoded}}
}

func TestHandlePkgBoundsPipelinedRequestWindow(t *testing.T) {
const (
servicePath = "bounded-request-window-test"
requestCount = maxRequestWindowPerConnection + 1
)
protocol := GetProtocol().(*JsonrpcProtocol)
invoker := &boundedWindowInvoker{
BaseInvoker: *base.NewBaseInvoker(common.NewURLWithOptions(common.WithProtocol(JSONRPC))),
started: make(chan struct{}, requestCount),
encoded: make(chan struct{}, requestCount),
releaseFirst: make(chan struct{}),
}
protocol.SetExporterMap(servicePath, NewJsonrpcExporter(servicePath, invoker, protocol.ExporterMap()))
t.Cleanup(func() { protocol.ExporterMap().Delete(servicePath) })

serverConn, clientConn := net.Pipe()
handleDone := make(chan struct{})
go func() {
NewServer().handlePkg(serverConn)
close(handleDone)
}()

var releaseOnce sync.Once
releaseFirst := func() { releaseOnce.Do(func() { close(invoker.releaseFirst) }) }
t.Cleanup(func() {
releaseFirst()
_ = clientConn.Close()
select {
case <-handleDone:
case <-time.After(time.Second):
t.Error("connection handler did not exit")
}
})
require.NoError(t, clientConn.SetDeadline(time.Now().Add(5*time.Second)))

makeRequest := func(method string, id int) string {
body := fmt.Sprintf(`{"jsonrpc":"2.0","method":%q,"params":[],"id":%d}`, method, id)
return "POST /" + servicePath + " HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + fmt.Sprint(len(body)) + "\r\n\r\n" + body
}

requests := makeRequest("Blocked", 1)
for id := 2; id <= requestCount; id++ {
requests += makeRequest("Fast", id)
}
writeDone := make(chan error, 1)
go func() {
_, err := clientConn.Write([]byte(requests))
writeDone <- err
}()

waitForSignals := func(signals <-chan struct{}, count int, description string) {
for range count {
select {
case <-signals:
case <-time.After(2 * time.Second):
t.Fatalf("timed out waiting for %s %d", description, count)
}
}
}
waitForSignals(invoker.started, maxRequestWindowPerConnection, "started invocations")
waitForSignals(invoker.encoded, maxRequestWindowPerConnection-1, "encoded responses")
select {
case <-invoker.started:
t.Fatalf("more than %d invocations started before the response window advanced", maxRequestWindowPerConnection)
case <-time.After(100 * time.Millisecond):
}

readDone := make(chan error, 1)
go func() {
reader := bufio.NewReader(clientConn)
for range requestCount {
response, err := http.ReadResponse(reader, nil)
if err != nil {
readDone <- err
return
}
_, err = io.Copy(io.Discard, response.Body)
closeErr := response.Body.Close()
if err != nil {
readDone <- err
return
}
if closeErr != nil {
readDone <- closeErr
return
}
}
readDone <- nil
}()

releaseFirst()
waitForSignals(invoker.started, 1, "invocations after advancing the response window")
require.NoError(t, <-writeDone)
require.NoError(t, <-readDone)
}

type requestTimeoutInvoker struct {
base.BaseInvoker
longStarted chan struct{}
Expand Down
Loading