diff options
author | Chocobozzz <me@florianbigard.com> | 2021-11-16 11:17:52 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-11-16 11:34:24 +0100 |
commit | 3455c2656e257ae3d9b4169af58b6889d9904148 (patch) | |
tree | 72ef7035ffd9030cffe8e73d7c1265241c120173 /server/helpers/requests.ts | |
parent | ac03618098e430acb21c075bdba57ce6d377b12d (diff) | |
download | PeerTube-3455c2656e257ae3d9b4169af58b6889d9904148.tar.gz PeerTube-3455c2656e257ae3d9b4169af58b6889d9904148.tar.zst PeerTube-3455c2656e257ae3d9b4169af58b6889d9904148.zip |
Test and log request retries
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r-- | server/helpers/requests.ts | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 6e80995ad..fc77ebd35 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { createWriteStream, remove } from 'fs-extra' | 1 | import { createWriteStream, remove } from 'fs-extra' |
2 | import got, { CancelableRequest, Options as GotOptions, RequestError, Response } from 'got' | 2 | import got, { CancelableRequest, NormalizedOptions, Options as GotOptions, RequestError, Response } from 'got' |
3 | import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent' | 3 | import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent' |
4 | import { join } from 'path' | 4 | import { join } from 'path' |
5 | import { CONFIG } from '../initializers/config' | 5 | import { CONFIG } from '../initializers/config' |
@@ -16,6 +16,7 @@ const httpSignature = require('@peertube/http-signature') | |||
16 | export interface PeerTubeRequestError extends Error { | 16 | export interface PeerTubeRequestError extends Error { |
17 | statusCode?: number | 17 | statusCode?: number |
18 | responseBody?: any | 18 | responseBody?: any |
19 | responseHeaders?: any | ||
19 | } | 20 | } |
20 | 21 | ||
21 | type PeerTubeRequestOptions = { | 22 | type PeerTubeRequestOptions = { |
@@ -99,6 +100,12 @@ const peertubeGot = got.extend({ | |||
99 | }, httpSignatureOptions) | 100 | }, httpSignatureOptions) |
100 | } | 101 | } |
101 | } | 102 | } |
103 | ], | ||
104 | |||
105 | beforeRetry: [ | ||
106 | (_options: NormalizedOptions, error: RequestError, retryCount: number) => { | ||
107 | logger.debug('Retrying request to %s.', error.request.requestUrl, { retryCount, error: buildRequestError(error), ...lTags() }) | ||
108 | } | ||
102 | ] | 109 | ] |
103 | } | 110 | } |
104 | }) | 111 | }) |
@@ -107,7 +114,6 @@ function doRequest (url: string, options: PeerTubeRequestOptions = {}) { | |||
107 | const gotOptions = buildGotOptions(options) | 114 | const gotOptions = buildGotOptions(options) |
108 | 115 | ||
109 | return peertubeGot(url, gotOptions) | 116 | return peertubeGot(url, gotOptions) |
110 | .on('retry', logRetryFactory(url)) | ||
111 | .catch(err => { throw buildRequestError(err) }) | 117 | .catch(err => { throw buildRequestError(err) }) |
112 | } | 118 | } |
113 | 119 | ||
@@ -115,7 +121,6 @@ function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) { | |||
115 | const gotOptions = buildGotOptions(options) | 121 | const gotOptions = buildGotOptions(options) |
116 | 122 | ||
117 | return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' }) | 123 | return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' }) |
118 | .on('retry', logRetryFactory(url)) | ||
119 | .catch(err => { throw buildRequestError(err) }) | 124 | .catch(err => { throw buildRequestError(err) }) |
120 | } | 125 | } |
121 | 126 | ||
@@ -246,14 +251,9 @@ function buildRequestError (error: RequestError) { | |||
246 | 251 | ||
247 | if (error.response) { | 252 | if (error.response) { |
248 | newError.responseBody = error.response.body | 253 | newError.responseBody = error.response.body |
254 | newError.responseHeaders = error.response.headers | ||
249 | newError.statusCode = error.response.statusCode | 255 | newError.statusCode = error.response.statusCode |
250 | } | 256 | } |
251 | 257 | ||
252 | return newError | 258 | return newError |
253 | } | 259 | } |
254 | |||
255 | function logRetryFactory (url: string) { | ||
256 | return (retryCount: number, error: RequestError) => { | ||
257 | logger.debug('Retrying request to %s.', url, { retryCount, error, ...lTags() }) | ||
258 | } | ||
259 | } | ||