Skip to content

Commit

Permalink
feat: allow to specify connect timeout in config
Browse files Browse the repository at this point in the history
  • Loading branch information
samialdury committed Apr 3, 2023
1 parent bd78617 commit 3c52be9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof configSchema>
Expand Down Expand Up @@ -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'],
})
}
9 changes: 7 additions & 2 deletions src/proxy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/proxy/http-server.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}

Expand Down

0 comments on commit 3c52be9

Please sign in to comment.