Skip to content

Commit 87f54d9

Browse files
fix: Cache fastjson.Value
1 parent 13b98e4 commit 87f54d9

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

internal/pkg/service/stream/mapping/recordctx/fasthttp.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/keboola/go-utils/pkg/orderedmap"
1313
"github.com/valyala/fasthttp"
14+
"github.com/valyala/fastjson"
1415

1516
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
1617
)
@@ -27,6 +28,7 @@ type fastHTTPContext struct {
2728
bodyStringErr error
2829
bodyMap *orderedmap.OrderedMap
2930
bodyMapErr error
31+
jsonValue *fastjson.Value
3032
}
3133

3234
func FromFastHTTP(ctx context.Context, timestamp time.Time, req *fasthttp.RequestCtx) Context {
@@ -122,6 +124,28 @@ func (c *fastHTTPContext) BodyMap() (*orderedmap.OrderedMap, error) {
122124
return c.bodyMap, c.bodyMapErr
123125
}
124126

127+
func (c *fastHTTPContext) JsonValue(parserPool *fastjson.ParserPool) (*fastjson.Value, error) {
128+
c.lock.Lock()
129+
defer c.lock.Unlock()
130+
131+
if c.jsonValue != nil {
132+
return c.jsonValue, nil
133+
}
134+
135+
body, err := c.BodyBytes()
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
parser := parserPool.Get()
141+
defer parserPool.Put(parser)
142+
143+
jsonValue, err := parser.ParseBytes(body)
144+
c.jsonValue = jsonValue
145+
146+
return jsonValue, err
147+
}
148+
125149
func (c *fastHTTPContext) headersToMap() *orderedmap.OrderedMap {
126150
out := orderedmap.New()
127151
for _, k := range c.req.Request.Header.PeekKeys() {

internal/pkg/service/stream/mapping/recordctx/http.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/keboola/go-utils/pkg/orderedmap"
14+
"github.com/valyala/fastjson"
1415

1516
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
1617
"github.com/keboola/keboola-as-code/internal/pkg/utils/ip"
@@ -29,6 +30,7 @@ type httpContext struct {
2930
bodyBytesErr error
3031
bodyMap *orderedmap.OrderedMap
3132
bodyMapErr error
33+
jsonValue *fastjson.Value
3234
}
3335

3436
func FromHTTP(timestamp time.Time, req *http.Request) Context {
@@ -124,6 +126,28 @@ func (c *httpContext) BodyMap() (*orderedmap.OrderedMap, error) {
124126
return c.bodyMap, c.bodyMapErr
125127
}
126128

129+
func (c *httpContext) JsonValue(parserPool *fastjson.ParserPool) (*fastjson.Value, error) {
130+
c.lock.Lock()
131+
defer c.lock.Unlock()
132+
133+
if c.jsonValue != nil {
134+
return c.jsonValue, nil
135+
}
136+
137+
body, err := c.BodyBytes()
138+
if err != nil {
139+
return nil, err
140+
}
141+
142+
parser := parserPool.Get()
143+
defer parserPool.Put(parser)
144+
145+
jsonValue, err := parser.ParseBytes(body)
146+
c.jsonValue = jsonValue
147+
148+
return jsonValue, err
149+
}
150+
127151
func (c *httpContext) bodyBytesWithoutLock() ([]byte, error) {
128152
if c.bodyBytes == nil && c.bodyBytesErr == nil {
129153
c.bodyBytes, c.bodyBytesErr = io.ReadAll(c.req.Body)

internal/pkg/service/stream/mapping/recordctx/recordctx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/keboola/go-utils/pkg/orderedmap"
10+
"github.com/valyala/fastjson"
1011
)
1112

1213
type Context interface {
@@ -18,4 +19,5 @@ type Context interface {
1819
BodyString() (string, error)
1920
BodyBytes() ([]byte, error)
2021
BodyMap() (*orderedmap.OrderedMap, error)
22+
JsonValue(*fastjson.ParserPool) (*fastjson.Value, error)
2123
}

internal/pkg/service/stream/mapping/table/column/renderer.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,7 @@ func (r *Renderer) mapPathCSVValue(c Path, ctx recordctx.Context) (string, error
105105
}
106106

107107
func (r *Renderer) jsonPathCSVValue(c Path, ctx recordctx.Context) (string, error) {
108-
body, err := ctx.BodyBytes()
109-
if err != nil {
110-
return "", err
111-
}
112-
113-
parser := r.fastjsonPool.Get()
114-
defer r.fastjsonPool.Put(parser)
115-
116-
value, err := parser.ParseBytes(body)
108+
value, err := ctx.JsonValue(r.fastjsonPool)
117109
if err != nil {
118110
return "", err
119111
}

0 commit comments

Comments
 (0)