1 | "use strict";
|
2 |
|
3 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
4 |
|
5 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
6 |
|
7 | var BottleneckError, LocalDatastore, parser;
|
8 | parser = require("./parser");
|
9 | BottleneckError = require("./BottleneckError");
|
10 | LocalDatastore = class LocalDatastore {
|
11 | constructor(instance, storeOptions, storeInstanceOptions) {
|
12 | this.instance = instance;
|
13 | this.storeOptions = storeOptions;
|
14 | this.clientId = this.instance._randomIndex();
|
15 | parser.load(storeInstanceOptions, storeInstanceOptions, this);
|
16 | this._nextRequest = this._lastReservoirRefresh = this._lastReservoirIncrease = Date.now();
|
17 | this._running = 0;
|
18 | this._done = 0;
|
19 | this._unblockTime = 0;
|
20 | this.ready = this.Promise.resolve();
|
21 | this.clients = {};
|
22 |
|
23 | this._startHeartbeat();
|
24 | }
|
25 |
|
26 | _startHeartbeat() {
|
27 | var base;
|
28 |
|
29 | if (this.heartbeat == null && (this.storeOptions.reservoirRefreshInterval != null && this.storeOptions.reservoirRefreshAmount != null || this.storeOptions.reservoirIncreaseInterval != null && this.storeOptions.reservoirIncreaseAmount != null)) {
|
30 | return typeof (base = this.heartbeat = setInterval(() => {
|
31 | var amount, incr, maximum, now, reservoir;
|
32 | now = Date.now();
|
33 |
|
34 | if (this.storeOptions.reservoirRefreshInterval != null && now >= this._lastReservoirRefresh + this.storeOptions.reservoirRefreshInterval) {
|
35 | this._lastReservoirRefresh = now;
|
36 | this.storeOptions.reservoir = this.storeOptions.reservoirRefreshAmount;
|
37 |
|
38 | this.instance._drainAll(this.computeCapacity());
|
39 | }
|
40 |
|
41 | if (this.storeOptions.reservoirIncreaseInterval != null && now >= this._lastReservoirIncrease + this.storeOptions.reservoirIncreaseInterval) {
|
42 | var _this$storeOptions = this.storeOptions;
|
43 | amount = _this$storeOptions.reservoirIncreaseAmount;
|
44 | maximum = _this$storeOptions.reservoirIncreaseMaximum;
|
45 | reservoir = _this$storeOptions.reservoir;
|
46 | this._lastReservoirIncrease = now;
|
47 | incr = maximum != null ? Math.min(amount, maximum - reservoir) : amount;
|
48 |
|
49 | if (incr > 0) {
|
50 | this.storeOptions.reservoir += incr;
|
51 | return this.instance._drainAll(this.computeCapacity());
|
52 | }
|
53 | }
|
54 | }, this.heartbeatInterval)).unref === "function" ? base.unref() : void 0;
|
55 | } else {
|
56 | return clearInterval(this.heartbeat);
|
57 | }
|
58 | }
|
59 |
|
60 | __publish__(message) {
|
61 | var _this = this;
|
62 |
|
63 | return _asyncToGenerator(function* () {
|
64 | yield _this.yieldLoop();
|
65 | return _this.instance.Events.trigger("message", message.toString());
|
66 | })();
|
67 | }
|
68 |
|
69 | __disconnect__(flush) {
|
70 | var _this2 = this;
|
71 |
|
72 | return _asyncToGenerator(function* () {
|
73 | yield _this2.yieldLoop();
|
74 | clearInterval(_this2.heartbeat);
|
75 | return _this2.Promise.resolve();
|
76 | })();
|
77 | }
|
78 |
|
79 | yieldLoop(t = 0) {
|
80 | return new this.Promise(function (resolve, reject) {
|
81 | return setTimeout(resolve, t);
|
82 | });
|
83 | }
|
84 |
|
85 | computePenalty() {
|
86 | var ref;
|
87 | return (ref = this.storeOptions.penalty) != null ? ref : 15 * this.storeOptions.minTime || 5000;
|
88 | }
|
89 |
|
90 | __updateSettings__(options) {
|
91 | var _this3 = this;
|
92 |
|
93 | return _asyncToGenerator(function* () {
|
94 | yield _this3.yieldLoop();
|
95 | parser.overwrite(options, options, _this3.storeOptions);
|
96 |
|
97 | _this3._startHeartbeat();
|
98 |
|
99 | _this3.instance._drainAll(_this3.computeCapacity());
|
100 |
|
101 | return true;
|
102 | })();
|
103 | }
|
104 |
|
105 | __running__() {
|
106 | var _this4 = this;
|
107 |
|
108 | return _asyncToGenerator(function* () {
|
109 | yield _this4.yieldLoop();
|
110 | return _this4._running;
|
111 | })();
|
112 | }
|
113 |
|
114 | __queued__() {
|
115 | var _this5 = this;
|
116 |
|
117 | return _asyncToGenerator(function* () {
|
118 | yield _this5.yieldLoop();
|
119 | return _this5.instance.queued();
|
120 | })();
|
121 | }
|
122 |
|
123 | __done__() {
|
124 | var _this6 = this;
|
125 |
|
126 | return _asyncToGenerator(function* () {
|
127 | yield _this6.yieldLoop();
|
128 | return _this6._done;
|
129 | })();
|
130 | }
|
131 |
|
132 | __groupCheck__(time) {
|
133 | var _this7 = this;
|
134 |
|
135 | return _asyncToGenerator(function* () {
|
136 | yield _this7.yieldLoop();
|
137 | return _this7._nextRequest + _this7.timeout < time;
|
138 | })();
|
139 | }
|
140 |
|
141 | computeCapacity() {
|
142 | var maxConcurrent, reservoir;
|
143 | var _this$storeOptions2 = this.storeOptions;
|
144 | maxConcurrent = _this$storeOptions2.maxConcurrent;
|
145 | reservoir = _this$storeOptions2.reservoir;
|
146 |
|
147 | if (maxConcurrent != null && reservoir != null) {
|
148 | return Math.min(maxConcurrent - this._running, reservoir);
|
149 | } else if (maxConcurrent != null) {
|
150 | return maxConcurrent - this._running;
|
151 | } else if (reservoir != null) {
|
152 | return reservoir;
|
153 | } else {
|
154 | return null;
|
155 | }
|
156 | }
|
157 |
|
158 | conditionsCheck(weight) {
|
159 | var capacity;
|
160 | capacity = this.computeCapacity();
|
161 | return capacity == null || weight <= capacity;
|
162 | }
|
163 |
|
164 | __incrementReservoir__(incr) {
|
165 | var _this8 = this;
|
166 |
|
167 | return _asyncToGenerator(function* () {
|
168 | var reservoir;
|
169 | yield _this8.yieldLoop();
|
170 | reservoir = _this8.storeOptions.reservoir += incr;
|
171 |
|
172 | _this8.instance._drainAll(_this8.computeCapacity());
|
173 |
|
174 | return reservoir;
|
175 | })();
|
176 | }
|
177 |
|
178 | __currentReservoir__() {
|
179 | var _this9 = this;
|
180 |
|
181 | return _asyncToGenerator(function* () {
|
182 | yield _this9.yieldLoop();
|
183 | return _this9.storeOptions.reservoir;
|
184 | })();
|
185 | }
|
186 |
|
187 | isBlocked(now) {
|
188 | return this._unblockTime >= now;
|
189 | }
|
190 |
|
191 | check(weight, now) {
|
192 | return this.conditionsCheck(weight) && this._nextRequest - now <= 0;
|
193 | }
|
194 |
|
195 | __check__(weight) {
|
196 | var _this10 = this;
|
197 |
|
198 | return _asyncToGenerator(function* () {
|
199 | var now;
|
200 | yield _this10.yieldLoop();
|
201 | now = Date.now();
|
202 | return _this10.check(weight, now);
|
203 | })();
|
204 | }
|
205 |
|
206 | __register__(index, weight, expiration) {
|
207 | var _this11 = this;
|
208 |
|
209 | return _asyncToGenerator(function* () {
|
210 | var now, wait;
|
211 | yield _this11.yieldLoop();
|
212 | now = Date.now();
|
213 |
|
214 | if (_this11.conditionsCheck(weight)) {
|
215 | _this11._running += weight;
|
216 |
|
217 | if (_this11.storeOptions.reservoir != null) {
|
218 | _this11.storeOptions.reservoir -= weight;
|
219 | }
|
220 |
|
221 | wait = Math.max(_this11._nextRequest - now, 0);
|
222 | _this11._nextRequest = now + wait + _this11.storeOptions.minTime;
|
223 | return {
|
224 | success: true,
|
225 | wait,
|
226 | reservoir: _this11.storeOptions.reservoir
|
227 | };
|
228 | } else {
|
229 | return {
|
230 | success: false
|
231 | };
|
232 | }
|
233 | })();
|
234 | }
|
235 |
|
236 | strategyIsBlock() {
|
237 | return this.storeOptions.strategy === 3;
|
238 | }
|
239 |
|
240 | __submit__(queueLength, weight) {
|
241 | var _this12 = this;
|
242 |
|
243 | return _asyncToGenerator(function* () {
|
244 | var blocked, now, reachedHWM;
|
245 | yield _this12.yieldLoop();
|
246 |
|
247 | if (_this12.storeOptions.maxConcurrent != null && weight > _this12.storeOptions.maxConcurrent) {
|
248 | throw new BottleneckError(`Impossible to add a job having a weight of ${weight} to a limiter having a maxConcurrent setting of ${_this12.storeOptions.maxConcurrent}`);
|
249 | }
|
250 |
|
251 | now = Date.now();
|
252 | reachedHWM = _this12.storeOptions.highWater != null && queueLength === _this12.storeOptions.highWater && !_this12.check(weight, now);
|
253 | blocked = _this12.strategyIsBlock() && (reachedHWM || _this12.isBlocked(now));
|
254 |
|
255 | if (blocked) {
|
256 | _this12._unblockTime = now + _this12.computePenalty();
|
257 | _this12._nextRequest = _this12._unblockTime + _this12.storeOptions.minTime;
|
258 |
|
259 | _this12.instance._dropAllQueued();
|
260 | }
|
261 |
|
262 | return {
|
263 | reachedHWM,
|
264 | blocked,
|
265 | strategy: _this12.storeOptions.strategy
|
266 | };
|
267 | })();
|
268 | }
|
269 |
|
270 | __free__(index, weight) {
|
271 | var _this13 = this;
|
272 |
|
273 | return _asyncToGenerator(function* () {
|
274 | yield _this13.yieldLoop();
|
275 | _this13._running -= weight;
|
276 | _this13._done += weight;
|
277 |
|
278 | _this13.instance._drainAll(_this13.computeCapacity());
|
279 |
|
280 | return {
|
281 | running: _this13._running
|
282 | };
|
283 | })();
|
284 | }
|
285 |
|
286 | };
|
287 | module.exports = LocalDatastore; |
\ | No newline at end of file |