From 98f28bbf03f891f7a575b802bd883450948b9dd3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 23 Mar 2021 14:54:09 +0900 Subject: [PATCH] fix bug https://github.com/syuilo/misskey/commit/48ea805999c6cb8e900aeaec6edaf68788bd51e0#commitcomment-48584326 --- src/db/redis.ts | 25 +++++++++++-------- src/misc/app-lock.ts | 6 ++--- src/server/api/endpoints/admin/server-info.ts | 4 +-- .../api/endpoints/get-online-users-count.ts | 4 +-- src/server/api/limiter.ts | 6 ++--- src/server/api/service/discord.ts | 10 ++++---- src/server/api/service/github.ts | 10 ++++---- src/server/api/service/twitter.ts | 10 ++++---- src/server/api/streaming.ts | 2 +- src/server/web/index.ts | 4 +-- 10 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/db/redis.ts b/src/db/redis.ts index 7570aa59b..5b6fc05df 100644 --- a/src/db/redis.ts +++ b/src/db/redis.ts @@ -1,16 +1,19 @@ import * as redis from 'redis'; import config from '../config'; -const client = redis.createClient( - config.redis.port, - config.redis.host, - { - password: config.redis.pass, - prefix: config.redis.prefix, - db: config.redis.db || 0 - } -); +export function createConnection() { + return redis.createClient( + config.redis.port, + config.redis.host, + { + password: config.redis.pass, + prefix: config.redis.prefix, + db: config.redis.db || 0 + } + ); +} -client.subscribe(config.host); +export const subsdcriber = createConnection(); +subsdcriber.subscribe(config.host); -export default client; +export const redisClient = createConnection(); diff --git a/src/misc/app-lock.ts b/src/misc/app-lock.ts index 847299b46..9ae2cba47 100644 --- a/src/misc/app-lock.ts +++ b/src/misc/app-lock.ts @@ -1,4 +1,4 @@ -import redis from '../db/redis'; +import { redisClient } from '../db/redis'; import { promisify } from 'util'; /** @@ -7,8 +7,8 @@ import { promisify } from 'util'; const retryDelay = 100; const lock: (key: string, timeout?: number) => Promise<() => void> - = redis - ? promisify(require('redis-lock')(redis, retryDelay)) + = redisClient + ? promisify(require('redis-lock')(redisClient, retryDelay)) : async () => () => { }; /** diff --git a/src/server/api/endpoints/admin/server-info.ts b/src/server/api/endpoints/admin/server-info.ts index 3b48a811e..33e619097 100644 --- a/src/server/api/endpoints/admin/server-info.ts +++ b/src/server/api/endpoints/admin/server-info.ts @@ -2,7 +2,7 @@ import * as os from 'os'; import * as si from 'systeminformation'; import { getConnection } from 'typeorm'; import define from '../../define'; -import redis from '../../../../db/redis'; +import { redisClient } from '../../../../db/redis'; export const meta = { requireCredential: true as const, @@ -115,7 +115,7 @@ export default define(meta, async () => { os: os.platform(), node: process.version, psql: await getConnection().query('SHOW server_version').then(x => x[0].server_version), - redis: redis.server_info.redis_version, + redis: redisClient.server_info.redis_version, cpu: { model: os.cpus()[0].model, cores: os.cpus().length diff --git a/src/server/api/endpoints/get-online-users-count.ts b/src/server/api/endpoints/get-online-users-count.ts index 752110488..520948ac6 100644 --- a/src/server/api/endpoints/get-online-users-count.ts +++ b/src/server/api/endpoints/get-online-users-count.ts @@ -1,5 +1,5 @@ import define from '../define'; -import redis from '../../../db/redis'; +import { redisClient } from '../../../db/redis'; import config from '../../../config'; export const meta = { @@ -13,7 +13,7 @@ export const meta = { export default define(meta, (ps, user) => { return new Promise((res, rej) => { - redis.pubsub('numsub', config.host, (_, x) => { + redisClient.pubsub('numsub', config.host, (_, x) => { res({ count: x[1] }); diff --git a/src/server/api/limiter.ts b/src/server/api/limiter.ts index 48d12d3cc..de4115858 100644 --- a/src/server/api/limiter.ts +++ b/src/server/api/limiter.ts @@ -1,5 +1,5 @@ import * as Limiter from 'ratelimiter'; -import limiterDB from '../../db/redis'; +import { redisClient } from '../../db/redis'; import { IEndpoint } from './endpoints'; import getAcct from '../../misc/acct/render'; import { User } from '../../models/entities/user'; @@ -35,7 +35,7 @@ export default (endpoint: IEndpoint, user: User) => new Promise((ok, reject) => id: `${user.id}:${key}:min`, duration: limitation.minInterval, max: 1, - db: limiterDB! + db: redisClient }); minIntervalLimiter.get((err, info) => { @@ -63,7 +63,7 @@ export default (endpoint: IEndpoint, user: User) => new Promise((ok, reject) => id: `${user.id}:${key}`, duration: limitation.duration, max: limitation.max, - db: limiterDB! + db: redisClient }); limiter.get((err, info) => { diff --git a/src/server/api/service/discord.ts b/src/server/api/service/discord.ts index fce840cde..0ee268e77 100644 --- a/src/server/api/service/discord.ts +++ b/src/server/api/service/discord.ts @@ -4,7 +4,7 @@ import { getJson } from '../../../misc/fetch'; import { OAuth2 } from 'oauth'; import config from '../../../config'; import { publishMainStream } from '../../../services/stream'; -import redis from '../../../db/redis'; +import { redisClient } from '../../../db/redis'; import { v4 as uuid } from 'uuid'; import signin from '../common/signin'; import { fetchMeta } from '../../../misc/fetch-meta'; @@ -96,7 +96,7 @@ router.get('/connect/discord', async ctx => { response_type: 'code' }; - redis.set(userToken, JSON.stringify(params)); + redisClient.set(userToken, JSON.stringify(params)); const oauth2 = await getOAuth2(); ctx.redirect(oauth2!.getAuthorizeUrl(params)); @@ -118,7 +118,7 @@ router.get('/signin/discord', async ctx => { httpOnly: true }); - redis.set(sessid, JSON.stringify(params)); + redisClient.set(sessid, JSON.stringify(params)); const oauth2 = await getOAuth2(); ctx.redirect(oauth2!.getAuthorizeUrl(params)); @@ -145,7 +145,7 @@ router.get('/dc/cb', async ctx => { } const { redirect_uri, state } = await new Promise((res, rej) => { - redis.get(sessid, async (_, state) => { + redisClient.get(sessid, async (_, state) => { res(JSON.parse(state)); }); }); @@ -216,7 +216,7 @@ router.get('/dc/cb', async ctx => { } const { redirect_uri, state } = await new Promise((res, rej) => { - redis.get(userToken, async (_, state) => { + redisClient.get(userToken, async (_, state) => { res(JSON.parse(state)); }); }); diff --git a/src/server/api/service/github.ts b/src/server/api/service/github.ts index 2b10fa02a..7ca95fc11 100644 --- a/src/server/api/service/github.ts +++ b/src/server/api/service/github.ts @@ -4,7 +4,7 @@ import { getJson } from '../../../misc/fetch'; import { OAuth2 } from 'oauth'; import config from '../../../config'; import { publishMainStream } from '../../../services/stream'; -import redis from '../../../db/redis'; +import { redisClient } from '../../../db/redis'; import { v4 as uuid } from 'uuid'; import signin from '../common/signin'; import { fetchMeta } from '../../../misc/fetch-meta'; @@ -95,7 +95,7 @@ router.get('/connect/github', async ctx => { state: uuid() }; - redis.set(userToken, JSON.stringify(params)); + redisClient.set(userToken, JSON.stringify(params)); const oauth2 = await getOath2(); ctx.redirect(oauth2!.getAuthorizeUrl(params)); @@ -116,7 +116,7 @@ router.get('/signin/github', async ctx => { httpOnly: true }); - redis.set(sessid, JSON.stringify(params)); + redisClient.set(sessid, JSON.stringify(params)); const oauth2 = await getOath2(); ctx.redirect(oauth2!.getAuthorizeUrl(params)); @@ -143,7 +143,7 @@ router.get('/gh/cb', async ctx => { } const { redirect_uri, state } = await new Promise((res, rej) => { - redis.get(sessid, async (_, state) => { + redisClient.get(sessid, async (_, state) => { res(JSON.parse(state)); }); }); @@ -194,7 +194,7 @@ router.get('/gh/cb', async ctx => { } const { redirect_uri, state } = await new Promise((res, rej) => { - redis.get(userToken, async (_, state) => { + redisClient.get(userToken, async (_, state) => { res(JSON.parse(state)); }); }); diff --git a/src/server/api/service/twitter.ts b/src/server/api/service/twitter.ts index 97e9d3a7f..7985042d2 100644 --- a/src/server/api/service/twitter.ts +++ b/src/server/api/service/twitter.ts @@ -2,7 +2,7 @@ import * as Koa from 'koa'; import * as Router from '@koa/router'; import { v4 as uuid } from 'uuid'; import autwh from 'autwh'; -import redis from '../../../db/redis'; +import { redisClient } from '../../../db/redis'; import { publishMainStream } from '../../../services/stream'; import config from '../../../config'; import signin from '../common/signin'; @@ -89,7 +89,7 @@ router.get('/connect/twitter', async ctx => { const twAuth = await getTwAuth(); const twCtx = await twAuth!.begin(); - redis.set(userToken, JSON.stringify(twCtx)); + redisClient.set(userToken, JSON.stringify(twCtx)); ctx.redirect(twCtx.url); }); @@ -99,7 +99,7 @@ router.get('/signin/twitter', async ctx => { const sessid = uuid(); - redis.set(sessid, JSON.stringify(twCtx)); + redisClient.set(sessid, JSON.stringify(twCtx)); ctx.cookies.set('signin_with_twitter_sid', sessid, { path: '/', @@ -124,7 +124,7 @@ router.get('/tw/cb', async ctx => { } const get = new Promise((res, rej) => { - redis.get(sessid, async (_, twCtx) => { + redisClient.get(sessid, async (_, twCtx) => { res(twCtx); }); }); @@ -153,7 +153,7 @@ router.get('/tw/cb', async ctx => { } const get = new Promise((res, rej) => { - redis.get(userToken, async (_, twCtx) => { + redisClient.get(userToken, async (_, twCtx) => { res(twCtx); }); }); diff --git a/src/server/api/streaming.ts b/src/server/api/streaming.ts index 4ec0c5137..81b83edcf 100644 --- a/src/server/api/streaming.ts +++ b/src/server/api/streaming.ts @@ -5,7 +5,7 @@ import MainStreamConnection from './stream'; import { ParsedUrlQuery } from 'querystring'; import authenticate from './authenticate'; import { EventEmitter } from 'events'; -import redisClient from '../../db/redis'; +import { subsdcriber as redisClient } from '../../db/redis'; module.exports = (server: http.Server) => { // Init websocket server diff --git a/src/server/web/index.ts b/src/server/web/index.ts index ea356206f..655862743 100644 --- a/src/server/web/index.ts +++ b/src/server/web/index.ts @@ -21,7 +21,7 @@ import { Users, Notes, Emojis, UserProfiles, Pages, Channels, Clips } from '../. import parseAcct from '../../misc/acct/parse'; import { getNoteSummary } from '../../misc/get-note-summary'; import { getConnection } from 'typeorm'; -import redis from '../../db/redis'; +import { redisClient } from '../../db/redis'; import locales = require('../../../locales'); const markdown = MarkdownIt({ @@ -379,7 +379,7 @@ router.get('/info', async ctx => { os: os.platform(), node: process.version, psql: await getConnection().query('SHOW server_version').then(x => x[0].server_version), - redis: redis.server_info.redis_version, + redis: redisClient.server_info.redis_version, cpu: { model: os.cpus()[0].model, cores: os.cpus().length