Skip to content

Commit 653ed0e

Browse files
authored
Merge pull request #459 from snyk/fix/when_it_should_retry
Fix/when retry request
2 parents cabc8d1 + de32f36 commit 653ed0e

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/transmitter/index.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
IDependencyGraphPayload,
99
IWorkloadMetadataPayload,
1010
IResponseWithAttempts,
11+
IRequestError,
1112
} from './types';
1213
import { getProxyAgent } from './proxy';
1314

@@ -73,10 +74,6 @@ async function retryRequest(verb: NeedleHttpVerbs, url: string, payload: object)
7374
const retry = {
7475
attempts: 3,
7576
intervalSeconds: 2,
76-
networkErrorMessages: [
77-
'socket hang up',
78-
'Client network socket disconnected before secure TLS connection was established',
79-
],
8077
};
8178
const options: NeedleOptions = {
8279
json: true,
@@ -97,11 +94,7 @@ async function retryRequest(verb: NeedleHttpVerbs, url: string, payload: object)
9794
break;
9895
}
9996
} catch (err) {
100-
if (!(
101-
err.code === 'ECONNRESET' &&
102-
retry.networkErrorMessages.includes(err.message) &&
103-
stillHaveRetries
104-
)) {
97+
if (!shouldRetryRequest(err, stillHaveRetries)) {
10598
throw err;
10699
}
107100
}
@@ -114,3 +107,24 @@ async function retryRequest(verb: NeedleHttpVerbs, url: string, payload: object)
114107

115108
return {response, attempt};
116109
}
110+
111+
function shouldRetryRequest(err: IRequestError, stillHaveRetries: boolean): boolean {
112+
const networkErrorMessages: string[] = [
113+
'socket hang up',
114+
'Client network socket disconnected before secure TLS connection was established',
115+
];
116+
117+
if (!stillHaveRetries) {
118+
return false;
119+
}
120+
121+
if (err.code === 'ECONNRESET' && networkErrorMessages.includes(err.message)) {
122+
return true;
123+
}
124+
125+
if (err.code === 'EAI_AGAIN') {
126+
return true;
127+
}
128+
129+
return false;
130+
}

src/transmitter/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ export interface IResponseWithAttempts {
7272
response: NeedleResponse;
7373
attempt: number;
7474
}
75+
76+
export interface IRequestError {
77+
code: string;
78+
message: string;
79+
}

test/system/kind.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ tap.test('Kubernetes-Monitor with KinD', async (t) => {
103103
message: 'socket hang up',
104104
});
105105

106+
nock('https://kubernetes-upstream.snyk.io')
107+
.post('/api/v1/dependency-graph')
108+
.times(1)
109+
.replyWithError({
110+
code: 'EAI_AGAIN',
111+
message: 'getaddrinfo EAI_AGAIN kubernetes-upstream.snyk.io',
112+
});
113+
106114
nock('https://kubernetes-upstream.snyk.io')
107115
.post('/api/v1/dependency-graph')
108116
.times(1)

0 commit comments

Comments
 (0)