screwdriver-api 4.1.153 → 4.1.154
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -416,6 +416,8 @@ redisLock:
|
|
|
416
416
|
retryDelay: REDLOCK_RETRY_DELAY
|
|
417
417
|
# the maximum time in milliseconds randomly added to retries
|
|
418
418
|
retryJitter: REDLOCK_RETRY_JITTER
|
|
419
|
+
# the maximum time in milliseconds living of a key that has a timeout
|
|
420
|
+
ttl: REDLOCK_TTL
|
|
419
421
|
# Configuration of the redis instance
|
|
420
422
|
redisConnection:
|
|
421
423
|
host: REDLOCK_REDIS_HOST
|
package/config/default.yaml
CHANGED
|
@@ -348,6 +348,8 @@ redisLock:
|
|
|
348
348
|
retryDelay: 500
|
|
349
349
|
# the maximum time in milliseconds randomly added to retries
|
|
350
350
|
retryJitter: 200
|
|
351
|
+
# the maximum time in milliseconds living of a key that has a timeout
|
|
352
|
+
ttl: 20000
|
|
351
353
|
# Configuration of the redis instance
|
|
352
354
|
redisConnection:
|
|
353
355
|
host: "127.0.0.1"
|
package/package.json
CHANGED
package/plugins/lock.js
CHANGED
|
@@ -5,19 +5,36 @@ const Redlock = require('redlock');
|
|
|
5
5
|
const config = require('config');
|
|
6
6
|
const logger = require('screwdriver-logger');
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* parse value to Boolean
|
|
10
|
+
* @method parseBool
|
|
11
|
+
* @param {(Boolean|String)} value
|
|
12
|
+
* @return {Boolean}
|
|
13
|
+
*/
|
|
14
|
+
function parseBool(value) {
|
|
15
|
+
if (typeof value === 'boolean') {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// True values refers to https://yaml.org/type/bool.html
|
|
20
|
+
return ['on', 'true', 'yes', 'y'].includes(String(value).toLowerCase());
|
|
21
|
+
}
|
|
22
|
+
|
|
8
23
|
class Lock {
|
|
9
24
|
/**
|
|
10
25
|
* Constructor
|
|
11
26
|
*/
|
|
12
27
|
constructor() {
|
|
13
|
-
if (config.get('redisLock.enabled')) {
|
|
28
|
+
if (parseBool(config.get('redisLock.enabled'))) {
|
|
14
29
|
const redisLockConfig = config.get('redisLock.options');
|
|
15
30
|
const connectionDetails = {
|
|
16
31
|
host: redisLockConfig.redisConnection.host,
|
|
17
32
|
options: {
|
|
18
33
|
password:
|
|
19
34
|
redisLockConfig.redisConnection.options && redisLockConfig.redisConnection.options.password,
|
|
20
|
-
tls: redisLockConfig.redisConnection.options
|
|
35
|
+
tls: redisLockConfig.redisConnection.options
|
|
36
|
+
? parseBool(redisLockConfig.redisConnection.options.tls)
|
|
37
|
+
: false
|
|
21
38
|
},
|
|
22
39
|
port: redisLockConfig.redisConnection.port
|
|
23
40
|
};
|
|
@@ -25,11 +42,12 @@ class Lock {
|
|
|
25
42
|
try {
|
|
26
43
|
this.redis = new Redis(connectionDetails.port, connectionDetails.host, connectionDetails.options);
|
|
27
44
|
this.redlock = new Redlock([this.redis], {
|
|
28
|
-
driftFactor: redisLockConfig.driftFactor,
|
|
29
|
-
retryCount: redisLockConfig.retryCount,
|
|
30
|
-
retryDelay: redisLockConfig.retryDelay,
|
|
31
|
-
retryJitter: redisLockConfig.retryJitter
|
|
45
|
+
driftFactor: parseFloat(redisLockConfig.driftFactor),
|
|
46
|
+
retryCount: parseInt(redisLockConfig.retryCount, 10),
|
|
47
|
+
retryDelay: parseFloat(redisLockConfig.retryDelay),
|
|
48
|
+
retryJitter: parseFloat(redisLockConfig.retryJitter)
|
|
32
49
|
});
|
|
50
|
+
this.ttl = parseFloat(redisLockConfig.ttl);
|
|
33
51
|
} catch (err) {
|
|
34
52
|
logger.error('Failed to initialize redlock', err);
|
|
35
53
|
}
|
|
@@ -44,7 +62,7 @@ class Lock {
|
|
|
44
62
|
* @param {Number} ttl maximum lock duration in milliseconds
|
|
45
63
|
* @returns {Promise}
|
|
46
64
|
*/
|
|
47
|
-
async lock(resource, ttl =
|
|
65
|
+
async lock(resource, ttl = this.ttl) {
|
|
48
66
|
if (this.redlock) {
|
|
49
67
|
try {
|
|
50
68
|
const lock = await this.redlock.lock(resource, ttl);
|