From 3c52be90129d1beff6190c946ae3fea2220a38c3 Mon Sep 17 00:00:00 2001 From: Sami Al-Dury <57627858+samialdury@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:04:23 +0200 Subject: [PATCH] feat: allow to specify connect timeout in config --- src/config.ts | 2 ++ src/proxy/handler.ts | 9 +++++++-- src/proxy/http-server.ts | 5 +++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/config.ts b/src/config.ts index da3950c..4e063f3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -26,6 +26,7 @@ const configSchema = z.object({ .url() .default('https://sdk-writer.eu.smartlook.cloud'), managerHost: z.string().url().default('https://manager.eu.smartlook.cloud'), + undiciConnectTimeout: z.coerce.number().positive().default(30_000), }) export let config: z.infer @@ -55,5 +56,6 @@ export function initConfig(): void { webSdkWriterHost: process.env['WEB_SDK_WRITER_HOST'], mobileSdkWriterHost: process.env['MOBILE_SDK_WRITER_HOST'], managerHost: process.env['MANAGER_HOST'], + undiciConnectTimeout: process.env['UNDICI_CONNECT_TIMEOUT'], }) } diff --git a/src/proxy/handler.ts b/src/proxy/handler.ts index d49c0f8..7a91662 100644 --- a/src/proxy/handler.ts +++ b/src/proxy/handler.ts @@ -3,6 +3,7 @@ import { IncomingMessage, ServerResponse } from 'http' import undici from 'undici' +import { config } from '../config.js' import { logger } from '../logger.js' import { internalError } from './internal-error.js' @@ -11,7 +12,11 @@ import { statusRoute } from './routes/status-route.js' import type { IStreamOpaque, RouteMapping } from './types.js' import { buildUrl, prepareHeaders } from './utils.js' -undici.setGlobalDispatcher(new undici.Agent({ connect: { timeout: 30_000 } })) +export function initUndiciDispatcher(): void { + undici.setGlobalDispatcher( + new undici.Agent({ connect: { timeout: config.undiciConnectTimeout } }) + ) +} async function pipeResponse( routeTargetHost: string, @@ -66,7 +71,7 @@ async function pipeResponse( outgoingHeaders, err, }, - 'Error while piping response. This is just a warning and you can safely ignore it, since Web SDK will retry the request later' + 'Error while piping response. This is just a warning and you can safely ignore it, since Smartlook Web SDK will retry the request later' ) internalError(res) diff --git a/src/proxy/http-server.ts b/src/proxy/http-server.ts index bab562d..1e76b71 100644 --- a/src/proxy/http-server.ts +++ b/src/proxy/http-server.ts @@ -1,14 +1,15 @@ import { createServer, Server } from 'http' -import { handler as rootHandler } from './handler.js' +import { initUndiciDispatcher, handler } from './handler.js' import { initRouteMappings } from './routes/route-mappings.js' export function initHTTPServer(): Server { const routeMappings = initRouteMappings() + initUndiciDispatcher() // eslint-disable-next-line @typescript-eslint/no-misused-promises return createServer(async (req, res) => { - return rootHandler(req, res, routeMappings) + return handler(req, res, routeMappings) }) }