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 committed Nov 21, 2024
1 parent 1822256 commit a309108
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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: 1,
pipelining: 1,
keepAliveTimeout: 60000,
}),
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 a309108

Please sign in to comment.