Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No json rpc streaming #2779

Merged
merged 29 commits into from
Oct 5, 2021
Merged
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
no stream of single request
  • Loading branch information
AskAlexSharov committed Oct 5, 2021
commit d04b9d24e28e89c035a1b7de408ddc9bcc126502
22 changes: 15 additions & 7 deletions rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package rpc

import (
"bytes"
"context"
"encoding/json"
"reflect"
Expand Down Expand Up @@ -123,6 +122,16 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
// Process calls on a goroutine because they may block indefinitely:
h.startCallProc(func(cp *callProc) {
// All goroutines will place results right to this array. Because requests order must match reply orders.
streams := make([]*jsoniter.Stream, len(msgs))
for i := range streams {
streams[i] = jsoniter.ConfigDefault.BorrowStream(nil)
}
defer func() {
for i := range streams {
jsoniter.ConfigDefault.ReturnStream(streams[i])
}
}()

answersWithNils := make([]interface{}, len(msgs))
// Bounded parallelism pattern explanation https://blog.golang.org/pipelines#TOC_9.
boundedConcurrency := make(chan struct{}, h.maxBatchConcurrency)
Expand All @@ -143,14 +152,13 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
default:
}

buf := bytes.NewBuffer(nil)
stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
stream := streams[i]
if res := h.handleCallMsg(cp, calls[i], stream); res != nil {
answersWithNils[i] = res
}
_ = stream.Flush()
if buf.Len() > 0 && answersWithNils[i] == nil {
answersWithNils[i] = json.RawMessage(buf.Bytes())
if len(stream.Buffer()) > 0 && answersWithNils[i] == nil {
answersWithNils[i] = json.RawMessage(stream.Buffer())
}
}(i)
}
Expand All @@ -177,8 +185,8 @@ func (h *handler) handleMsg(msg *jsonrpcMessage) {
return
}
h.startCallProc(func(cp *callProc) {
jsoniter.ConfigDefault.BorrowStream(nil)
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 4096)
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
answer := h.handleCallMsg(cp, msg, stream)
h.addSubscriptions(cp.notifiers)
if answer != nil {
Expand Down