From 4b7950458cb46316e72171e5f21fc8312685a879 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 12 Oct 2022 13:40:16 +0800 Subject: [PATCH] Remove Redis client in api --- .github/workflows/run-tests.yaml | 11 ----------- packages/api/package.json | 1 - packages/api/src/server.ts | 22 ---------------------- packages/api/src/util.ts | 12 ------------ packages/api/src/utils/redis.ts | 25 ------------------------- packages/api/test/global-setup.ts | 4 ---- 6 files changed, 75 deletions(-) delete mode 100644 packages/api/src/utils/redis.ts diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index cdf9a1508..3b226c0b0 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -42,16 +42,6 @@ jobs: --health-retries 10 ports: - 9200 - redis: - image: redis - # Set health checks to wait until redis has started - options: >- - --health-cmd "redis-cli ping" - --health-interval 10s - --health-timeout 5s - --health-retries 5 - ports: - - 6379 steps: - uses: actions/checkout@v2 with: @@ -87,7 +77,6 @@ jobs: PG_DB: omnivore_test PG_POOL_MAX: 10 ELASTIC_URL: http://localhost:${{ job.services.elastic.ports[9200] }}/ - REDIS_URL: redis://localhost:${{ job.services.redis.ports[6379] }} build-docker-images: name: Build docker images runs-on: ubuntu-latest diff --git a/packages/api/package.json b/packages/api/package.json index 14cc66423..0631570ed 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -77,7 +77,6 @@ "pg": "^8.3.3", "postgrator": "^4.2.0", "private-ip": "^2.3.3", - "redis": "^4.3.1", "sanitize-html": "^2.3.2", "search-query-parser": "^1.6.0", "snake-case": "^3.0.3", diff --git a/packages/api/src/server.ts b/packages/api/src/server.ts index 98003f432..62dbc8d40 100755 --- a/packages/api/src/server.ts +++ b/packages/api/src/server.ts @@ -46,7 +46,6 @@ import rateLimit from 'express-rate-limit' import { webhooksServiceRouter } from './routers/svc/webhooks' import { integrationsServiceRouter } from './routers/svc/integrations' import { textToSpeechRouter } from './routers/text_to_speech' -import { connectRedisClient, redisClient } from './utils/redis' const PORT = process.env.PORT || 4000 @@ -105,25 +104,6 @@ export const createApp = (): { app.use('/api/', apiLimiter) } - // set user device from request header to Redis - app.use('/api/', async (req, res, next) => { - const client = req.header('X-OmnivoreClient') - const token = - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - req.header('Authorization') || (req.cookies['auth'] as string | undefined) - - if (client && token) { - const key = `client:${token}` - if (!(await redisClient.exists(key))) { - await redisClient.set(key, client, { - EX: 600, // expires in 10 minutes - NX: true, - }) - } - } - next() - }) - // respond healthy to auto-scaler. app.get('/_ah/health', (req, res) => res.sendStatus(200)) @@ -168,8 +148,6 @@ const main = async (): Promise => { await initElasticsearch() - await connectRedisClient() - const { app, apollo, httpServer } = createApp() await apollo.start() diff --git a/packages/api/src/util.ts b/packages/api/src/util.ts index 5d3f52fb0..0ca9ad8b8 100755 --- a/packages/api/src/util.ts +++ b/packages/api/src/util.ts @@ -97,10 +97,6 @@ interface BackendEnv { gcp: { location: string } - redis: { - url?: string - cert?: string - } } /*** @@ -156,8 +152,6 @@ const nullableEnvVars = [ 'AZURE_SPEECH_KEY', 'AZURE_SPEECH_REGION', 'GCP_LOCATION', - 'REDIS_URL', - 'REDIS_CERT', ] // Allow some vars to be null/empty /* If not in GAE and Prod/QA/Demo env (f.e. on localhost/dev env), allow following env vars to be null */ @@ -287,11 +281,6 @@ export function getEnv(): BackendEnv { location: parse('GCP_LOCATION'), } - const redis = { - url: parse('REDIS_URL'), - cert: parse('REDIS_CERT'), - } - return { pg, client, @@ -312,7 +301,6 @@ export function getEnv(): BackendEnv { readwise, azure, gcp, - redis, } } diff --git a/packages/api/src/utils/redis.ts b/packages/api/src/utils/redis.ts deleted file mode 100644 index 6550f435a..000000000 --- a/packages/api/src/utils/redis.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { createClient } from 'redis' -import { env } from '../env' - -export const redisClient = createClient({ - url: env.redis.url, - socket: { - tls: env.redis.url?.startsWith('rediss://'), // rediss:// is the protocol for TLS - cert: env.redis.cert?.replace(/\\n/g, '\n'), // replace \n with new line - rejectUnauthorized: false, // for self-signed certs - connectTimeout: 10000, // 10 seconds - reconnectStrategy(retries: number): number | Error { - if (retries > 10) { - return new Error('Retries exhausted') - } - return 1000 - }, - }, -}) - -export const connectRedisClient = async () => { - redisClient.on('error', (err) => console.error('Redis Client Error', err)) - - await redisClient.connect() - console.log('Redis Client Connected') -} diff --git a/packages/api/test/global-setup.ts b/packages/api/test/global-setup.ts index 2774fa93f..2025c97f7 100644 --- a/packages/api/test/global-setup.ts +++ b/packages/api/test/global-setup.ts @@ -1,7 +1,6 @@ import { createTestConnection } from './db' import { initElasticsearch } from '../src/elastic' import { startApolloServer } from './util' -import { connectRedisClient } from '../src/utils/redis' export const mochaGlobalSetup = async () => { await createTestConnection() @@ -10,9 +9,6 @@ export const mochaGlobalSetup = async () => { await initElasticsearch() console.log('elasticsearch initialized') - await connectRedisClient() - console.log('redis client connected') - await startApolloServer() console.log('apollo server started') }