Skip to content

Commit

Permalink
web: Make sure to close connection on error
Browse files Browse the repository at this point in the history
  • Loading branch information
schneefux committed Feb 20, 2025
1 parent c206789 commit f3b57e5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion web/api/lib/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { URLSearchParams, URL } from 'url'
import StatsD from 'hot-shots'
import { TRPCError } from '@trpc/server'
import { fetch } from 'undici'

const stats = new StatsD({ prefix: 'brawltime.api.' })

Expand Down Expand Up @@ -34,14 +35,19 @@ export function request<T>(
},
signal: AbortSignal.timeout(timeoutMs),
})
.then(response => {
.then(async response => {
if (!response.ok) {
if (response.status == 429) {
stats.increment(metricName + '.ratelimited')
} else if (response.status >= 500) {
stats.increment(metricName + '.servererror')
}

if (response.body) {
// consume body to close connection, https://undici.nodejs.org/#/?id=garbage-collection
for await (const chunk of response.body) {}
}

throw new RequestError({
url: url.toString(),
status: response.status,
Expand Down

0 comments on commit f3b57e5

Please sign in to comment.