Skip to content

Commit 3624b0a

Browse files
committed
OPTIM/MINOR: Replace stdlib json calls with jsoniter for performance
1 parent 2e20f7f commit 3624b0a

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

configuration/cluster_sync.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"crypto/x509"
2525
"crypto/x509/pkix"
2626
"encoding/asn1"
27-
"encoding/json"
2827
"encoding/pem"
2928
"fmt"
3029
"io/ioutil"
@@ -39,6 +38,7 @@ import (
3938
client_native "github.com/haproxytech/client-native/v3"
4039
"github.com/haproxytech/config-parser/v4/types"
4140
"github.com/haproxytech/dataplaneapi/log"
41+
jsoniter "github.com/json-iterator/go"
4242

4343
"github.com/haproxytech/dataplaneapi/haproxy"
4444
"github.com/haproxytech/dataplaneapi/misc"
@@ -144,6 +144,7 @@ func (c *ClusterSync) issueRefreshRequest(url, port, basePath string, nodesPath
144144
Status: cfg.Status.Load(),
145145
Type: DataplaneAPIType,
146146
}
147+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
147148
bytesRepresentation, _ := json.Marshal(nodeData)
148149

149150
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(bytesRepresentation))
@@ -343,6 +344,7 @@ func (c *ClusterSync) issueJoinRequest(url, port, basePath string, registerPath
343344
}
344345
nodeData.Facts = c.getNodeFacts()
345346

347+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
346348
bytesRepresentation, _ := json.Marshal(nodeData)
347349

348350
req, err := http.NewRequest(registerMethod, url, bytes.NewBuffer(bytesRepresentation))
@@ -518,6 +520,7 @@ func (c *ClusterSync) fetchCert() {
518520
break
519521
}
520522
var responseData Node
523+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
521524
err = json.Unmarshal(body, &responseData)
522525
if err != nil {
523526
c.activateFetchCert(err)

configuration/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
petname "github.com/dustinkirkland/golang-petname"
2929
"github.com/haproxytech/client-native/v3/models"
3030
"github.com/haproxytech/dataplaneapi/log"
31+
jsoniter "github.com/json-iterator/go"
3132
)
3233

3334
var cfg *Configuration
@@ -247,6 +248,7 @@ func (c *Configuration) Load() error {
247248

248249
func (c *Configuration) LoadRuntimeVars(swaggerJSON json.RawMessage, host string, port int) error {
249250
var m map[string]interface{}
251+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
250252
err := json.Unmarshal(swaggerJSON, &m)
251253
if err != nil {
252254
return err

configuration/misc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package configuration
1717

1818
import (
1919
"encoding/base64"
20-
"encoding/json"
2120
"errors"
2221
"fmt"
2322
"os"
@@ -26,6 +25,7 @@ import (
2625

2726
"github.com/haproxytech/client-native/v3/misc"
2827
"github.com/haproxytech/client-native/v3/storage"
28+
jsoniter "github.com/json-iterator/go"
2929
)
3030

3131
func DecodeBootstrapKey(key string) (map[string]string, error) {
@@ -34,6 +34,7 @@ func DecodeBootstrapKey(key string) (map[string]string, error) {
3434
return nil, fmt.Errorf("%s - %w", key, err)
3535
}
3636
var decodedKey map[string]string
37+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
3738
err = json.Unmarshal(raw, &decodedKey)
3839
if err != nil {
3940
return nil, fmt.Errorf("%s - %w", key, err)

configure_data_plane.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ package dataplaneapi
2020
import (
2121
"context"
2222
"crypto/tls"
23-
"encoding/json"
2423
"fmt"
24+
"io"
2525
"net/http"
2626
"os"
2727
"os/signal"
@@ -46,6 +46,7 @@ import (
4646
parser "github.com/haproxytech/config-parser/v4"
4747
"github.com/haproxytech/config-parser/v4/types"
4848
"github.com/haproxytech/dataplaneapi/log"
49+
jsoniter "github.com/json-iterator/go"
4950
"github.com/rs/cors"
5051

5152
"github.com/haproxytech/dataplaneapi/adapters"
@@ -174,11 +175,21 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
174175
// Example:
175176
api.Logger = log.Printf
176177

177-
api.JSONConsumer = runtime.JSONConsumer()
178+
api.JSONConsumer = runtime.ConsumerFunc(func(reader io.Reader, data interface{}) error {
179+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
180+
dec := json.NewDecoder(reader)
181+
dec.UseNumber() // preserve number formats
182+
return dec.Decode(data)
183+
})
178184

179185
api.TxtConsumer = runtime.TextConsumer()
180186

181-
api.JSONProducer = runtime.JSONProducer()
187+
api.JSONProducer = runtime.ProducerFunc(func(writer io.Writer, data interface{}) error {
188+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
189+
enc := json.NewEncoder(writer)
190+
enc.SetEscapeHTML(false)
191+
return enc.Encode(data)
192+
})
182193

183194
api.ServerShutdown = serverShutdown
184195

@@ -561,6 +572,7 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
561572
// setup specification handler
562573
api.SpecificationGetSpecificationHandler = specification.GetSpecificationHandlerFunc(func(params specification.GetSpecificationParams, principal interface{}) middleware.Responder {
563574
var m map[string]interface{}
575+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
564576
if err := json.Unmarshal(SwaggerJSON, &m); err != nil {
565577
e := misc.HandleError(err)
566578
return specification.NewGetSpecificationDefault(int(*e.Code)).WithPayload(e)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/hashicorp/consul/api v1.6.0
2828
github.com/hashicorp/hcl v1.0.0
2929
github.com/jessevdk/go-flags v1.4.0
30+
github.com/json-iterator/go v1.1.12
3031
github.com/lestrrat-go/apache-logformat v0.0.0-20210106032603-24d066f940f8
3132
github.com/nathanaelle/syslog5424/v2 v2.0.5
3233
github.com/rodaine/hclencoder v0.0.0-20200910194838-aaa140ee61ed
@@ -78,6 +79,8 @@ require (
7879
github.com/mattn/go-isatty v0.0.12 // indirect
7980
github.com/mitchellh/go-homedir v1.1.0 // indirect
8081
github.com/mitchellh/mapstructure v1.2.2 // indirect
82+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
83+
github.com/modern-go/reflect2 v1.0.2 // indirect
8184
github.com/pkg/errors v0.9.1 // indirect
8285
github.com/pmezard/go-difflib v1.0.0 // indirect
8386
github.com/tebeka/strftime v0.1.5 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
166166
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
167167
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
168168
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
169+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
169170
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
170171
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
171172
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -223,6 +224,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW
223224
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
224225
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
225226
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
227+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
228+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
226229
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
227230
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
228231
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@@ -275,6 +278,10 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
275278
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
276279
github.com/mitchellh/mapstructure v1.2.2 h1:dxe5oCinTXiTIcfgmZecdCzPmAJKd46KsCWc35r0TV4=
277280
github.com/mitchellh/mapstructure v1.2.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
281+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
282+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
283+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
284+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
278285
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
279286
github.com/nathanaelle/syslog5424/v2 v2.0.5 h1:oLVYQmKnv3nlnmvlbBWM+iWl9AONcU+tuxIL0fjko5A=
280287
github.com/nathanaelle/syslog5424/v2 v2.0.5/go.mod h1:f9MvsGkLOtYh2VzLT4Pjuwx3+Xv2EogxJNda+HLPDus=

misc/misc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/haproxytech/client-native/v3/configuration"
3030
client_errors "github.com/haproxytech/client-native/v3/errors"
3131
"github.com/haproxytech/client-native/v3/models"
32+
jsoniter "github.com/json-iterator/go"
3233

3334
"github.com/haproxytech/dataplaneapi/haproxy"
3435
"github.com/haproxytech/dataplaneapi/rate"
@@ -113,6 +114,7 @@ func HandleContainerGetError(err error) *models.Error {
113114
// DiscoverChildPaths return children models.Endpoints given path
114115
func DiscoverChildPaths(path string, spec json.RawMessage) (models.Endpoints, error) {
115116
var m map[string]interface{}
117+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
116118
err := json.Unmarshal(spec, &m)
117119
if err != nil {
118120
return nil, err

0 commit comments

Comments
 (0)