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

Tune fetch for improved connection management #1532

Merged
merged 2 commits into from
Nov 23, 2024
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
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