feat(streams): support explicit LIMIT 0 in XTRIM/XADD trimming via XTrimLimitDisabled sentinel#3848
Merged
Merged
Conversation
…rimLimitDisabled sentinel
Redis treats XTRIM/XADD trimming LIMIT 0 (valid only with the ~ approximate
flag) as "disable the trimming effort cap entirely", which is different from
omitting LIMIT (implicit default of 100 * stream-node-max-entries examined
entries). The command builders only emitted LIMIT when limit > 0, so callers
could never send an explicit LIMIT 0.
Following the KeepTTL = -1 precedent, add the XTrimLimitDisabled = -1
sentinel and a shared appendXTrimLimit helper used by XAdd, xTrim and
xTrimMode:
- limit == 0 keeps the historical behavior: no LIMIT clause is sent;
- limit > 0 emits LIMIT <limit> as before;
- limit < 0 (XTrimLimitDisabled) emits an explicit LIMIT 0.
No public signatures or struct fields change, and existing callers passing 0
produce byte-identical commands. Exact ("=") trim commands (XTrimMaxLen,
XTrimMinID and their Mode variants) still never emit LIMIT; XAddArgs keeps
the existing upstream behavior of passing a user-supplied Limit through even
without Approx (Redis rejects it server-side), now explicitly documented on
the field.
Add server-less unit tests asserting the constructed command args for the
omitted/positive/disabled-limit and exact-trim cases across all XTRIM
variants and XAdd.
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
ndyakov
reviewed
Jun 11, 2026
Member
There was a problem hiding this comment.
Hello @TheRealMal and thank you for this. I like the approach and it is somewhat consistent with the options where we use -1 to disable a given option. Let me rerun the failed job (it was github runner issue) and I will review the code.
ndyakov
approved these changes
Jun 11, 2026
ndyakov
left a comment
Member
There was a problem hiding this comment.
Looks good to me, thank you @TheRealMal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Redis distinguishes between omitting the
LIMITclause and passingLIMIT 0when trimming a stream with the approximate (~) strategy:LIMITclause — Redis applies an implicit effort cap of100 * stream-node-max-entriesexamined entries per call.LIMIT 0— the effort cap is disabled entirely; Redis trims as many macro nodes as the threshold requires in a single call.(See the
XADD/XTRIMdocs: "Specifying the value 0 as count disables the limiting mechanism entirely.")Today the library cannot express the second form.
xTrim,xTrimMode, andXAddall build the clause with:so
0is silently swallowed and there is no way to sendXTRIM key MINID ~ <id> LIMIT 0through the typed API. This matters in practice for periodic cleaners on busy streams: with the implicit default cap, a singleXTRIM ... ~call may not reclaim a large backlog, and the only workaround is dropping down to rawDo().Changes
Introduces a documented negative-value sentinel, following the existing
KeepTTL = -1precedent incommands.go(the same "zero value already has a meaning, a third state is needed" problem forSET):The
limitparameter ofXTrimMaxLenApprox,XTrimMinIDApprox,XTrimMaxLenApproxMode,XTrimMinIDApproxMode, and theXAddArgs.Limitfield now have three documented states:0> 0LIMIT <n>< 0(XTrimLimitDisabled)LIMIT 0Implementation is a single shared helper,
appendXTrimLimit, used byXAdd,xTrim, andxTrimMode, replacing the three duplicatedif limit > 0blocks.Usage:
Backward compatibility
limit = 0still omits the clause; positive values are emitted as before. Only previously-meaningless negative values gain a meaning.XTrimMaxLen,XTrimMinID, and theirModevariants) take no limit parameter and still can never emitLIMIT— Redis rejectsLIMITwithout~(ERR syntax error, LIMIT cannot be used without the special ~ option). ForXAddArgsthe library keeps its existing stance of not validating theApprox/Limitcombination client-side (matching how the conflictingMaxLen/MinIDpair is handled); this is now stated explicitly in theLimitfield docs instead of being implicit.Testing
Added server-less unit tests asserting the constructed command args via the existing
captureCmdablepattern (stream_commands_unit_test.go):TestXTrim_LimitArgs— 13 cases covering omitted / positive / disabled limit acrossXTrimMaxLenApprox,XTrimMinIDApprox, theirModevariants, and pinning that exact-trim commands never emitLIMIT.TestXAdd_TrimLimitArgs— 5 cases covering theXAddArgs.Limitpath, including clause ordering relative to the entry ID.go build ./...,go vet ./..., andgofmtare clean; the pre-existing unit test suite passes.Note
Low Risk
Additive sentinel and command-building change only; existing
limit0 and positive values behave the same, with coverage for emitted Redis args.Overview
Adds
XTrimLimitDisabled(-1) so approximate stream trimming (XADD/XTRIMwith~) can send an explicitLIMIT 0to Redis, disabling the default trimming effort cap—something the client could not express before because onlylimit > 0ever emitted aLIMITclause.A shared
appendXTrimLimithelper centralizes the three behaviors: omitLIMITwhenlimit == 0(unchanged),LIMIT nwhen positive, andLIMIT 0when negative.XAdd,xTrim, andxTrimModeuse it instead of duplicated checks.XAddArgs.Limitand the*Approx*trim APIs are documented accordingly.New
stream_commands_unit_test.gotests assert built command args viacaptureCmdablefor omitted, positive, and disabled limits across trim variants andXAdd, including that exact (=) trims never emitLIMIT.Reviewed by Cursor Bugbot for commit 87acd5d. Bugbot is set up for automated code reviews on this repo. Configure here.