Skip to content

Commit

Permalink
Introduce json-iterator in dispatcher for faster work with json
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Nov 7, 2023
1 parent 44f7d88 commit 1fa1bdc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ updates:
directory: /
target-branch: "develop"
schedule:
interval: weekly
interval: monthly
ignore:
- dependency-name: "github.com/aws/aws-sdk-go"
update-types: [ "version-update:semver-patch" ]
open-pull-requests-limit: 10
open-pull-requests-limit: 20
pull-request-branch-name:
separator: "-"
reviewers:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
)

require (
github.com/json-iterator/go v1.1.12
github.com/quasilyte/go-ruleguard v0.4.0
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/sethvargo/go-retry v0.2.4
Expand All @@ -87,6 +88,8 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect
github.com/ipfs/boxo v0.8.1 // indirect
github.com/libp2p/go-yamux/v4 v4.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/outcaste-io/ristretto v0.2.3 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlT
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
Expand Down Expand Up @@ -481,6 +482,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
Expand Down
25 changes: 15 additions & 10 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jsonrpc

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math"
Expand All @@ -14,6 +13,12 @@ import (

"github.com/armon/go-metrics"
"github.com/hashicorp/go-hclog"
jsonIter "github.com/json-iterator/go"
)

var (
jsonIt = jsonIter.ConfigCompatibleWithStandardLibrary
fastJsonIt = jsonIter.ConfigFastest

Check failure on line 21 in jsonrpc/dispatcher.go

View workflow job for this annotation

GitHub Actions / golangci_lint

ST1003: var fastJsonIt should be fastJSONIt (stylecheck)

Check failure on line 21 in jsonrpc/dispatcher.go

View workflow job for this annotation

GitHub Actions / golangci_lint

ST1003: var fastJsonIt should be fastJSONIt (stylecheck)
)

type serviceData struct {
Expand Down Expand Up @@ -186,7 +191,7 @@ func formatID(id interface{}) (interface{}, Error) {

func (d *Dispatcher) handleSubscribe(req Request, conn wsConn) (string, Error) {
var params []interface{}
if err := json.Unmarshal(req.Params, &params); err != nil {
if err := jsonIt.Unmarshal(req.Params, &params); err != nil {
return "", NewInvalidRequestError("Invalid json request")
}

Expand Down Expand Up @@ -219,7 +224,7 @@ func (d *Dispatcher) handleSubscribe(req Request, conn wsConn) (string, Error) {

func (d *Dispatcher) handleUnsubscribe(req Request) (bool, Error) {
var params []interface{}
if err := json.Unmarshal(req.Params, &params); err != nil {
if err := jsonIt.Unmarshal(req.Params, &params); err != nil {
return false, NewInvalidRequestError("Invalid json request")
}

Expand Down Expand Up @@ -252,7 +257,7 @@ func (d *Dispatcher) HandleWs(reqBody []byte, conn wsConn) ([]byte, error) {
if len(reqBody) > 0 && reqBody[0] == openSquareBracket {
var batchReq BatchRequest

err := json.Unmarshal(reqBody, &batchReq)
err := jsonIt.Unmarshal(reqBody, &batchReq)
if err != nil {
return NewRPCResponse(nil, "2.0", nil,
NewInvalidRequestError("Invalid json batch request")).Bytes()
Expand Down Expand Up @@ -289,7 +294,7 @@ func (d *Dispatcher) HandleWs(reqBody []byte, conn wsConn) ([]byte, error) {
}

var req Request
if err := json.Unmarshal(reqBody, &req); err != nil {
if err := jsonIt.Unmarshal(reqBody, &req); err != nil {
return NewRPCResponse(req.ID, "2.0", nil, NewInvalidRequestError("Invalid json request")).Bytes()
}

Expand Down Expand Up @@ -334,7 +339,7 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) {

if x[0] == '{' {
var req Request
if err := json.Unmarshal(reqBody, &req); err != nil {
if err := jsonIt.Unmarshal(reqBody, &req); err != nil {
return NewRPCResponse(nil, "2.0", nil, NewInvalidRequestError("Invalid json request")).Bytes()
}

Expand All @@ -349,7 +354,7 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) {

// handle batch requests
var requests BatchRequest
if err := json.Unmarshal(reqBody, &requests); err != nil {
if err := jsonIt.Unmarshal(reqBody, &requests); err != nil {
return NewRPCResponse(
nil,
"2.0",
Expand Down Expand Up @@ -383,7 +388,7 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) {
responses = append(responses, resp)
}

respBytes, err := json.Marshal(responses)
respBytes, err := jsonIt.Marshal(responses)
if err != nil {
return NewRPCResponse(nil, "2.0", nil, NewInternalError("Internal error")).Bytes()
}
Expand Down Expand Up @@ -411,7 +416,7 @@ func (d *Dispatcher) handleReq(req Request) ([]byte, Error) {
}

if fd.numParams() > 0 {
if err := json.Unmarshal(req.Params, &inputs); err != nil {
if err := jsonIt.Unmarshal(req.Params, &inputs); err != nil {
return nil, NewInvalidParamsError("Invalid Params")
}
}
Expand Down Expand Up @@ -444,7 +449,7 @@ func (d *Dispatcher) handleReq(req Request) ([]byte, Error) {
}

if res := output[0].Interface(); res != nil {
data, err = json.Marshal(res)
data, err = fastJsonIt.Marshal(res)
if err != nil {
d.logInternalError(req.Method, err)

Expand Down

0 comments on commit 1fa1bdc

Please sign in to comment.