Skip to content

Commit

Permalink
perf: 替换vercel KV 为 upstash redis
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 28, 2024
1 parent 42505d7 commit b387a13
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion doc/cn/VERCEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ yarn run deploy:vercel # 部署到vercel
```

### 已知问题
1. redis 连接关闭有问题会导致客户端连接数过多导致无法连接
1. redis 连接关闭有问题会导致客户端连接数过多导致无法连接建议使用upstash的redis服务
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"prepare:vercel": "tsx ./scripts/plugins/vercel/setenv.ts"
},
"dependencies": {
"cloudflare-worker-adapter": "^1.3.0"
"cloudflare-worker-adapter": "^1.3.2"
},
"devDependencies": {
"@antfu/eslint-config": "^2.27.3",
Expand Down
24 changes: 9 additions & 15 deletions src/adapter/vercel/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import * as process from 'node:process';
import { RedisCache } from 'cloudflare-worker-adapter/cache/redis';
import { UpStashRedis } from 'cloudflare-worker-adapter/cache/upstash';
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { ENV } from '../../config/env';
import { createRouter } from '../../route';

export default async function (request: VercelRequest, response: VercelResponse) {
let redis: RedisCache | null = null;
let redis: UpStashRedis | null = null;
try {
const {
UPSTASH_REDIS_REST_URL,
UPSTASH_REDIS_REST_TOKEN,
VERCEL_DOMAIN,
REDIS_URL,
} = process.env;
if (!VERCEL_DOMAIN) {
response.status(500).send('VERCEL_DOMAIN is required');
if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {
response.status(500).send('UPSTASH_REDIS_REST_TOKEN and UPSTASH_REDIS_REST_URL are required');
return;
}
if (!REDIS_URL) {
response.status(500).send('REDIS_URL is required');
if (!VERCEL_DOMAIN) {
response.status(500).send('VERCEL_DOMAIN is required');
return;
}
const cache = RedisCache.createFromUri(REDIS_URL);
const cache = UpStashRedis.create(UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN);
// edge function 使用 redis 作为数据库容易出现连接数过多的问题,此处仅作为演示,请自行实现 `Cache` 接口
redis = cache;
ENV.merge({
Expand Down Expand Up @@ -49,15 +50,8 @@ export default async function (request: VercelRequest, response: VercelResponse)
body,
});
const res = await router.fetch(newReq);
if (redis) {
await redis.close().catch(console.error);
redis = null;
}
response.status(res.status).send(await res.text());
} catch (e) {
if (redis) {
await redis.close();
}
response.status(500).send(JSON.stringify({
message: (e as Error).message,
stack: (e as Error).stack,
Expand Down
25 changes: 19 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,13 @@
"@typescript-eslint/types" "8.3.0"
eslint-visitor-keys "^3.4.3"

"@upstash/redis@^1.34.0":
version "1.34.0"
resolved "https://registry.yarnpkg.com/@upstash/redis/-/redis-1.34.0.tgz#f32cd53ebeeafbba7eca10f8597a573d5a2fed0d"
integrity sha512-TrXNoJLkysIl8SBc4u9bNnyoFYoILpCcFJcLyWCccb/QSUmaVKdvY0m5diZqc3btExsapcMbaw/s/wh9Sf1pJw==
dependencies:
crypto-js "^4.2.0"

"@vercel/[email protected]":
version "8.3.7"
resolved "https://registry.yarnpkg.com/@vercel/build-utils/-/build-utils-8.3.7.tgz#4859c1d0c605b3392dd5ba82a49ebae755f2ce4c"
Expand Down Expand Up @@ -1795,15 +1802,16 @@ cliui@^8.0.1:
strip-ansi "^6.0.1"
wrap-ansi "^7.0.0"

cloudflare-worker-adapter@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/cloudflare-worker-adapter/-/cloudflare-worker-adapter-1.3.0.tgz#d89728b82d2c65e4be23192936fa003e056e0273"
integrity sha512-ZA0b5RDR3dZUcOOxxBcmTj59DeoTjBrKNPecG0TxatIe0q1+dwUd1gT1cOfy7WVOzrdh/SF10EfZSZhAm6B1ag==
cloudflare-worker-adapter@^1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/cloudflare-worker-adapter/-/cloudflare-worker-adapter-1.3.2.tgz#29e8f7068fef1e73b2ce5c98c3f8fbdce60a48da"
integrity sha512-tBkvDwuBSdbjxcBbNPnPvzi/E2Ak88P+8GbJLx5a2ls9pYXYsyUc2gkvaYXOykELDwsnDQNLV/98cGk+6tc1KQ==
dependencies:
"@upstash/redis" "^1.34.0"
https-proxy-agent "^7.0.5"
ioredis "^5.4.1"
node-fetch "^3.3.2"
sqlite3 "^5"
sqlite3 "^5.1.7"
toml "^3.0.0"

cluster-key-slot@^1.1.0:
Expand Down Expand Up @@ -1916,6 +1924,11 @@ cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"

crypto-js@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.2.0.tgz#4d931639ecdfd12ff80e8186dba6af2c2e856631"
integrity sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==

cssesc@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee"
Expand Down Expand Up @@ -4520,7 +4533,7 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==

sqlite3@^5:
sqlite3@^5.1.7:
version "5.1.7"
resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-5.1.7.tgz#59ca1053c1ab38647396586edad019b1551041b7"
integrity sha512-GGIyOiFaG+TUra3JIfkI/zGP8yZYLPQ0pl1bH+ODjiX57sPhrLU5sQJn1y9bDKZUFYkX1crlrPfSYt0BKKdkog==
Expand Down

0 comments on commit b387a13

Please sign in to comment.