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

rpc: improve the realtime notify performance by 30% #28328

Merged
merged 9 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
18 changes: 8 additions & 10 deletions rpc/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type Notifier struct {

mu sync.Mutex
sub *Subscription
buffer []json.RawMessage
buffer []interface{}
callReturned bool
activated bool
}
Expand All @@ -130,11 +130,6 @@ func (n *Notifier) CreateSubscription() *Subscription {
// Notify sends a notification to the client with the given data as payload.
// If an error occurs the RPC connection is closed and the error is returned.
func (n *Notifier) Notify(id ID, data interface{}) error {
enc, err := json.Marshal(data)
if err != nil {
return err
}

n.mu.Lock()
defer n.mu.Unlock()

Expand All @@ -144,9 +139,9 @@ func (n *Notifier) Notify(id ID, data interface{}) error {
panic("Notify with wrong ID")
}
if n.activated {
return n.send(n.sub, enc)
return n.send(n.sub, data)
}
n.buffer = append(n.buffer, enc)
n.buffer = append(n.buffer, data)
return nil
}

Expand Down Expand Up @@ -181,8 +176,11 @@ func (n *Notifier) activate() error {
return nil
}

func (n *Notifier) send(sub *Subscription, data json.RawMessage) error {
params, _ := json.Marshal(&subscriptionResult{ID: string(sub.ID), Result: data})
func (n *Notifier) send(sub *Subscription, data interface{}) error {
params, _ := json.Marshal(struct {
ID string `json:"subscription"`
Result interface{} `json:"result,omitempty"`
}{ID: string(sub.ID), Result: data})
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.Background()

msg := &jsonrpcMessage{
Expand Down
35 changes: 35 additions & 0 deletions rpc/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
package rpc

import (
"context"
"encoding/json"
"fmt"
"math/big"
"net"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

func TestNewID(t *testing.T) {
Expand Down Expand Up @@ -218,3 +223,33 @@ func readAndValidateMessage(in *json.Decoder) (*subConfirmation, *subscriptionRe
return nil, nil, fmt.Errorf("unrecognized message: %v", msg)
}
}

type mockConn struct{}

// writeJSON writes a message to the connection.
func (c *mockConn) writeJSON(ctx context.Context, msg interface{}, isError bool) error { return nil }

// Closed returns a channel which is closed when the connection is closed.
func (c *mockConn) closed() <-chan interface{} { return nil }

// RemoteAddr returns the peer address of the connection.
func (c *mockConn) remoteAddr() string { return "" }

// BenchmarkNotify benchmarks the performance of notifying a subscription.
func BenchmarkNotify(b *testing.B) {
id := ID("test")
notifier := &Notifier{
h: &handler{conn: &mockConn{}},
sub: &Subscription{ID: id},
activated: true,
}

msg := &types.Header{
ParentHash: common.HexToHash("0x01"),
Number: big.NewInt(100),
}

for i := 0; i < b.N; i++ {
notifier.Notify(id, msg)
}
}