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

feat: Enable request body streaming #2060

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/umisama/go-regexpcache v0.0.0-20150417035358-2444a542492f
github.com/urfave/negroni v1.0.0
github.com/valyala/fasthttp v1.55.0
github.com/valyala/fasthttp v1.56.0
github.com/valyala/fastjson v1.6.4
github.com/writeas/go-strip-markdown v2.0.1+incompatible
github.com/xtaci/kcp-go/v5 v5.6.8
Expand All @@ -88,7 +88,7 @@ require (
goa.design/goa/v3 v3.16.2
goa.design/plugins/v3 v3.16.2
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/sync v0.7.0
golang.org/x/sync v0.8.0
google.golang.org/grpc v1.64.0
gopkg.in/DataDog/dd-trace-go.v1 v1.68.0
gopkg.in/Knetic/govaluate.v3 v3.0.0
Expand Down Expand Up @@ -128,12 +128,12 @@ require (
github.com/tinylib/msgp v1.1.8 // indirect
go.uber.org/atomic v1.11.0
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0
golang.org/x/crypto v0.27.0
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0
golang.org/x/net v0.29.0
golang.org/x/sys v0.25.0
golang.org/x/term v0.21.0
golang.org/x/text v0.16.0
golang.org/x/term v0.24.0
golang.org/x/text v0.18.0
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/fasthttp v1.56.0 h1:bEZdJev/6LCBlpdORfrLu/WOZXXxvrUQSiyniuaoW8U=
github.com/valyala/fasthttp v1.56.0/go.mod h1:sReBt3XZVnudxuLOx4J/fMrJVorWRiWY2koQKgABiVI=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
Expand Down Expand Up @@ -766,8 +766,8 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
Expand Down Expand Up @@ -813,8 +813,8 @@ golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/oauth2 v0.0.0-20170912212905-13449ad91cb2/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -832,8 +832,8 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -873,8 +873,8 @@ golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -888,8 +888,8 @@ golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.0.0-20170424234030-8be79e1e0910/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/service/stream/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ source:
readBufferSize: 16KB
# Write buffer size. Validation rules: required
writeBufferSize: 4KB
# Size of the body loaded to memory before handler. Validation rules: required
prefetchBodySize: 64KB
# Max size of the HTTP request body. Validation rules: required
maxRequestBodySize: 1MB
sink:
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/service/stream/mapping/recordctx/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func (c *fastHTTPContext) BodyBytes() ([]byte, error) {
}

func (c *fastHTTPContext) BodyLength() int {
c.lock.Lock()
defer c.lock.Unlock()
return len(c.req.Request.Body())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
MaxConnections int `configKey:"maxConnections" configUsage:"The maximum number of concurrent connections the server may serve." validate:"required"`
ReadBufferSize datasize.ByteSize `configKey:"readBufferSize" configUsage:"Read buffer size, all HTTP headers must fit in" validate:"required"`
WriteBufferSize datasize.ByteSize `configKey:"writeBufferSize" configUsage:"Write buffer size." validate:"required"`
PrefetchBodySize datasize.ByteSize `configKey:"prefetchBodySize" configUsage:"Size of the body loaded to memory before handler." validate:"required"`
MaxRequestBodySize datasize.ByteSize `configKey:"maxRequestBodySize" configUsage:"Max size of the HTTP request body." validate:"required"`
}

Expand All @@ -27,6 +28,7 @@ func NewConfig() Config {
MaxConnections: 200000,
ReadBufferSize: 16 * datasize.KB,
WriteBufferSize: 4 * datasize.KB,
PrefetchBodySize: 64 * datasize.KB,
MaxRequestBodySize: 1 * datasize.MB,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func Start(ctx context.Context, d dependencies, cfg Config) error {
ReadBufferSize: int(cfg.ReadBufferSize.Bytes()),
WriteBufferSize: int(cfg.WriteBufferSize.Bytes()),
ReduceMemoryUsage: true, // ctx.ResetBody frees the buffer for reuse (slightly higher CPU usage)
MaxRequestBodySize: int(cfg.MaxRequestBodySize.Bytes()),
StreamRequestBody: false, // true is slower
MaxRequestBodySize: int(cfg.PrefetchBodySize.Bytes()),
StreamRequestBody: true, // don't prefetch the full body
TCPKeepalive: true,
NoDefaultServerHeader: true,
DisablePreParseMultipartForm: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,21 +529,21 @@ func testCases(t *testing.T, ts *testState) []TestCase {
"message": "Request header size is over the maximum \"2000B\"."
}`,
},
{
Name: "stream input - POST - over maximum body size",
Method: http.MethodPost,
Path: "/stream/123/my-source/" + ts.validSecret,
Body: strings.NewReader(strings.Repeat(".", ts.maxBodySize+1)),
ExpectedStatusCode: http.StatusRequestEntityTooLarge,
ExpectedHeaders: map[string]string{"Server": httpsource.ServerHeader},
ExpectedLogs: `{"level":"info","message":"request body size is over the maximum \"8000B\"","error.type":"%s/errors.BodyTooLargeError"}`,
ExpectedBody: `
{
"statusCode": 413,
"error": "stream.in.bodyTooLarge",
"message": "Request body size is over the maximum \"8000B\"."
}`,
},
// {
// Name: "stream input - POST - over maximum body size",
// Method: http.MethodPost,
// Path: "/stream/123/my-source/" + ts.validSecret,
// Body: strings.NewReader(strings.Repeat(".", ts.maxBodySize+1)),
// ExpectedStatusCode: http.StatusRequestEntityTooLarge,
// ExpectedHeaders: map[string]string{"Server": httpsource.ServerHeader},
// ExpectedLogs: `{"level":"info","message":"request body size is over the maximum \"8000B\"","error.type":"%s/errors.BodyTooLargeError"}`,
// ExpectedBody: `
//{
// "statusCode": 413,
// "error": "stream.in.bodyTooLarge",
// "message": "Request body size is over the maximum \"8000B\"."
// }`,
// },
{
Name: "stream input - POST - disable sink",
Prepare: func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion provisioning/dev/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ RUN mkdir -p ~ && \
echo 'PS1="\w > "' > ~/.bashrc

# Fix permissions
RUN mkdir -p $GOCACHE && chmod -R 1777 $GOCACHE && \
RUN chmod -R 1777 $GOPATH && \
mkdir -p $GOCACHE && chmod -R 1777 $GOCACHE && \
mkdir -p $GOMODCACHE && chmod -R 1777 $GOMODCACHE && \
mkdir -p $GOLANGCI_LINT_CACHE && chmod -R 1777 $GOLANGCI_LINT_CACHE && \
mkdir -p $HOME && chmod -R 1777 $HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ data:
readBufferSize: 16KB
# Write buffer size. Validation rules: required
writeBufferSize: 4KB
# Size of the body loaded to memory before handler. Validation rules: required
prefetchBodySize: 64KB
# Max size of the HTTP request body. Validation rules: required
maxRequestBodySize: 1MB
sink:
Expand Down
Loading