-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eaf310
commit 84a2a20
Showing
7 changed files
with
375 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,74 @@ | ||
// Strategies: cutoff, linear-remaining, twice-average-remaining | ||
'use strict'; | ||
|
||
// TODO: Proper implementation | ||
module.exports = require('./window-sliding.js'); | ||
// Possible strategies: cutoff, linear-remaining, twice-average-remaining | ||
// Currently implemented strategy: cuttoff | ||
// A more linear strategy can be produced by just dividing the limit by a certain factor and returning shorter intervals with getStartOfNextWindow. | ||
|
||
var _ = require('lodash'); | ||
|
||
|
||
function FixedWindow(options, moreAvailableCb) { | ||
|
||
if (_.isUndefined(options.limit)) { | ||
throw new Error('Please pass the limit parameter to allow window-fixed throttling'); | ||
} | ||
|
||
if (!_.isFunction(options.getStartOfNextWindow)) { | ||
throw new Error('Please pass a function as the getStartOfNextWindow parameter to allow window-fixed throttling'); | ||
} | ||
|
||
this._limit = options.limit; | ||
this._used = _.isFinite(options.used) ? options.used : 0; | ||
this._moreAvailableCb = moreAvailableCb; | ||
this._getStartOfNextWindow = options.getStartOfNextWindow; | ||
|
||
this._prepareNextWindow(); | ||
|
||
} | ||
|
||
FixedWindow.prototype.isAvailable = function (resourceAmount, options) { | ||
return this._used + resourceAmount <= this._limit; | ||
}; | ||
|
||
FixedWindow.prototype.reserve = function (resourceAmount, options) { | ||
|
||
this._used += resourceAmount; | ||
|
||
var self = this; | ||
return function (feedback) { | ||
|
||
if (!_.isUndefined(feedback.limit)) { | ||
|
||
if (!_.isFinite(feedback.limit) || feedback.limit <= 0) { | ||
throw new Error('Please pass a positive number as the limit parameter'); | ||
} | ||
|
||
var limitIncreased = feedback.limit > self._limit; | ||
|
||
self._limit = feedback.limit; | ||
|
||
if (limitIncreased && self._used < self._limit) { | ||
self._moreAvailableCb(); | ||
} | ||
|
||
} | ||
|
||
}; | ||
|
||
}; | ||
|
||
FixedWindow.prototype._prepareNextWindow = function () { | ||
|
||
var time = this._getStartOfNextWindow(); | ||
|
||
var self = this; | ||
setTimeout(function () { | ||
self._used = 0; | ||
self._prepareNextWindow(); | ||
self._moreAvailableCb(); | ||
}, time - (new Date()).getTime()).unref(); | ||
|
||
}; | ||
|
||
|
||
module.exports = FixedWindow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.