ソースを参照

Merge pull request #16775 from overleaf/bg-clsi-timeout-use-fetchutils-connect-timeout

Update UrlFetcher to use custom agent with connect timeout

GitOrigin-RevId: c92dc27a0e682b0b79859b3521bf64cbf8485345
Brian Gough 2 年 前
コミット
c1a4e4a873
1 ファイル変更13 行追加1 行削除
  1. 13 1
      services/clsi/app/js/UrlFetcher.js

+ 13 - 1
services/clsi/app/js/UrlFetcher.js

@@ -1,11 +1,19 @@
 const fs = require('fs')
 const logger = require('@overleaf/logger')
 const Settings = require('@overleaf/settings')
-const { fetchStream } = require('@overleaf/fetch-utils')
+const {
+  CustomHttpAgent,
+  CustomHttpsAgent,
+  fetchStream,
+} = require('@overleaf/fetch-utils')
 const { URL } = require('url')
 const { pipeline } = require('stream/promises')
 const Metrics = require('./Metrics')
 
+const MAX_CONNECT_TIME = 1000
+const httpAgent = new CustomHttpAgent({ connectTimeout: MAX_CONNECT_TIME })
+const httpsAgent = new CustomHttpsAgent({ connectTimeout: MAX_CONNECT_TIME })
+
 async function pipeUrlToFileWithRetry(url, filePath) {
   let remainingAttempts = 3
   let lastErr
@@ -40,6 +48,10 @@ async function pipeUrlToFile(url, filePath) {
 
   const stream = await fetchStream(url, {
     signal: AbortSignal.timeout(60 * 1000),
+    // provide a function to get the agent for each request
+    // as there may be multiple requests with different protocols
+    // due to redirects.
+    agent: _url => (_url.protocol === 'https:' ? httpsAgent : httpAgent),
   })
 
   const atomicWrite = filePath + '~'