|
|
const Timer = require('../util/timer'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TaskQueue { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor (maxTokens, refillRate, options = {}) { |
|
|
this._maxTokens = maxTokens; |
|
|
this._refillRate = refillRate; |
|
|
this._pendingTaskRecords = []; |
|
|
this._tokenCount = options.hasOwnProperty('startingTokens') ? options.startingTokens : maxTokens; |
|
|
this._maxTotalCost = options.hasOwnProperty('maxTotalCost') ? options.maxTotalCost : Infinity; |
|
|
this._timer = new Timer(); |
|
|
this._timer.start(); |
|
|
this._timeout = null; |
|
|
this._lastUpdateTime = this._timer.timeElapsed(); |
|
|
|
|
|
this._runTasks = this._runTasks.bind(this); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get length () { |
|
|
return this._pendingTaskRecords.length; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do (task, cost = 1) { |
|
|
if (this._maxTotalCost < Infinity) { |
|
|
const currentTotalCost = this._pendingTaskRecords.reduce((t, r) => t + r.cost, 0); |
|
|
if (currentTotalCost + cost > this._maxTotalCost) { |
|
|
return Promise.reject('Maximum total cost exceeded'); |
|
|
} |
|
|
} |
|
|
const newRecord = { |
|
|
cost |
|
|
}; |
|
|
newRecord.promise = new Promise((resolve, reject) => { |
|
|
newRecord.cancel = () => { |
|
|
reject(new Error('Task canceled')); |
|
|
}; |
|
|
|
|
|
|
|
|
newRecord.wrappedTask = () => { |
|
|
try { |
|
|
resolve(task()); |
|
|
} catch (e) { |
|
|
reject(e); |
|
|
} |
|
|
}; |
|
|
}); |
|
|
this._pendingTaskRecords.push(newRecord); |
|
|
|
|
|
|
|
|
if (this._pendingTaskRecords.length === 1) { |
|
|
this._runTasks(); |
|
|
} |
|
|
|
|
|
return newRecord.promise; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cancel (taskPromise) { |
|
|
const taskIndex = this._pendingTaskRecords.findIndex(r => r.promise === taskPromise); |
|
|
if (taskIndex !== -1) { |
|
|
const [taskRecord] = this._pendingTaskRecords.splice(taskIndex, 1); |
|
|
taskRecord.cancel(); |
|
|
if (taskIndex === 0 && this._pendingTaskRecords.length > 0) { |
|
|
this._runTasks(); |
|
|
} |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cancelAll () { |
|
|
if (this._timeout !== null) { |
|
|
this._timer.clearTimeout(this._timeout); |
|
|
this._timeout = null; |
|
|
} |
|
|
const oldTasks = this._pendingTaskRecords; |
|
|
this._pendingTaskRecords = []; |
|
|
oldTasks.forEach(r => r.cancel()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_refillAndSpend (cost) { |
|
|
this._refill(); |
|
|
return this._spend(cost); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_refill () { |
|
|
const now = this._timer.timeElapsed(); |
|
|
const timeSinceRefill = now - this._lastUpdateTime; |
|
|
if (timeSinceRefill <= 0) return; |
|
|
|
|
|
this._lastUpdateTime = now; |
|
|
this._tokenCount += timeSinceRefill * this._refillRate / 1000; |
|
|
this._tokenCount = Math.min(this._tokenCount, this._maxTokens); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_spend (cost) { |
|
|
if (cost <= this._tokenCount) { |
|
|
this._tokenCount -= cost; |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_runTasks () { |
|
|
if (this._timeout) { |
|
|
this._timer.clearTimeout(this._timeout); |
|
|
this._timeout = null; |
|
|
} |
|
|
for (;;) { |
|
|
const nextRecord = this._pendingTaskRecords.shift(); |
|
|
if (!nextRecord) { |
|
|
|
|
|
return; |
|
|
} |
|
|
if (nextRecord.cost > this._maxTokens) { |
|
|
throw new Error(`Task cost ${nextRecord.cost} is greater than bucket limit ${this._maxTokens}`); |
|
|
} |
|
|
|
|
|
if (this._refillAndSpend(nextRecord.cost)) { |
|
|
nextRecord.wrappedTask(); |
|
|
} else { |
|
|
|
|
|
this._pendingTaskRecords.unshift(nextRecord); |
|
|
const tokensNeeded = Math.max(nextRecord.cost - this._tokenCount, 0); |
|
|
const estimatedWait = Math.ceil(1000 * tokensNeeded / this._refillRate); |
|
|
this._timeout = this._timer.setTimeout(this._runTasks, estimatedWait); |
|
|
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = TaskQueue; |
|
|
|