-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathshared-lock.js
149 lines (132 loc) · 4.28 KB
/
shared-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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { console_with_prefix, localStorageSupported } from './utils'; // eslint-disable-line camelcase
var logger = console_with_prefix('lock');
/**
* SharedLock: a mutex built on HTML5 localStorage, to ensure that only one browser
* window/tab at a time will be able to access shared resources.
*
* Based on the Alur and Taubenfeld fast lock
* (http://www.cs.rochester.edu/research/synchronization/pseudocode/fastlock.html)
* with an added timeout to ensure there will be eventual progress in the event
* that a window is closed in the middle of the callback.
*
* Implementation based on the original version by David Wolever (https://github.com/wolever)
* at https://gist.github.com/wolever/5fd7573d1ef6166e8f8c4af286a69432.
*
* @example
* const myLock = new SharedLock('some-key');
* myLock.withLock(function() {
* console.log('I hold the mutex!');
* });
*
* @constructor
*/
var SharedLock = function(key, options) {
options = options || {};
this.storageKey = key;
this.storage = options.storage || window.localStorage;
this.pollIntervalMS = options.pollIntervalMS || 100;
this.timeoutMS = options.timeoutMS || 2000;
};
// pass in a specific pid to test contention scenarios; otherwise
// it is chosen randomly for each acquisition attempt
SharedLock.prototype.withLock = function(lockedCB, errorCB, pid) {
if (!pid && typeof errorCB !== 'function') {
pid = errorCB;
errorCB = null;
}
var i = pid || (new Date().getTime() + '|' + Math.random());
var startTime = new Date().getTime();
var key = this.storageKey;
var pollIntervalMS = this.pollIntervalMS;
var timeoutMS = this.timeoutMS;
var storage = this.storage;
var keyX = key + ':X';
var keyY = key + ':Y';
var keyZ = key + ':Z';
var reportError = function(err) {
errorCB && errorCB(err);
};
var delay = function(cb) {
if (new Date().getTime() - startTime > timeoutMS) {
logger.error('Timeout waiting for mutex on ' + key + '; clearing lock. [' + i + ']');
storage.removeItem(keyZ);
storage.removeItem(keyY);
loop();
return;
}
setTimeout(function() {
try {
cb();
} catch(err) {
reportError(err);
}
}, pollIntervalMS * (Math.random() + 0.1));
};
var waitFor = function(predicate, cb) {
if (predicate()) {
cb();
} else {
delay(function() {
waitFor(predicate, cb);
});
}
};
var getSetY = function() {
var valY = storage.getItem(keyY);
if (valY && valY !== i) { // if Y == i then this process already has the lock (useful for test cases)
return false;
} else {
storage.setItem(keyY, i);
if (storage.getItem(keyY) === i) {
return true;
} else {
if (!localStorageSupported(storage, true)) {
throw new Error('localStorage support dropped while acquiring lock');
}
return false;
}
}
};
var loop = function() {
storage.setItem(keyX, i);
waitFor(getSetY, function() {
if (storage.getItem(keyX) === i) {
criticalSection();
return;
}
delay(function() {
if (storage.getItem(keyY) !== i) {
loop();
return;
}
waitFor(function() {
return !storage.getItem(keyZ);
}, criticalSection);
});
});
};
var criticalSection = function() {
storage.setItem(keyZ, '1');
try {
lockedCB();
} finally {
storage.removeItem(keyZ);
if (storage.getItem(keyY) === i) {
storage.removeItem(keyY);
}
if (storage.getItem(keyX) === i) {
storage.removeItem(keyX);
}
}
};
try {
if (localStorageSupported(storage, true)) {
loop();
} else {
throw new Error('localStorage support check failed');
}
} catch(err) {
reportError(err);
}
};
export { SharedLock };