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(api): Add automatic quota throttling #5485

Merged
merged 40 commits into from May 15, 2024

Conversation

rifont
Copy link
Contributor

@rifont rifont commented May 2, 2024

What changed? Why was the change needed?

  • Integrate the quota limiting PR from EE repo
  • Add feature flag for quota limiting
  • Rename feature flag for event limiting to align with other naming and purpose
  • Refactor cache key builders to use new folder hierarchy to separate builder pattern from use-cases
  • Add new cache key builders for quota limit cache

Screenshots

Passing key builder Tests
image

Expand for optional sections

Related enterprise PR

https://github.com/novuhq/packages-enterprise/pull/133

Special notes for your reviewer

@rifont rifont requested a review from djabarovgeorge May 2, 2024 10:10
@rifont rifont requested a review from a team as a code owner May 2, 2024 10:10
Copy link

linear bot commented May 2, 2024

@rifont rifont changed the title Ent 5 api ingress blocking cache keys chore(shared): Refactor cache key builders and quota limit feature flags May 2, 2024
Copy link

netlify bot commented May 2, 2024

Deploy Preview for dev-web-novu ready!

Name Link
🔨 Latest commit b016f39
🔍 Latest deploy log https://app.netlify.com/sites/dev-web-novu/deploys/66446561873f7c000877cf18
😎 Deploy Preview https://deploy-preview-5485--dev-web-novu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented May 2, 2024

Deploy Preview for novu-design ready!

Name Link
🔨 Latest commit b016f39
🔍 Latest deploy log https://app.netlify.com/sites/novu-design/deploys/66446561cc03680008fd30e5
😎 Deploy Preview https://deploy-preview-5485--novu-design.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@rifont rifont changed the title chore(shared): Refactor cache key builders and quota limit feature flags chore(shared, app-gen): Refactor cache key builders and quota limit feature flags May 2, 2024
@@ -71,7 +71,7 @@ export const SettingsNavMenu: React.FC = () => {
testId="side-nav-settings-branding-link"
></NavMenuLinkButton>
<NavMenuLinkButton
label="Billing plans"
label="Billing"
Copy link
Contributor Author

@rifont rifont May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a regression during Information Architecture feature development.

@@ -47,7 +48,7 @@ export class ResourceThrottlerInterceptor implements NestInterceptor {
organizationId,
environmentId: 'system',
userId: 'system',
key: FeatureFlagsKeysEnum.IS_EVENT_RESOURCE_LIMITING_ENABLED,
key: FeatureFlagsKeysEnum.IS_EVENT_QUOTA_LIMITING_ENABLED,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming to align with intended purpose, which is to limit resource quotas.

hash.update(str);

return hash.digest('hex');
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separating into a new file to keep independent from the key builder use-cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! , i wanted to separate this one and another 'createHmac' but was not able to do it because we had changes in multiple PRs.
IMO this one is a generic function and can live outside of the cache scope, wdyt?

buildEvaluateApiRateLimitKey,
buildServiceConfigApiRateLimitMaximumKey,
buildVariablesKey,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a code smell to update here, so we instead export each of the required builders in-line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file only has internal API changes, no change to external exports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file only has internal API changes, no changes to external exports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has comments added to provide a description of when to modify and use each base key builder. There are changes to the exports of these builders which are in-turn, consumed by the entities and queries.

@@ -87,15 +241,26 @@ export enum IdentifierPrefixEnum {
GROUPED_BLUEPRINT = 'g_b',
API_RATE_LIMIT_CATEGORY = 'a_r_l_c',
SERVICE_CONFIG = 's_c',
RESOURCE_TYPE = 'r_t',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New prefix to handle storage of quota usage for each resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactoring all the identifiers out into a separate file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

Copy link
Contributor

@djabarovgeorge djabarovgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left a couple of comments for your review.
all of them as thoughts

hash.update(str);

return hash.digest('hex');
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! , i wanted to separate this one and another 'createHmac' but was not able to do it because we had changes in multiple PRs.
IMO this one is a generic function and can live outside of the cache scope, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

Comment on lines 16 to 17
* Wraps the entire prefix string with curly braces. This has the effect of ensuring
* that the entire prefix string is treated as a single key part by Redis.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this one!

Comment on lines 2 to 5
***************************************
* BASE KEY BUILDERS
***************************************
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could create a separate file for every section that has such a title, wdyt?

@rifont rifont changed the title chore(shared, app-gen): Refactor cache key builders and quota limit feature flags feat(api): Add automatic quota throttling May 13, 2024
@rifont rifont merged commit 571104a into next May 15, 2024
26 checks passed
@rifont rifont deleted the ent-5-api-ingress-blocking-cache-keys branch May 15, 2024 08:06
github-actions bot pushed a commit that referenced this pull request May 16, 2024
* fix(web): Rename Billing plans to Billing

* refactor(shared): Tidy up builders and add comments

* chore(shared): Add feature flags for quota limiting

* test(shared): Update key builder test

* fix(api): Use new ff key

* chore(shared): Remove uncessary out-of-line exports

* chore(app-gen): Fix typo

* refactor(app-gen): Separate file for key builder identifiers

* chore: Update per PR comments

* fix: App gen imports

* fix: testing controller import

* fix: imports

* fix: naming

* chore: Upgrade to Redlock with typescript

* chore: Remove unused decorator

* feat: Add locking to cached entity interceptor

* test: fix create-usage-records test

* test: fix customer subscription updated test

* test(api): add evaluate-event-resource-limit tests

* chore: remove redundant resource guard

* feat(api): Add safe EE import for quota throttler

* chore: Add nestjs imports for billing

* test(api): Add ci ee flag to api ee tests

* test(api): Use enums for test

* test(api): Add quota throttler guard test

* chore: update submodule

* revert(api): Accidental package script change

* chore: Update submodule

* chore: submodule

* chore: submodule update

* chore: update submodule

* test(web): Update cypress tests

* chore: update submodule

* fix(billing): Add mongoose dependency

* test(web): Update billing widget assertion

* fix(app-gen): Separate method resolution and cache set

* chore: update submodule

* fix(app-gen): unlock only on error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants