Skip to content

Commit

Permalink
Tune fetch for improved connection management (#1532)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Nov 23, 2024
1 parent b3b17c4 commit 209f13e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/purple-teachers-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'ring-client-api': patch
'homebridge-ring': patch
---

Increase keepalive and adjust connection pooling, which will hopefully result in more stable fetch requests
28 changes: 22 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/ring-client-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"rxjs": "^7.8.1",
"socket.io-client": "^2.5.0",
"systeminformation": "^5.23.5",
"undici": "^6.21.0",
"uuid": "^11.0.3",
"werift": "0.20.1",
"ws": "^8.18.0"
Expand Down
11 changes: 9 additions & 2 deletions packages/ring-client-api/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ import {
import { ReplaySubject } from 'rxjs'
import assert from 'assert'
import type { Credentials } from '@eneris/push-receiver/dist/types'
import { Agent } from 'undici'

interface RequestOptions extends RequestInit {
responseType?: 'json' | 'buffer'
timeout?: number
json?: object
dispatcher?: Agent
}

const defaultRequestOptions: RequestOptions = {
const fetchAgent = new Agent({
connections: 6,
pipelining: 1,
keepAliveTimeout: 115000,
}),
defaultRequestOptions: RequestOptions = {
responseType: 'json',
method: 'GET',
timeout: 20000,
Expand Down Expand Up @@ -116,6 +123,7 @@ async function requestWithRetry<T>(
const options = {
...defaultRequestOptions,
...requestOptions,
dispatcher: fetchAgent,
}

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

0 comments on commit 209f13e

Please sign in to comment.