Skip to content

Commit

Permalink
feat: limit the number of connections open in Node.js (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr authored Aug 2, 2023
1 parent 0f4fd81 commit 7d3d537
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
},
"dependencies": {
"@sanity/eventsource": "^5.0.0",
"get-it": "^8.2.0",
"get-it": "^8.3.0",
"rxjs": "^7.0.0"
},
"devDependencies": {
Expand Down
16 changes: 15 additions & 1 deletion src/http/nodeMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import {debug, headers} from 'get-it/middleware'
import {agent, debug, headers} from 'get-it/middleware'

import {name, version} from '../../package.json'

const middleware = [
debug({verbose: true, namespace: 'sanity:client'}),
headers({'User-Agent': `${name} ${version}`}),

// Enable keep-alive, and in addition limit the number of sockets that can be opened.
// This avoids opening too many connections to the server if someone tries to execute
// a bunch of requests in parallel. It's recommended to have a concurrency limit
// at a "higher limit" (i.e. you shouldn't actually execute hundreds of requests in parallel),
// and this is mainly to minimize the impact for the network and server.
//
// We're currently matching the same defaults as browsers:
// https://stackoverflow.com/questions/26003756/is-there-a-limit-practical-or-otherwise-to-the-number-of-web-sockets-a-page-op
agent({
keepAlive: true,
maxSockets: 30,
maxTotalSockets: 256,
}),
]

export default middleware

0 comments on commit 7d3d537

Please sign in to comment.