Vercel Edge Function
Unanswered
Atlantic horse mackerel posted this in #help-forum
Atlantic horse mackerelOP
I'm getting this error in my logs
Error: Response closed due to connection limit when I was testing my edge api. This is some of code import fetch from "node-fetch";
import { NextRequest, NextResponse } from "next/server";
export const config = {
runtime: "edge",
};
export default async function handler(req: NextRequest) {
const {searchParams} = new URL(req.url)
const publicKey = searchParams.get('publicKey')
const fetchPromises = [
fetch(
`https://alphaline.wtf/api/walletContainer/getBalance?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/borrowingContainer/borrow?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/borrowingContainer/active?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/borrowingContainer/default?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/lendingContainer/lend?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/lendingContainer/active?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/lendingContainer/default?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/recentDefaultRate/fiveRecentDefaultRate?publicKey=${publicKey}`
),
fetch(
`https://alphaline.wtf/api/recentDefaultRate/tenRecentDefaultRate?publicKey=${publicKey}`
),
fetch(`https://alphaline.wtf/api/age/ageOfAccount?publicKey=${publicKey}`),
];
const responses = await Promise.all(fetchPromises);
for (const res of responses) {
if (!res.ok) {
console.log("Failed to fetch:", res);
throw new Error("Failed to fetch");
}
}
const [
balance,
borrows,
activeBorrows,
defaults,
loans,
activeLoan,
defaultLoan,
fiveRecentDefaultRate,
tenRecentDefaultRate,
ageOfAccount,
] = await Promise.all(responses.map((res) => res.json()));14 Replies
wow. ur codes are really interesting. Solana. Cool.
does it work with Node.js(Aws lambda) instead of Edge Runtime(Cloudflare worker)?
or just delete
runtime: "node.js"or just delete
config completely.Atlantic horse mackerelOP
I'll try it rn
@tafutada777 wow. ur codes are really interesting. Solana. Cool.
Atlantic horse mackerelOP
appreciate it bro
Edge Runtime is a brand new service from Vercel, which allows you to streaming and CPU usage base charge. in ur case, when CPU is idle during async/await, you won't be charged.
Atlantic horse mackerelOP
Changing the runtime to node.js results in this error
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:399:5)
at new URL (node:internal/url:560:13)
at handler (/var/task/.next/server/pages/api/creditScore/edgeScore.js:33:17)
at /var/task/node_modules/next/dist/server/api-utils/node.js:463:16
at /var/task/node_modules/next/dist/server/lib/trace/tracer.js:113:36
at NoopContextManager.with (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7057)
at ContextAPI.with (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:516)
at NoopTracer.startActiveSpan (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18086)
at ProxyTracer.startActiveSpan (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18847)
at /var/task/node_modules/next/dist/server/lib/trace/tracer.js:102:107 {
input: 'undefined',
code: 'ERR_INVALID_URL'
}The serverless function version of my api works fine but it's just too slow.
Edge is just faster
@tafutada777 when I removed config. I'm getting this error
ypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:399:5)
at new URL (node:internal/url:560:13)
at handler (/var/task/.next/server/pages/api/creditScore/edgeScore.js:32:17)
at /var/task/node_modules/next/dist/server/api-utils/node.js:463:16
at /var/task/node_modules/next/dist/server/lib/trace/tracer.js:113:36
at NoopContextManager.with (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7057)
at ContextAPI.with (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:516)
at NoopTracer.startActiveSpan (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18086)
at ProxyTracer.startActiveSpan (/var/task/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18847)
at /var/task/node_modules/next/dist/server/lib/trace/tracer.js:102:107 {
input: 'undefined',
code: 'ERR_INVALID_URL'
} which I believe has to do with this code const url = new URL(req.nextUrl);
const publicKey = url.searchParams.get("publicKey");Do you know how to properly get the body from a url in an edge function that's like this
export default async function handler(req: NextRequest) {when you remove the config.runtime, it defaults to nodejs instead of edge. sorry to make you confused. so let's stick with edge.
back to the original question, you might reach the cap on open socket. (i am not sure).
so u can work around by split
back to the original question, you might reach the cap on open socket. (i am not sure).
so u can work around by split
fetchPromises, the promise.all, into small chunks like 3 requests at onceindeed, the said error seems to be coming from underling CF like this
https://community.cloudflare.com/t/cf-worker-response-closed-due-to-connection-limit/198119
https://community.cloudflare.com/t/cf-worker-response-closed-due-to-connection-limit/198119
Pumi
interesting -- I just had the same error in my prod deploy (not locally), trying to access the Google Custom Search API. Once I deactivated edge runtime, it worked again. So I think this is consistent with what you describe, @tafutada777 .