From f94918bebd83eba9cb8c9d631ac60df500f253f8 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 19 Apr 2019 13:10:49 +0200 Subject: [PATCH] Prevent possible download counter race condition --- server/routes/download.js | 2 +- server/storage/index.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/server/routes/download.js b/server/routes/download.js index 7828c880..006f11c2 100644 --- a/server/routes/download.js +++ b/server/routes/download.js @@ -34,7 +34,7 @@ module.exports = async function(req, res) { if (dl >= dlimit) { await storage.del(id); } else { - await storage.setField(id, 'dl', dl); + await storage.incrementField(id, 'dl'); } } catch (e) { log.info('StorageError:', id); diff --git a/server/storage/index.js b/server/storage/index.js index 1c80f26c..3e46c5c1 100644 --- a/server/storage/index.js +++ b/server/storage/index.js @@ -62,6 +62,10 @@ class DB { this.redis.hset(id, key, value); } + incrementField(id, key, increment = 1) { + this.redis.hincrby(id, key, increment); + } + async del(id) { const filePath = await this.getPrefixedId(id); this.storage.del(filePath);