From a309108f51615afea5e146a056abaf3e51b1b387 Mon Sep 17 00:00:00 2001 From: Tom Sightler Date: Wed, 20 Nov 2024 21:49:49 -0500 Subject: [PATCH] Custom fetch keepalive Use a custom fetch agent to reduce total TCP connections/DNS lookups and leverage long-lived HTTPS connections --- packages/ring-client-api/package.json | 1 + packages/ring-client-api/rest-client.ts | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/ring-client-api/package.json b/packages/ring-client-api/package.json index 70f435c7..c23847ce 100644 --- a/packages/ring-client-api/package.json +++ b/packages/ring-client-api/package.json @@ -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" diff --git a/packages/ring-client-api/rest-client.ts b/packages/ring-client-api/rest-client.ts index 2b854305..afcac824 100644 --- a/packages/ring-client-api/rest-client.ts +++ b/packages/ring-client-api/rest-client.ts @@ -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, @@ -116,6 +123,7 @@ async function requestWithRetry( const options = { ...defaultRequestOptions, ...requestOptions, + dispatcher: fetchAgent, } // If a timeout is provided, create an AbortSignal for it @@ -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) {