Skip to content

Commit 209f13e

Browse files
tsightlerTom Sightlerdgreif
authored
Tune fetch for improved connection management (#1532)
* Custom fetch keepalive Use a custom fetch agent to reduce total TCP connections/DNS lookups and leverage long-lived HTTPS connections * Create changeset --------- Co-authored-by: Tom Sightler <[email protected]> Co-authored-by: dgreif <[email protected]>
1 parent b3b17c4 commit 209f13e

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

.changeset/purple-teachers-applaud.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'ring-client-api': patch
3+
'homebridge-ring': patch
4+
---
5+
6+
Increase keepalive and adjust connection pooling, which will hopefully result in more stable fetch requests

package-lock.json

+22-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ring-client-api/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"rxjs": "^7.8.1",
2727
"socket.io-client": "^2.5.0",
2828
"systeminformation": "^5.23.5",
29+
"undici": "^6.21.0",
2930
"uuid": "^11.0.3",
3031
"werift": "0.20.1",
3132
"ws": "^8.18.0"

packages/ring-client-api/rest-client.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ import {
1616
import { ReplaySubject } from 'rxjs'
1717
import assert from 'assert'
1818
import type { Credentials } from '@eneris/push-receiver/dist/types'
19+
import { Agent } from 'undici'
1920

2021
interface RequestOptions extends RequestInit {
2122
responseType?: 'json' | 'buffer'
2223
timeout?: number
2324
json?: object
25+
dispatcher?: Agent
2426
}
2527

26-
const defaultRequestOptions: RequestOptions = {
28+
const fetchAgent = new Agent({
29+
connections: 6,
30+
pipelining: 1,
31+
keepAliveTimeout: 115000,
32+
}),
33+
defaultRequestOptions: RequestOptions = {
2734
responseType: 'json',
2835
method: 'GET',
2936
timeout: 20000,
@@ -116,6 +123,7 @@ async function requestWithRetry<T>(
116123
const options = {
117124
...defaultRequestOptions,
118125
...requestOptions,
126+
dispatcher: fetchAgent,
119127
}
120128

121129
// If a timeout is provided, create an AbortSignal for it
@@ -501,7 +509,6 @@ export class RingRestClient {
501509
authorization: `Bearer ${authTokenResponse.access_token}`,
502510
hardware_id: hardwareId,
503511
'User-Agent': 'android:com.ringapp',
504-
Connection: 'close',
505512
},
506513
})
507514
} catch (e: any) {

0 commit comments

Comments
 (0)