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

feature: coalescing calls + feature: max cache size #877

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

daan944
Copy link
Contributor

@daan944 daan944 commented Oct 11, 2024

To not break existing functionality, I chose to not modify the cache behavior but introduce a second one.

If options.coalesce is set, multiple calls to the circuitbreaker will be handled as one, within the given timeframe (options.coalesceTTL).

If true, this provides coalescing of requests to this breaker, in other words: the promise will be cached.
Only one action (with same cache key) is executed at a time, and the other pending actions wait for the result. Performance will improve when rapidly firing the circuitbreaker with the same request, especially on a slower action (e.g. multiple end-users fetching same data from remote). Will use internal cache only. Can be used in combination with options.cache.

Additional fix/feature:

feature: max cache size

To prevent internal cache from growing without bounds (and triggering OOM crashes), the cache is now limited to 2^24 items. This is an insane amount, so options.cacheSize and options.coalesceSize have been added to control this.

lib/cache.js Outdated Show resolved Hide resolved
@daan944
Copy link
Contributor Author

daan944 commented Oct 11, 2024

Up for discussion: If you don't consider coalescing a breaking change, I could modify the existing cache behavior instead.

The main difference is: cache will only cache successful calls and only at the end of the call. Coalescing will cache both and at the beginning of the call.

Also, I'm uncertain external cache transports will work with caching promises necessary for coalescing. I do see current cache is also caching the promise, but I'm currently not using the cache feature of circuitbreaker so I don't know the details and caveats. Probably all memory cache functions will work as expected, but external key/value storage like Redis would require custom implementation and might break coalescing.

@lholmquist
Copy link
Member

would you mind rebasing? i had to add a fix for the CI to pass

daan-nu and others added 2 commits October 15, 2024 09:49
If options.coalesce is set, multiple calls to the circuitbreaker will
be handled as one, within the given timeframe (options.coalesceTTL).

feature: max cache size

To prevent internal cache from growing without bounds (and triggering
OOM crashes), the cache is now limited to 2^24 items. This is an insane
amount, so options.cacheSize and options.coalesceSize have been added
to control this.
@daan944
Copy link
Contributor Author

daan944 commented Oct 21, 2024

Hi @lholmquist the branch is up to date with main, can I do anything else to help this PR along? (Or should I just be more patient ;)? ). Thanks.

@lholmquist
Copy link
Member

(Or should I just be more patient ;)? )

Totally my fault for slacking at looking at this 🤣

@coveralls
Copy link

Pull Request Test Coverage Report for Build 11344548094

Details

  • 23 of 23 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.07%) to 98.259%

Totals Coverage Status
Change from base Build 11331355812: -0.07%
Covered Lines: 392
Relevant Lines: 393

💛 - Coveralls

@lholmquist
Copy link
Member

@daan944 thanks for the contribution, i think this looks pretty good. This will probably be a minor release since it doesn't break any existing functionality

@lholmquist lholmquist merged commit d50c912 into nodeshift:main Oct 23, 2024
15 checks passed
This was referenced Oct 14, 2024
@lholmquist
Copy link
Member

just released as 8.2.0 on npm

@daan944
Copy link
Contributor Author

daan944 commented Oct 23, 2024

Awesome! thanks :) I hope to issue a PR for the types at https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/opossum tomorrow.

@daan944
Copy link
Contributor Author

daan944 commented Oct 25, 2024

I see there's one issue with coalesce: the cachekey is only generated when the 'normal' cache is enabled too. Am working on a fix and will create new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants