Skip to content

Commit 5828a0a

Browse files
dkorunicmjuraga
authored andcommitted
OPTIM/MINOR: Replace stdlib json calls with jsoniter for performance
1 parent 304c3ca commit 5828a0a

File tree

7 files changed

+88
-16
lines changed

7 files changed

+88
-16
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/v2"
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))
@@ -519,6 +521,7 @@ func (c *ClusterSync) fetchCert() {
519521
break
520522
}
521523
var responseData Node
524+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
522525
err = json.Unmarshal(body, &responseData)
523526
if err != nil {
524527
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/v2/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/v2/misc"
2827
"github.com/haproxytech/client-native/v2/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+
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"
@@ -173,11 +174,21 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
173174
// Example:
174175
api.Logger = log.Printf
175176

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

178184
api.TxtConsumer = runtime.TextConsumer()
179185

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

182193
api.ServerShutdown = serverShutdown
183194

@@ -531,6 +542,7 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
531542
// setup specification handler
532543
api.SpecificationGetSpecificationHandler = specification.GetSpecificationHandlerFunc(func(params specification.GetSpecificationParams, principal interface{}) middleware.Responder {
533544
var m map[string]interface{}
545+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
534546
if err := json.Unmarshal(SwaggerJSON, &m); err != nil {
535547
e := misc.HandleError(err)
536548
return specification.NewGetSpecificationDefault(int(*e.Code)).WithPayload(e)

go.mod

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
module github.com/haproxytech/dataplaneapi
22

3-
go 1.16
3+
go 1.17
44

55
require (
66
github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962
7-
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
87
github.com/aws/aws-sdk-go-v2 v1.3.1
98
github.com/aws/aws-sdk-go-v2/config v1.1.3
109
github.com/aws/aws-sdk-go-v2/credentials v1.1.3
1110
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.2.1
1211
github.com/aws/aws-sdk-go-v2/service/ec2 v1.2.0
1312
github.com/docker/go-units v0.4.0
1413
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0
15-
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
1614
github.com/fsnotify/fsnotify v1.4.9
1715
github.com/getkin/kin-openapi v0.17.0
18-
github.com/go-ole/go-ole v1.2.4 // indirect
1916
github.com/go-openapi/errors v0.19.4
2017
github.com/go-openapi/loads v0.19.5
2118
github.com/go-openapi/runtime v0.19.15
@@ -25,23 +22,71 @@ require (
2522
github.com/go-openapi/validate v0.19.8
2623
github.com/google/renameio v1.0.1
2724
github.com/google/uuid v1.2.0
28-
github.com/haproxytech/client-native/v2 v2.5.3
25+
github.com/haproxytech/client-native/v2 v2.5.4-0.20220218084053-8755a69eb733
2926
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220107093215-b580bd3a8a90
3027
github.com/hashicorp/consul/api v1.6.0
3128
github.com/hashicorp/hcl v1.0.0
32-
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
3329
github.com/jessevdk/go-flags v1.4.0
30+
github.com/json-iterator/go v1.1.12
3431
github.com/lestrrat-go/apache-logformat v0.0.0-20210106032603-24d066f940f8
35-
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc // indirect
3632
github.com/nathanaelle/syslog5424/v2 v2.0.5
3733
github.com/rodaine/hclencoder v0.0.0-20200910194838-aaa140ee61ed
3834
github.com/rs/cors v1.7.0
3935
github.com/shirou/gopsutil v3.21.4+incompatible
4036
github.com/sirupsen/logrus v1.8.1
4137
github.com/stretchr/testify v1.6.1
42-
github.com/tebeka/strftime v0.1.5 // indirect
43-
github.com/tklauser/go-sysconf v0.3.6 // indirect
4438
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
4539
golang.org/x/sys v0.0.0-20211102192858-4dd72447c267
4640
gopkg.in/yaml.v2 v2.3.0
4741
)
42+
43+
require (
44+
github.com/PuerkitoBio/purell v1.1.1 // indirect
45+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
46+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
47+
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
48+
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
49+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.0.4 // indirect
50+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.0.4 // indirect
51+
github.com/aws/aws-sdk-go-v2/service/sso v1.1.3 // indirect
52+
github.com/aws/aws-sdk-go-v2/service/sts v1.2.0 // indirect
53+
github.com/aws/smithy-go v1.3.0 // indirect
54+
github.com/davecgh/go-spew v1.1.1 // indirect
55+
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
56+
github.com/fatih/color v1.9.0 // indirect
57+
github.com/ghodss/yaml v1.0.0 // indirect
58+
github.com/go-ole/go-ole v1.2.4 // indirect
59+
github.com/go-openapi/analysis v0.19.10 // indirect
60+
github.com/go-openapi/jsonpointer v0.19.3 // indirect
61+
github.com/go-openapi/jsonreference v0.19.3 // indirect
62+
github.com/go-stack/stack v1.8.0 // indirect
63+
github.com/gofrs/flock v0.8.1 // indirect
64+
github.com/google/go-cmp v0.5.4 // indirect
65+
github.com/haproxytech/go-logger v1.0.1-0.20211022075555-178f1cdf4d84 // indirect
66+
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
67+
github.com/hashicorp/go-hclog v0.12.0 // indirect
68+
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
69+
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
70+
github.com/hashicorp/golang-lru v0.5.0 // indirect
71+
github.com/hashicorp/serf v0.9.3 // indirect
72+
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
73+
github.com/jmespath/go-jmespath v0.4.0 // indirect
74+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
75+
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc // indirect
76+
github.com/lestrrat-go/strftime v0.0.0-20180821113735-8b31f9c59b0f // indirect
77+
github.com/mailru/easyjson v0.7.1 // indirect
78+
github.com/mattn/go-colorable v0.1.6 // indirect
79+
github.com/mattn/go-isatty v0.0.12 // indirect
80+
github.com/mitchellh/go-homedir v1.1.0 // indirect
81+
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
84+
github.com/pkg/errors v0.9.1 // indirect
85+
github.com/pmezard/go-difflib v1.0.0 // indirect
86+
github.com/tebeka/strftime v0.1.5 // indirect
87+
github.com/tklauser/go-sysconf v0.3.6 // indirect
88+
github.com/tklauser/numcpus v0.2.2 // indirect
89+
go.mongodb.org/mongo-driver v1.5.1 // indirect
90+
golang.org/x/text v0.3.5 // indirect
91+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
92+
)

go.sum

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
170170
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
171171
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
172172
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
173+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
173174
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
174175
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
175176
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
176177
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
177178
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
178179
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
179-
github.com/haproxytech/client-native/v2 v2.5.3 h1:PnDGTlJ2phzfCjzy8IcQafwJMO5FRHIotVAdQfCORK4=
180-
github.com/haproxytech/client-native/v2 v2.5.3/go.mod h1:S7CBbMb7hP8nxiL+PE2edFVMOMO7Uvptvr7hAQw5IJk=
180+
github.com/haproxytech/client-native/v2 v2.5.4-0.20220218084053-8755a69eb733 h1:PeVDCZaT0kqvo/8xmS6t+I1gisiHGN6p4F3DIJq6dEI=
181+
github.com/haproxytech/client-native/v2 v2.5.4-0.20220218084053-8755a69eb733/go.mod h1:S7CBbMb7hP8nxiL+PE2edFVMOMO7Uvptvr7hAQw5IJk=
181182
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220107093215-b580bd3a8a90 h1:OSCqtgyvhMVgAA9aIX1zN8FkiQwL/gp9h7Eqvt58hfg=
182183
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220107093215-b580bd3a8a90/go.mod h1:pEuHx+aFhn0lIdvAg1OaawQfeRkpq1I8HzjtZN4/PLI=
183184
github.com/haproxytech/go-logger v1.0.1-0.20211022075555-178f1cdf4d84 h1:rSLHjJ4VGvMZcGAGQ9GaXuhvdswu1iLVXTThLX6OKN8=
@@ -227,6 +228,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW
227228
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
228229
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
229230
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
231+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
232+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
230233
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
231234
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
232235
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@@ -281,6 +284,10 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
281284
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
282285
github.com/mitchellh/mapstructure v1.2.2 h1:dxe5oCinTXiTIcfgmZecdCzPmAJKd46KsCWc35r0TV4=
283286
github.com/mitchellh/mapstructure v1.2.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
287+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
288+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
289+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
290+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
284291
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
285292
github.com/nathanaelle/syslog5424/v2 v2.0.5 h1:oLVYQmKnv3nlnmvlbBWM+iWl9AONcU+tuxIL0fjko5A=
286293
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/v2/configuration"
3030
client_errors "github.com/haproxytech/client-native/v2/errors"
3131
"github.com/haproxytech/client-native/v2/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+
json := jsoniter.ConfigCompatibleWithStandardLibrary
116118
err := json.Unmarshal(spec, &m)
117119
if err != nil {
118120
return nil, err

0 commit comments

Comments
 (0)