Skip to content

Commit 4428ae1

Browse files
authored
Better instanceof check (#21)
* Better check on whether setImmediate can be used * Documentation on config.prioRequestsProcessing and setImmediate
1 parent 3349544 commit 4428ae1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ server.listen(3000)
7474
```
7575
- **cacheSize**: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: `1000`
7676
- **errorHandler**: Global error handler function. Default value:
77+
7778
```js
7879
(err, req, res) => {
7980
res.statusCode = 500
8081
res.end(err.message)
8182
}
8283
```
8384

85+
* **prioRequestsProcessing**: `true` to use SetImmediate to prioritize router lookup, `false` to disable. By default `true`, if used with native Node.js `http` and `https` servers. Set to `false`, if using Node.js Native Addon server, such as uWebSockets.js, as this will cause a huge performance penalty
86+
8487
Example passing configuration options:
88+
8589
```js
8690
const sequential = require('0http/lib/router/sequential')
8791
const { router, server } = cero({
@@ -180,6 +184,7 @@ wrk -t8 -c40 -d5s http://127.0.0.1:3000/hi
180184
`Requests/sec: 82682.86`
181185

182186
> For more accurate benchmarks please see:
187+
>
183188
> - https://github.com/the-benchmarker/web-frameworks
184189
185190
## Support / Donate 💚

index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
const http = require('http')
2+
const httpServer = http.Server
3+
const httpsServer = require('https').Server
24

35
module.exports = (config = {}) => {
46
const router = config.router || require('./lib/router/sequential')()
57
const server = config.server || http.createServer()
68

7-
server.on('request', (req, res) => {
8-
server instanceof http.Server
9-
? setImmediate(() => router.lookup(req, res))
10-
: router.lookup(req, res)
11-
})
9+
config.prioRequestsProcessing = config.prioRequestsProcessing || server instanceof httpServer || server instanceof httpsServer
10+
11+
/*
12+
Native server can also be https server, so we also need to check for it.
13+
Unfortunately, there appears to be no proper way to check for http2 server.
14+
*/
15+
16+
if (config.prioRequestsProcessing) {
17+
server.on('request', (req, res) => {
18+
setImmediate(() => router.lookup(req, res))
19+
})
20+
} else {
21+
server.on('request', (req, res) => {
22+
router.lookup(req, res)
23+
})
24+
}
1225

1326
return {
1427
router,

0 commit comments

Comments
 (0)