在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:davidmarkclements/overload-protection开源软件地址:https://github.com/davidmarkclements/overload-protection开源编程语言:JavaScript 100.0%开源软件介绍:overload-protectionLoad detection and shedding capabilities for http, express, restify, and koa About
If a threshold is crossed for a given metric, Current supported metrics are:
For a great explanation of Used Heap Memory vs Resident Set Size see Daniel Khans article at https://www.dynatrace.com/blog/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js UsageCreate a config object for your thresholds (and other const protectCfg = {
production: process.env.NODE_ENV === 'production', // if production is false, detailed error messages are exposed to the client
clientRetrySecs: 1, // Retry-After header, in seconds (0 to disable) [default 1]
sampleInterval: 5, // sample rate, milliseconds [default 5]
maxEventLoopDelay: 42, // maximum detected delay between event loop ticks [default 42]
maxHeapUsedBytes: 0, // maximum heap used threshold (0 to disable) [default 0]
maxRssBytes: 0, // maximum rss size threshold (0 to disable) [default 0]
errorPropagationMode: false, // dictate behavior: take over the response
// or propagate an error to the framework [default false]
logging: false, // set to string for log level or function to pass data to
logStatsOnReq: false // set to true to log stats on every requests
} Then pass the framework we're integrating with along with the configuration object. For instance with Express we would do: const app = require('express')()
const protect = require('overload-protection')('express', protectCfg)
app.use(protect) With middleware based frameworks, always put the Restify, and Koa all work in much the same way, call the const Koa = require('koa')
const protect = require('overload-protection')('koa', protectCfg)
const app = new Koa()
app.use(protect) For pure core HTTP the const http = require('http')
const protect = require('overload-protection')('http', protectCfg)
http.createServer(function (req, res) {
if (protect(req, res) === true) return
res.end('content')
}) With three arguments (the third argument being a callback), the rest of the work should be done within the supplied callback. const http = require('http')
const protect = require('overload-protection')('http', protectCfg)
http.createServer(function (req, res) {
protect(req, res, function () {
// when errorPropagationMode mode is false, will *only*
// be called if load shedding didn't occur
// (if it was true we'd need to check for an Error object as first arg)
res.end('content')
})
}) Installationnpm install overload-protection --save Testsnpm install
npm test BenchmarkThe overhead of using npm run benchmarks APIrequire('overload-protection') => (framework, opts) => instanceThe
The Options (particularly thresholds) are quite sensitive and highly relevant on a case by case basis. Possible options are as follows: production: process.env.NODE_ENV === 'production'The clientRetrySecs: 1By default, sampleInterval: 5In order to establish whether a threshold has been crossed, the metrics are sampled at a regular interval. The interval defaults to 5 milliseconds. maxEventLoopDelay: 42Synchronous work causes the event loop to freeze, when this happens an interval timer (which is our sampler) will be delayed by the amount of time the event loop was stalled for while the thread processed synchronous work. We can measure this with timestamp comparison. This option sets a threshold for the maximum amount of stalling between intervals we'll accept before our service begins responding with 503 codes to requests. Defaults to 42 milliseconds. When set to 0 this threshold will be disabled. maxHeapUsedBytes: 0Disabled by default (set to 0), this defines maximum V8 (Node's JavaScript engine) used heap size. If the Used Heap size exceeds the threshold the server will begin return 503 error codes until it crosses back under the threshold. See https://www.dynatrace.com/blog/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js for more info on Used Heap from a V8 context. maxRssBytes: 0Disabled by default (set to 0) maximum process Resident Set Size. If the RSS exceeds the threshold the server will begin return 503 error codes until it crosses back under the threshold. errorPropagationMode: falseThis is relevant to middleware integration only By default, However, it could be argued, from a puritanical perspective, that middleware should defer to the framework and that any HTTP code of 500 or above should be generated by propagating an error through the framework. This option prevents logging: falseThe If If the application isn't using a request bound Log4j-style logger, the This is primarily for usage when logStatsOnReq: falseSet instance.overloadThe returned instance (which in many cases is passed as middleware to This allows for any heavy load detection required outside of a framework. instance.eventLoopOverloadThe returned instance (which in many cases is passed as middleware to This allows for any event loop delay detection necessary outside of a framework. instance.heapUsedOverloadThe returned instance (which in many cases is passed as middleware to This allows for any heap used threshold detection necessary outside of a framework. instance.rssOverloadThe returned instance (which in many cases is passed as middleware to This allows for any heap used threshold detection necessary outside of a framework. instance.eventLoopDelayThe delay in milliseconds (with additional decimal precision) since the last sample. If instance.maxEventLoopDelayCorresponds to the instance.maxHeapUsedBytesCorresponds to the instance.maxRssBytesCorresponds to the Dependencies
Dev Dependencies
LicenseMIT AcknowledgementsKindly sponsored by nearForm |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论