mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 07:24:13 +00:00 
			
		
		
		
	merge: fix race conditions in check_connect.js (!715)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/715 Approved-by: Marie <github@yuugi.dev> Approved-by: dakkar <dakkar@thenautilus.net>
This commit is contained in:
		
						commit
						d72c40d157
					
				
					 1 changed files with 39 additions and 17 deletions
				
			
		| 
						 | 
				
			
			@ -12,27 +12,49 @@ const config = loadConfig();
 | 
			
		|||
// createPostgresDataSource handels primaries and replicas automatically.
 | 
			
		||||
// usually, it only opens connections first use, so we force it using
 | 
			
		||||
// .initialize()
 | 
			
		||||
createPostgresDataSource(config)
 | 
			
		||||
	.initialize()
 | 
			
		||||
	.then(c => { c.destroy() })
 | 
			
		||||
	.catch(e => { throw e });
 | 
			
		||||
 | 
			
		||||
async function connectToPostgres(){
 | 
			
		||||
	const source = createPostgresDataSource(config);
 | 
			
		||||
	await source.initialize();
 | 
			
		||||
	await source.destroy();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Connect to all redis servers
 | 
			
		||||
function connectToRedis(redisOptions) {
 | 
			
		||||
	const redis = new Redis(redisOptions);
 | 
			
		||||
	redis.on('connect', () => redis.disconnect());
 | 
			
		||||
	redis.on('error', (e) => {
 | 
			
		||||
		throw e;
 | 
			
		||||
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.
 | 
			
		||||
(new Set([
 | 
			
		||||
const promises = Array
 | 
			
		||||
	.from(new Set([
 | 
			
		||||
		config.redis,
 | 
			
		||||
		config.redisForPubsub,
 | 
			
		||||
		config.redisForJobQueue,
 | 
			
		||||
		config.redisForTimelines,
 | 
			
		||||
		config.redisForReactions,
 | 
			
		||||
])).forEach(connectToRedis);
 | 
			
		||||
	]))
 | 
			
		||||
	.map(connectToRedis)
 | 
			
		||||
	.concat([
 | 
			
		||||
		connectToPostgres()
 | 
			
		||||
	]);
 | 
			
		||||
 | 
			
		||||
await Promise.allSettled(promises);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue