feat(ratelimits): Add metric buckets rate limit#11506
Conversation
size-limit report 📦
|
b44c390 to
945c6e5
Compare
packages/utils/src/ratelimit.ts
Outdated
| for (const category of categories.split(';')) { | ||
| updatedRateLimits[category] = now + delay; | ||
| if (category === 'metric_bucket') { | ||
| const namespaces = limit.split(':', 5)[4] ? limit.split(':', 5)[4].split(';') : null; |
There was a problem hiding this comment.
can we combine this limit.split call with the one above in const [retryAfter, categories]?
also generally we prefer against using null, undefined is returned by default when array access returns nothing, and it helps reduce bundle size generally.
|
|
||
| if (!namespaces || namespaces.includes('custom')) { | ||
| // back off transmitting metrics from the SDK | ||
| updatedRateLimits[category] = now + delay; |
There was a problem hiding this comment.
this is the same as line 96, can we merge the logic somehow?
There was a problem hiding this comment.
I added all in one if statement. But as it is not so clear anymore that 'metric_bucket' actually is the category when namespaces is defined, I added a comment that explains it :)
packages/utils/src/ratelimit.ts
Outdated
| // namespaces will be present when category === 'metric_bucket' | ||
| if (category !== 'metric_bucket' || !namespaces || namespaces.split(';').includes('custom')) { | ||
| updatedRateLimits[category] = now + delay; | ||
| } |
There was a problem hiding this comment.
| // namespaces will be present when category === 'metric_bucket' | |
| if (category !== 'metric_bucket' || !namespaces || namespaces.split(';').includes('custom')) { | |
| updatedRateLimits[category] = now + delay; | |
| } | |
| if (category === 'metric_bucket') { | |
| // namespaces will be present when category === 'metric_bucket' | |
| if (!namespaces || namespaces.split(';').includes('custom')) { | |
| updatedRateLimits[category] = now + delay; | |
| } | |
| } else { | |
| updatedRateLimits[category] = now + delay; | |
| } |
This is giga nitting but I would do it like this. Regardless of bundlesize (hoping terser is smart enough). Makes it easier to understand this admittedly convoluted logic.
| }); | ||
| }); | ||
|
|
||
| describe('data category "metric_bucket"', () => { |
There was a problem hiding this comment.
Let's add another test case for 2700:metric_bucket:organization:quota_exceeded:
closes #11336