Skip to content

Commit

Permalink
enhance(backend): check_connect.js で全RedisとDBへの接続を確認するように (#14853)
Browse files Browse the repository at this point in the history
* fix race conditions in check_connect.js

(cherry picked from commit 524ddb9)

* fix

* Update Changelog

---------

Co-authored-by: Hazelnoot <[email protected]>
  • Loading branch information
kakkokari-gtyih and warriordog authored Oct 28, 2024
1 parent 8eb7749 commit f30d190
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
- Fix: リンク切れを修正

### Server
- Enhance: 起動前の疎通チェックで、DBとメイン以外のRedisの疎通確認も行うように
(Based on https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/588)
(Cherry-picked from https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/715)
- Fix: Nested proxy requestsを検出した際にブロックするように
[ghsa-gq5q-c77c-v236](https://github.com/misskey-dev/misskey/security/advisories/ghsa-gq5q-c77c-v236)
- Fix: 招待コードの発行可能な残り数算出に使用すべきロールポリシーの値が違う問題を修正
Expand Down
51 changes: 46 additions & 5 deletions packages/backend/scripts/check_connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,52 @@

import Redis from 'ioredis';
import { loadConfig } from '../built/config.js';
import { createPostgresDataSource } from '../built/postgres.js';

const config = loadConfig();
const redis = new Redis(config.redis);

redis.on('connect', () => redis.disconnect());
redis.on('error', (e) => {
throw e;
});
async function connectToPostgres() {
const source = createPostgresDataSource(config);
await source.initialize();
await source.destroy();
}

async function connectToRedis(redisOptions) {
return await new Promise(async (resolve, reject) => {
const redis = new Redis({
...redisOptions,
lazyConnect: true,
reconnectOnError: false,
showFriendlyErrorStack: true,
});
redis.on('error', e => reject(e));

try {
await redis.connect();
resolve();

} catch (e) {
reject(e);

} finally {
redis.disconnect(false);
}
});
}

// If not all of these are defined, the default one gets reused.
// so we use a Set to only try connecting once to each **uniq** redis.
const promises = Array
.from(new Set([
config.redis,
config.redisForPubsub,
config.redisForJobQueue,
config.redisForTimelines,
config.redisForReactions,
]))
.map(connectToRedis)
.concat([
connectToPostgres()
]);

await Promise.allSettled(promises);

0 comments on commit f30d190

Please sign in to comment.