Skip to content

Commit

Permalink
Custom fetch keepalive
Browse files Browse the repository at this point in the history
Use a custom fetch agent to reduce total TCP connections/DNS lookups and leverage long-lived HTTPS connections
  • Loading branch information
Tom Sightler authored and dgreif committed Nov 23, 2024
1 parent b3b17c4 commit e1965d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
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 e1965d1

Please sign in to comment.