-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-http-rate-limiter.js
144 lines (127 loc) · 4.25 KB
/
ng-http-rate-limiter.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
(function(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
// CommonJS
if (typeof angular === 'undefined') {
module.exports = factory(require('angular'));
} else {
module.exports = factory(angular);
}
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['angular'], factory);
} else {
// Global Variables
factory(root.angular);
}
})(this, function(angular) {
"use strict";
angular.module("ngHttpRateLimiter", [])
.factory("ngHttpRateLimiterQueue", ["$timeout", "$window", function rateLimiterQueue($timeout, $window) {
function RateLimiterQueue(pattern, requestLimit, timeLimit) {
this.pattern = pattern;
this.requestLimit = requestLimit;
this.timeLimit = timeLimit || 1000;
this.queue = [];
this.count = 0; // number of requests in the current period
this.lastRequestTime = $window.performance.now();
this.timer = undefined;
}
angular.extend(RateLimiterQueue.prototype, {
matches: function(url) {
return url.match(this.pattern);
},
// Add a request to the queue, acting on it immediately if possible
add: function(config, deferred) {
this.queue.push({
config: config,
deferred: deferred
});
this.processQueue();
},
// Schedule processing the queue at the next soonest time.
scheduleProcessing: function() {
if (!angular.isDefined(this.interval)) {
var nextTryIn = Math.max(0, this.timeLimit - ($window.performance.now() - this.lastRequestTime));
this.timer = $timeout(function() {
this.timer = undefined;
this.processQueue();
}.bind(this), nextTryIn);
}
},
processQueue: function() {
while (this.queue.length) {
if (this.canProcess()) {
var request = this.queue.shift();
request.deferred.resolve(request.config);
} else {
this.scheduleProcessing();
return;
}
}
},
// Returns whether or not we can process a request right now. Mutates state.
canProcess: function() {
var currentRequestTime = $window.performance.now();
var timeSinceLastRequest = currentRequestTime - this.lastRequestTime;
if (timeSinceLastRequest >= this.timeLimit) {
this.lastRequestTime = currentRequestTime;
this.count = 0;
}
if (this.count < this.requestLimit) {
this.count++;
return true;
} else {
return false;
}
}
});
function RateLimiterQueueFactory(pattern, requestLimit, timeLimit) {
return new RateLimiterQueue(pattern, requestLimit, timeLimit);
}
return RateLimiterQueueFactory;
}])
.provider("ngHttpRateLimiterConfig", function rateLimiterConfig() {
var limiterConfig = [];
var limiters = [];
this.addLimiter = function(pattern, requestLimit, timeLimit) {
limiterConfig.push({
pattern: pattern,
requestLimit: requestLimit,
timeLimit: timeLimit
});
};
this.$get = ["ngHttpRateLimiterQueue", function(rateLimiterQueue) {
while (limiterConfig.length) {
var config = limiterConfig.shift();
limiters.push(rateLimiterQueue(config.pattern, config.requestLimit, config.timeLimit));
}
return {
getLimiters: function() {
return limiters;
}
};
}];
})
.factory("ngHttpRateLimiterInterceptor", ["$q", "ngHttpRateLimiterConfig", function rateLimiterInterceptor($q, rateLimiterConfig) {
return {
request: function(config) {
var limiters = rateLimiterConfig.getLimiters();
var limiter;
for (var i = 0; i < limiters.length; i++) {
if (limiters[i].matches(config.url)) {
limiter = limiters[i];
break;
}
}
if (limiter) {
var deferred = $q.defer();
limiter.add(config, deferred);
return deferred.promise;
} else {
return config;
}
}
};
}]);
return 'ngHttpRateLimiter';
});