-
Notifications
You must be signed in to change notification settings - Fork 103
/
lock.js
94 lines (82 loc) · 2.34 KB
/
lock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
const log = require('@cumulus/common/log');
const aws = require('@cumulus/common/aws');
const lockPrefix = 'lock';
function delay(t) {
return new Promise((resolve) => {
setTimeout(resolve, t);
});
}
/**
* Check Old Locks
* Checks all locks and removes those older than five minutes
*
* @param {Object} bucket - The AWS S3 bucket with the locks to check
* @param {string} list - The list of locks in the bucket
* @returns {boolean} - Number of locks remaining in bucket
**/
async function checkOldLocks(bucket, list) {
if (list) {
let count = list.length;
let item;
for (item in list) {
const date = list[item].LastModified;
const diff = new Date() - date;
const fiveMinutes = 300000; // 5 * 60 seconds * 1000 milliseconds
if (diff > fiveMinutes) {
aws.S3.delete(bucket, list[item].Key);
count--;
}
}
return count;
}
return 0;
}
/**
* Count Lock
* Counts the number of locks in a bucket
*
* @param {Object} bucket - The AWS S3 bucket to check
* @param {string} pName - The provider name
* @returns {integer} - Number of current locks in the bucket
**/
async function countLock(bucket, pName) {
const list = aws.s3().listObjectsV2({
Bucket: bucket,
Prefix: `${lockPrefix}/${pName}`
}).promise();
const count = checkOldLocks(bucket, list.Contents);
return count;
}
function addLock(bucket, pName, filename) {
return aws.s3().putObject({
Bucket: bucket,
Key: `${lockPrefix}/${pName}/${filename}`,
Body: ''
}).promise();
}
function removeLock(bucket, pName, filename) {
return aws.s3().deleteObject({
Bucket: bucket,
Key: `${lockPrefix}/${pName}/${filename}`
}).promise();
}
async function proceed(bucket, provider, filename, counter = 0) {
// try to proceed for 270 seconds
if (counter > 270) {
return false;
}
const globalConnectionLimit = provider.globalConnectionLimit;
const count = await countLock(bucket, provider.id);
if (count >= globalConnectionLimit) {
log.debug({ provider: provider.id }, 'Reached the connection limit, trying again');
// wait for 5 second and try again
await delay(5000);
return proceed(bucket, provider, filename, counter + 1);
}
// add the lock
await addLock(bucket, provider.id, filename);
return true;
}
module.exports.removeLock = removeLock;
module.exports.proceed = proceed;