Comments (7)
Here's a repro: https://github.com/unstubbable/upstash-ratelimit-analytics-lambda-timeout
- The timeouts do not occur when analytics is disabled.
- They also do not occur with analytics enabled, and using a non-streaming handler.
- With analytics enabled, not only does the timeout occur, but the response times are also significantly higher (approx. 2–3s vs. 150ms)
- Awaiting the
pending
promise does not help.
from ratelimit.
Hi @unstubbable,
I tried your lambda and saw that the requests timeout like you said. Long story short, I found a solution but didn't figure out why this occurs in the first place.
I will make sure to add an example for streaming lambda or update our docs.
Solution
- I checked whether the analytics were submitted correctly to redis before the Lambda timeout. They are succesfully submitted.
- We also receive a succesful response from the API, so the code in our handler executes properly
But we still get timeout. I tried to find if there were other people who experienced similar issues and found some posts [1, 2, 3, 4].
What I kept seeing was the callbackWaitsForEmptyEventLoop
parameter in the Lambda nodejs context. I set the parameter to false in the handler you provided and the timeouts dissappeared.
import {Ratelimit} from '@upstash/ratelimit';
import {Redis} from '@upstash/redis';
import type {APIGatewayProxyEventV2} from 'aws-lambda';
export const handler = awslambda.streamifyResponse<APIGatewayProxyEventV2>(
async (event, responseStream, context) => {
console.log(JSON.stringify({event}));
context.callbackWaitsForEmptyEventLoop = false
// ...
I looked into why we need to set this parameter to false in our case but I couldn't figure that out yet. After this change, there is no more timeouts.
Checking Latency
After verify the solution, I checked the time it takes for the requests to complete. The request time reduced as we expect after the fix.
Pre Fix
--- Successful Requests Statistics ---
Mean Response Time: 2.65 seconds
Minimum Response Time: 2.469 seconds
1st Quantile Response Time: 2.571 seconds
Median Response Time: 2.631 seconds
3rd Quantile Response Time: 2.666 seconds
Maximum Response Time: 3.271 seconds
--- Rate-Limited Requests Statistics ---
Mean Response Time: 2.63 seconds
Minimum Response Time: 2.508 seconds
1st Quantile Response Time: 2.563 seconds
Median Response Time: 2.638 seconds
3rd Quantile Response Time: 2.694 seconds
Maximum Response Time: 2.766 seconds
--- Summary ---
Total Successful Requests: 41
Total Rate-Limited Requests: 19
Post Fix
--- Successful Requests Statistics ---
Mean Response Time: 0.456 seconds
Minimum Response Time: 0.306 seconds
1st Quantile Response Time: 0.309 seconds
Median Response Time: 0.316 seconds
3rd Quantile Response Time: 0.425 seconds
Maximum Response Time: 1.353 seconds
--- Rate-Limited Requests Statistics ---
Mean Response Time: 0.31 seconds
Minimum Response Time: 0.289 seconds
1st Quantile Response Time: 0.299 seconds
Median Response Time: 0.301 seconds
3rd Quantile Response Time: 0.308 seconds
Maximum Response Time: 0.604 seconds
--- Summary ---
Total Successful Requests: 10
Total Rate-Limited Requests: 50
from ratelimit.
Btw, I don't know for sure, there might be a floating promise in our core analytics packages. Thats why event loop waits until the end to execute the floating promises and sometimes not even executing them. That might be worth checking.
from ratelimit.
Hi again @unstubbable,
We identified the reason why we had to set the callbackWaitsForEmptyEventLoop
parameter to false.
In the core-analytics library, there is a cache which calls setInterval but clearInterval is never called. We are making changes to disable the cache in rate limit library and only enable it in the analytics dashboard in Upstash console which is the original purpose of the cache.
After this change, lambda timeouts stopped without having to set callbackWaitsForEmptyEventLoop
to false.
Thanks for bringing this issue to our attention!
from ratelimit.
It looks like you've found a temporary fix for the issue. If an immediate fix for analytics isn't necessary, I'll postpone addressing this issue for now since we're currently short on resources. Additionally, it would be helpful if you could share a sample code to make it easier to fix in the future.
from ratelimit.
Hey, @unstubbable, I haven't found the underlying issue, but I'm still trying to figure out.
from ratelimit.
Closing this issue since the changes were merged and version was bumped. Thanks again @unstubbable.
from ratelimit.
Related Issues (20)
- Version 1.1.2 throws errors HOT 3
- Is there a way to fetch the remaining requests for a given key HOT 5
- Ratelimit.tokenBucket default tokens value HOT 3
- Dynamic token cost on Ratelimit.tokenBucket algorithm HOT 3
- Ratelimit doesn't seem to return proper reset time HOT 4
- Ratelimit usage with ioredis HOT 4
- Question about rate limit HOT 1
- SlidingWindow dose not work properly in the latest release HOT 1
- Rate Limiter takes 5s to complete API call HOT 5
- blockUntilReady and cf workers HOT 7
- Limiting Concurrency HOT 9
- custom rates support HOT 21
- Upstash Ratelimit Error in GH Action HOT 8
- cf workers: TypeError: Cannot read properties of undefined (reading 'eval') HOT 7
- Support getting and resetting limit of a specific identifier HOT 11
- Self-hosted Redis use? HOT 3
- How to set one hour limit for sliding window? HOT 2
- Awaiting `pending` promise is required in Lambda HOT 1
- No TTL set for slidingWindow rate limit keys on latest version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ratelimit.