From 01ba5f48b8973fef557cbee630c1e88a90878d3a Mon Sep 17 00:00:00 2001 From: GameDog9988 Date: Tue, 3 Oct 2023 12:33:28 -0400 Subject: [PATCH 1/6] feat: use REDIS_PRIVATE_URL --- apps/server/src/redis.ts | 9 ++++++++- turbo.json | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/server/src/redis.ts b/apps/server/src/redis.ts index 0fd14995..5a4ca267 100644 --- a/apps/server/src/redis.ts +++ b/apps/server/src/redis.ts @@ -1,6 +1,13 @@ import Redis from "ioredis"; -const redis = new Redis(process.env.REDIS_URL ?? ""); +const redis = new Redis( + process.env.REDIS_PRIVATE_URL ?? process.env.REDIS_URL ?? "", + { + tls: { + rejectUnauthorized: false, + }, + } +); // Listen to connection events to prevent TCP Issue // https://github.com/redis/ioredis/issues/1203 diff --git a/turbo.json b/turbo.json index 231e6981..7435a4f4 100644 --- a/turbo.json +++ b/turbo.json @@ -17,6 +17,7 @@ "DATABASE_URL", "POSTGRES_URL", "SESSION_COOKIE_DOMAIN", + "REDIS_PRIVATE_URL", "REDIS_URL", "PORT", "NODE_ENV", From ebc8e56862775f802bb06b9fbeec780190c07af3 Mon Sep 17 00:00:00 2001 From: GameDog9988 Date: Tue, 3 Oct 2023 13:25:12 -0400 Subject: [PATCH 2/6] fix: remove tls option --- apps/server/src/redis.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/apps/server/src/redis.ts b/apps/server/src/redis.ts index 5a4ca267..831eac50 100644 --- a/apps/server/src/redis.ts +++ b/apps/server/src/redis.ts @@ -1,12 +1,7 @@ import Redis from "ioredis"; const redis = new Redis( - process.env.REDIS_PRIVATE_URL ?? process.env.REDIS_URL ?? "", - { - tls: { - rejectUnauthorized: false, - }, - } + process.env.REDIS_PRIVATE_URL ?? process.env.REDIS_URL ?? "" ); // Listen to connection events to prevent TCP Issue From e6eab079f47385fefacdf2078d930aa25866cdd6 Mon Sep 17 00:00:00 2001 From: Alexander Grattan Date: Tue, 3 Oct 2023 14:10:26 -0400 Subject: [PATCH 3/6] test: empty commit From dc56a34070e70abf64bf6d6de75866f5ffbe10d0 Mon Sep 17 00:00:00 2001 From: Alexander Grattan Date: Tue, 3 Oct 2023 14:40:29 -0400 Subject: [PATCH 4/6] fix: remove redis private URL --- apps/server/src/redis.ts | 2 +- turbo.json | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/server/src/redis.ts b/apps/server/src/redis.ts index 831eac50..70988ee6 100644 --- a/apps/server/src/redis.ts +++ b/apps/server/src/redis.ts @@ -1,7 +1,7 @@ import Redis from "ioredis"; const redis = new Redis( - process.env.REDIS_PRIVATE_URL ?? process.env.REDIS_URL ?? "" +process.env.REDIS_URL ?? "" ); // Listen to connection events to prevent TCP Issue diff --git a/turbo.json b/turbo.json index 7435a4f4..231e6981 100644 --- a/turbo.json +++ b/turbo.json @@ -17,7 +17,6 @@ "DATABASE_URL", "POSTGRES_URL", "SESSION_COOKIE_DOMAIN", - "REDIS_PRIVATE_URL", "REDIS_URL", "PORT", "NODE_ENV", From 1ce38afe98f025c5bd4becac334f45968c0cbcb4 Mon Sep 17 00:00:00 2001 From: Alexander Grattan Date: Tue, 3 Oct 2023 16:44:06 -0400 Subject: [PATCH 5/6] feat: use upstash redis instead of vercel kv --- apps/client/package.json | 2 +- apps/client/src/middleware.ts | 6 ++++-- pnpm-lock.yaml | 23 +++-------------------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/apps/client/package.json b/apps/client/package.json index 8b804c7e..4c4e1587 100644 --- a/apps/client/package.json +++ b/apps/client/package.json @@ -14,8 +14,8 @@ "@sentry/nextjs": "^7.72.0", "@tanstack/react-query": "^4.35.3", "@upstash/ratelimit": "^0.4.4", + "@upstash/redis": "^1.22.1", "@vercel/analytics": "^1.0.2", - "@vercel/kv": "^0.2.3", "@xstate/react": "^3.2.2", "autoprefixer": "10.4.16", "clsx": "^2.0.0", diff --git a/apps/client/src/middleware.ts b/apps/client/src/middleware.ts index 7acf4537..1f18f9af 100644 --- a/apps/client/src/middleware.ts +++ b/apps/client/src/middleware.ts @@ -1,11 +1,13 @@ import { NextRequest, NextResponse } from "next/server"; import { Ratelimit } from "@upstash/ratelimit"; -import { kv } from "@vercel/kv"; +import { Redis } from "@upstash/redis"; const ratelimit = new Ratelimit({ - redis: kv, + redis: Redis.fromEnv(), // 5 requests from the same IP in 10 seconds limiter: Ratelimit.slidingWindow(5, "10 s"), + analytics: true, + prefix: "@upstash/ratelimit", }); export const config = { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9de2baef..0aa1eb81 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,12 +35,12 @@ importers: '@upstash/ratelimit': specifier: ^0.4.4 version: 0.4.4 + '@upstash/redis': + specifier: ^1.22.1 + version: 1.22.1 '@vercel/analytics': specifier: ^1.0.2 version: 1.0.2 - '@vercel/kv': - specifier: ^0.2.3 - version: 0.2.3 '@xstate/react': specifier: ^3.2.2 version: 3.2.2(@types/react@18.2.23)(react@18.2.0)(xstate@4.38.2) @@ -2006,14 +2006,6 @@ packages: - encoding dev: false - /@upstash/redis@1.22.0: - resolution: {integrity: sha512-sXoJDoEqqik0HbrNE7yRWckOySEFsoBxfRdCgOqkc0w6py19ZZG50SpGkDDEUXSnBqP8VgGYXhWAiBpqxrt5oA==} - dependencies: - isomorphic-fetch: 3.0.0 - transitivePeerDependencies: - - encoding - dev: false - /@upstash/redis@1.22.1: resolution: {integrity: sha512-7ec2eCMkVxZzuHNb+hPKonX4b/Pu0BdDeSBsEy+jKIqiweXzCs5Dpu9642vJgf57YnEsfwgXnQMVEataarvyeQ==} dependencies: @@ -2026,15 +2018,6 @@ packages: resolution: {integrity: sha512-BZFxVrv24VbNNl5xMxqUojQIegEeXMI6rX3rg1uVLYUEXsuKNBSAEQf4BWEcjQDp/8aYJOj6m8V4PUA3x/cxgg==} dev: false - /@vercel/kv@0.2.3: - resolution: {integrity: sha512-Wq1+EsRBQmvLlcqCZeYVg1MAARWrnETgLe3Sy3UCqG+zg7LThpkt0YHZe1NN3Aj4IRmCKQamotWrLDdEx+ZB3w==} - engines: {node: '>=14.6'} - dependencies: - '@upstash/redis': 1.22.0 - transitivePeerDependencies: - - encoding - dev: false - /@xstate/react@3.2.2(@types/react@18.2.23)(react@18.2.0)(xstate@4.38.2): resolution: {integrity: sha512-feghXWLedyq8JeL13yda3XnHPZKwYDN5HPBLykpLeuNpr9178tQd2/3d0NrH6gSd0sG5mLuLeuD+ck830fgzLQ==} peerDependencies: From a33c280718a89cac33271395b6c6688b107fa882 Mon Sep 17 00:00:00 2001 From: Alexander Grattan Date: Tue, 3 Oct 2023 16:45:59 -0400 Subject: [PATCH 6/6] fix: prettier for redis --- apps/server/src/redis.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/server/src/redis.ts b/apps/server/src/redis.ts index 70988ee6..0fd14995 100644 --- a/apps/server/src/redis.ts +++ b/apps/server/src/redis.ts @@ -1,8 +1,6 @@ import Redis from "ioredis"; -const redis = new Redis( -process.env.REDIS_URL ?? "" -); +const redis = new Redis(process.env.REDIS_URL ?? ""); // Listen to connection events to prevent TCP Issue // https://github.com/redis/ioredis/issues/1203