From 075b35e7ad72f41f0507d39d591c2d11354bbde5 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 30 Apr 2025 17:40:17 -0700 Subject: [PATCH] reuse the client when sending a notification --- src/notifications/senders/AppleNotificationSender.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/notifications/senders/AppleNotificationSender.ts b/src/notifications/senders/AppleNotificationSender.ts index 4aac769..61c0b73 100644 --- a/src/notifications/senders/AppleNotificationSender.ts +++ b/src/notifications/senders/AppleNotificationSender.ts @@ -1,5 +1,6 @@ import jwt from "jsonwebtoken"; import http2 from "http2"; +import { ClientHttp2Session } from "node:http2"; interface APNsUrl { fullUrl: string; @@ -17,6 +18,8 @@ export class AppleNotificationSender { private apnsToken: string | undefined = undefined; private _lastRefreshedTimeMs: number | undefined = undefined; + private client: ClientHttp2Session | undefined = undefined; + constructor(private shouldActuallySendNotifications = true) { this.sendNotificationImmediately = this.sendNotificationImmediately.bind(this); this.lastReloadedTimeForAPNsIsTooRecent = this.lastReloadedTimeForAPNsIsTooRecent.bind(this); @@ -95,7 +98,10 @@ export class AppleNotificationSender { "apns-topic": bundleId, }; try { - const client = http2.connect(host); + if (!this.client) { + this.client = http2.connect(host); + } + const client = this.client; const req = client.request(headers); req.setEncoding('utf8');