Skip to content

Commit

Permalink
Increase function timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Apr 21, 2024
1 parent 1a5ab65 commit 6366d56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/stack-server/src/lib/route-handlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { groupBy, typedIncludes } from "@stackframe/stack-shared/dist/utils/arra
import { deindent } from "@stackframe/stack-shared/dist/utils/strings";
import { generateSecureRandomString } from "@stackframe/stack-shared/dist/utils/crypto";
import { KnownErrors } from "@stackframe/stack-shared/dist/known-errors";
import { runAsynchronously, wait } from "@stackframe/stack-shared/dist/utils/promises";

const allowedMethods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] as const;

Expand Down Expand Up @@ -227,9 +228,20 @@ export function deprecatedSmartRouteHandler(handler: (req: NextRequest, options:
censoredUrl.searchParams.set(key, value.slice(0, 4) + "--REDACTED--" + value.slice(-4));
}

// request duration warning
let hasRequestFinished = false;
const warnAfterSeconds = 12;
runAsynchronously(async () => {
await wait(warnAfterSeconds * 1000);
if (!hasRequestFinished) {
captureError("request-timeout-watcher", new Error(`Request with ID ${requestId} to endpoint ${req.url} has been running for ${warnAfterSeconds} seconds. Try to keep requests short. The request may be cancelled by the serverless provider if it takes too long.`));
}
});

console.log(`[API REQ] [${requestId}] ${req.method} ${censoredUrl}`);
const timeStart = performance.now();
const res = await handler(req, options, requestId);
hasRequestFinished = true;
const time = (performance.now() - timeStart);
console.log(`[ RES] [${requestId}] ${req.method} ${censoredUrl} (in ${time.toFixed(0)}ms)`);
return res;
Expand Down
7 changes: 7 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"functions": {
"**/*": {
"maxDuration": 300
}
}
}

0 comments on commit 6366d56

Please sign in to comment.