Skip to content

Commit

Permalink
fix: Extends connection timeout threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Mar 29, 2024
1 parent 73491f4 commit 5b1a83b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion xmcl-runtime/install/getJsonWithCache.ts
Expand Up @@ -4,7 +4,7 @@ import { AnyError } from '~/util/error'

export async function getJson<T>(url: string, error: string) {
try {
const response = await request(url)
const response = await request(url, { maxRedirections: 4 })

const body = await response.body.text()

Expand Down
18 changes: 14 additions & 4 deletions xmcl-runtime/network/dispatchers/proxyDispatcher.ts
Expand Up @@ -50,6 +50,9 @@ export class ProxyAgent extends DispatcherBase {
private proxyHeader?: Record<string, string>

private pConnect: buildConnector.connector
private _connect: buildConnector.connector
private requestTls?: buildConnector.BuildOptions
private proxyTls?: buildConnector.BuildOptions

async setProxy(uri: URL, auth?: string) {
const oldClient = this.proxyClient
Expand All @@ -68,6 +71,11 @@ export class ProxyAgent extends DispatcherBase {
this.isProxyEnabled = enabled
}

setConnectTimeout(timeout: number) {
this.pConnect = buildConnector({ timeout, ...this.proxyTls || {} })
this._connect = buildConnector({ timeout, ...this.requestTls || {} })
}

constructor(opts: {
controller: ProxySettingController
factory: (connect: buildConnector.connector) => Agent
Expand All @@ -77,13 +85,15 @@ export class ProxyAgent extends DispatcherBase {
super()

opts.controller.add(this)
this.requestTls = opts.requestTls
this.proxyTls = opts.proxyTls

this.pConnect = buildConnector(opts.requestTls)
const connector = buildConnector(opts.proxyTls)
this.pConnect = buildConnector(opts.proxyTls)
this._connect = buildConnector(opts.requestTls)

const connect = async (opts: any, callback: buildConnector.Callback) => {
if (!this.isProxyEnabled || !this.proxyClient) {
connector(opts, callback)
this._connect(opts, callback)
return
}
let requestedHost = opts.host
Expand All @@ -108,7 +118,7 @@ export class ProxyAgent extends DispatcherBase {
return
}
const servername = opts.servername
connector({
this._connect({
...opts,
servername,
httpSocket: socket,
Expand Down
10 changes: 4 additions & 6 deletions xmcl-runtime/network/pluginNetworkInterface.ts
@@ -1,4 +1,4 @@
import { DefaultRangePolicy, getDefaultAgentOptions } from '@xmcl/file-transfer'
import { DefaultRangePolicy, createRedirectInterceptor, getDefaultAgentOptions } from '@xmcl/file-transfer'
import { PoolStats } from '@xmcl/runtime-api'
import { ClassicLevel } from 'classic-level'
import { join } from 'path'
Expand Down Expand Up @@ -79,18 +79,16 @@ export const pluginNetworkInterface: LauncherAppPlugin = (app) => {
options.headers = headers
}

const apiAgentOptions = getDefaultAgentOptions()
const apiDispatcher = new CacheDispatcher(new ProxyAgent({
controller: proxy,
factory: (connect) => new Agent({
interceptors: {
Agent: [...apiAgentOptions.interceptors.Agent],
Client: [...apiAgentOptions.interceptors.Client, createInterceptOptionsInterceptor([appendUserAgent, ...dispatchInterceptors])],
Client: [createInterceptOptionsInterceptor([appendUserAgent, ...dispatchInterceptors]), createRedirectInterceptor(5)],
},
pipelining: 1,
bodyTimeout: 20_000,
headersTimeout: 10_000,
connectTimeout: 10_000,
connectTimeout: 45_000,
connect,
factory(origin, opts: Agent.Options) {
let dispatcher: Dispatcher | undefined
Expand All @@ -117,7 +115,7 @@ export const pluginNetworkInterface: LauncherAppPlugin = (app) => {
Client: [...downloadAgentOptions.interceptors.Client],
},
headersTimeout: 15_000,
connectTimeout: 35_000,
connectTimeout: 45_000,
bodyTimeout: 60_000,
connect,
factory: (origin, opts) => patchIfPool(new Pool(origin, opts)),
Expand Down

0 comments on commit 5b1a83b

Please sign in to comment.