Skip to content

Commit

Permalink
feat: add req-frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 9, 2024
1 parent 5e2689e commit 32bf91e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"data-uri-regex": "~0.1.4",
"data-uri-to-buffer": "~5.0.1",
"debug-logfmt": "~1.2.0",
"frequency-counter": "~1.0.1",
"got": "~11.8.6",
"helmet": "~7.1.0",
"html-get": "~2.14.0",
Expand Down
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { API_URL, NODE_ENV, PORT } = require('./constant')

const server = createServer(require('.'))

require('./util/req-frequency')(server)

server.listen(PORT, () => {
debug({
status: 'listening',
Expand Down
21 changes: 21 additions & 0 deletions src/util/req-frequency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const debug = require('debug-logfmt')('req-frequency')
const FrequencyCounter = require('frequency-counter')
const onFinished = require('on-finished')

module.exports = server => {
const min = new FrequencyCounter(60)
let requests = 0
const info = () => {
const perMinute = min.freq()
return { requests, perMinute, perSecond: Number(perMinute / 60).toFixed(1) }
}
server.on('request', (_, res) => {
++requests
min.inc()
debug(info())
onFinished(res, () => --requests)
})
return info
}

0 comments on commit 32bf91e

Please sign in to comment.