Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: limit the number of connections open in Node.js #271

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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