MCPcopy Index your code
hub / github.com/restify/node-restify / cpuUsageThrottle

Function cpuUsageThrottle

lib/plugins/cpuUsageThrottle.js:178–206  ·  view source on GitHub ↗
(req, res, next)

Source from the content-addressed store, hash-verified

176 updateReject();
177
178 function cpuUsageThrottle(req, res, next) {
179 // Check to see if this request gets rejected. Since, in updateReject,
180 // we calculate a percentage of traffic we are planning to reject, we
181 // can use Math.random() (which picks from a uniform distribution in
182 // [0,1)) to give us a `plugin._reject`% chance of dropping any given
183 // request. This is a stateless was to drop approximatly
184 // `plugin._reject`% of traffic.
185 var probabilityDraw = Math.random();
186
187 if (probabilityDraw >= plugin._reject) {
188 return next(); // Don't reject this request
189 }
190
191 var err = new errors.ServiceUnavailableError({
192 context: {
193 plugin: 'cpuUsageThrottle',
194 cpuUsage: plugin._cpu,
195 limit: plugin._limit,
196 max: plugin._max,
197 reject: plugin._reject,
198 halfLife: plugin._halfLife,
199 interval: plugin._interval,
200 probabilityDraw: probabilityDraw,
201 lag: plugin._timeoutDelta
202 }
203 });
204
205 return next(err);
206 }
207
208 // Allow the app to clear the timeout for this plugin if necessary, without
209 // this we would never be able to clear the event loop when letting Node

Callers 1

Calls 1

nextFunction · 0.50

Tested by

no test coverage detected