Skip to content

Commit f07e5cc

Browse files
[FIXED] (2.11) Too Many Requests error (#6410)
When sending too many requests into a stream it's rate-limited. However, that would result in the following error using the Go client (as part of a `nats bench`): `invalid jetstream publish response` due to `unexpected end of JSON input`, since the returned pub ack message is empty. This PR introduces a specific error for 429 Too Many Requests so the clients can properly handle this without further changes. Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
2 parents 23c0c0b + d165f1e commit f07e5cc

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

server/errors.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,5 +1648,15 @@
16481648
"help": "",
16491649
"url": "",
16501650
"deprecates": ""
1651+
},
1652+
{
1653+
"constant": "JSStreamTooManyRequests",
1654+
"code": 429,
1655+
"error_code": 10167,
1656+
"description": "too many requests",
1657+
"comment": "",
1658+
"help": "",
1659+
"url": "",
1660+
"deprecates": ""
16511661
}
16521662
]

server/jetstream_errors_generated.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ const (
476476
// JSStreamTemplateNotFoundErr template not found
477477
JSStreamTemplateNotFoundErr ErrorIdentifier = 10068
478478

479+
// JSStreamTooManyRequests too many requests
480+
JSStreamTooManyRequests ErrorIdentifier = 10167
481+
479482
// JSStreamTransformInvalidDestination stream transform: {err}
480483
JSStreamTransformInvalidDestination ErrorIdentifier = 10156
481484

@@ -660,6 +663,7 @@ var (
660663
JSStreamTemplateCreateErrF: {Code: 500, ErrCode: 10066, Description: "{err}"},
661664
JSStreamTemplateDeleteErrF: {Code: 500, ErrCode: 10067, Description: "{err}"},
662665
JSStreamTemplateNotFoundErr: {Code: 404, ErrCode: 10068, Description: "template not found"},
666+
JSStreamTooManyRequests: {Code: 429, ErrCode: 10167, Description: "too many requests"},
663667
JSStreamTransformInvalidDestination: {Code: 400, ErrCode: 10156, Description: "stream transform: {err}"},
664668
JSStreamTransformInvalidSource: {Code: 400, ErrCode: 10155, Description: "stream transform source: {err}"},
665669
JSStreamUpdateErrF: {Code: 500, ErrCode: 10069, Description: "{err}"},
@@ -2503,6 +2507,16 @@ func NewJSStreamTemplateNotFoundError(opts ...ErrorOption) *ApiError {
25032507
return ApiErrors[JSStreamTemplateNotFoundErr]
25042508
}
25052509

2510+
// NewJSStreamTooManyRequestsError creates a new JSStreamTooManyRequests error: "too many requests"
2511+
func NewJSStreamTooManyRequestsError(opts ...ErrorOption) *ApiError {
2512+
eopts := parseOpts(opts)
2513+
if ae, ok := eopts.err.(*ApiError); ok {
2514+
return ae
2515+
}
2516+
2517+
return ApiErrors[JSStreamTooManyRequests]
2518+
}
2519+
25062520
// NewJSStreamTransformInvalidDestinationError creates a new JSStreamTransformInvalidDestination error: "stream transform: {err}"
25072521
func NewJSStreamTransformInvalidDestinationError(err error, opts ...ErrorOption) *ApiError {
25082522
eopts := parseOpts(opts)

server/stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4336,7 +4336,8 @@ func (mset *stream) queueInbound(ib *ipQueue[*inMsg], subj, rply string, hdr, ms
43364336
mset.srv.RateLimitWarnf("Dropping messages due to excessive stream ingest rate on '%s' > '%s': %s", mset.acc.Name, mset.name(), err)
43374337
if rply != _EMPTY_ {
43384338
hdr := []byte("NATS/1.0 429 Too Many Requests\r\n\r\n")
4339-
mset.outq.send(newJSPubMsg(rply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0))
4339+
b, _ := json.Marshal(&JSPubAckResponse{PubAck: &PubAck{Stream: mset.cfg.Name}, Error: NewJSStreamTooManyRequestsError()})
4340+
mset.outq.send(newJSPubMsg(rply, _EMPTY_, _EMPTY_, hdr, b, nil, 0))
43404341
}
43414342
}
43424343
}

0 commit comments

Comments
 (0)