From 415b48856f63223e9912dc3a654d72c1a8b981f9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 25 Mar 2021 19:22:08 +0000 Subject: [PATCH] Fixing an issue with bucket creation, sometimes many calls will be made at once for checking bucket exists, just manage this by storing the state of promise. --- .../server/src/utilities/fileSystem/utilities.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/server/src/utilities/fileSystem/utilities.js b/packages/server/src/utilities/fileSystem/utilities.js index 98f856bd37..b586cfcfe5 100644 --- a/packages/server/src/utilities/fileSystem/utilities.js +++ b/packages/server/src/utilities/fileSystem/utilities.js @@ -13,6 +13,10 @@ const { ObjectStoreBuckets } = require("../../constants") const uuid = require("uuid/v4") const streamPipeline = promisify(stream.pipeline) +// use this as a temporary store of buckets that are being created +const STATE = { + bucketCreationPromises: {}, +} const CONTENT_TYPE_MAP = { html: "text/html", @@ -81,13 +85,18 @@ exports.makeSureBucketExists = async (client, bucketName) => { }) .promise() } catch (err) { - // bucket doesn't exist create it - if (err.statusCode === 404) { - await client + const promises = STATE.bucketCreationPromises + if (promises[bucketName]) { + await promises[bucketName] + } else if (err.statusCode === 404) { + // bucket doesn't exist create it + promises[bucketName] = client .createBucket({ Bucket: bucketName, }) .promise() + await promises[bucketName] + delete promises[bucketName] // public buckets are quite hidden in the system, make sure // no bucket is set accidentally if (PUBLIC_BUCKETS.includes(bucketName)) {