forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupstreamHttpClient.ts
More file actions
105 lines (92 loc) · 3.04 KB
/
Copy pathupstreamHttpClient.ts
File metadata and controls
105 lines (92 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import axios, { AxiosRequestConfig } from 'axios';
import { ChaosPolicy } from '../chaos/chaosPolicy';
import { RetryOptions, withRetry } from '../utils/retry';
export class DependencyError extends Error {
constructor(message: string) {
super(message);
this.name = 'DependencyError';
}
}
export interface UpstreamClientConfig {
dependencyName: string;
baseUrl: string;
timeoutMs: number;
retryOptions?: RetryOptions;
}
/**
* A shared HTTP client wrapper for upstream dependencies.
* Provides resilient features such as:
* - Exponential backoff with jitter (via withRetry)
* - Global timeout budget across all retries
* - Integration with chaos testing hooks
*/
export class UpstreamHttpClient {
private readonly client;
constructor(
private readonly config: UpstreamClientConfig,
private readonly chaosPolicy: ChaosPolicy,
) {
this.client = axios.create({
baseURL: this.config.baseUrl,
});
}
/**
* Executes an HTTP request with retries, timeout budget, and chaos injection.
*/
async request<T>(requestConfig: AxiosRequestConfig): Promise<T> {
const chaosResult = this.chaosPolicy.decide(this.config.dependencyName);
if (chaosResult === 'error') {
throw new DependencyError('Injected dependency failure');
}
if (chaosResult === 'timeout') {
throw new DependencyError('Injected dependency timeout');
}
const controller = new AbortController();
const globalTimeout = setTimeout(() => {
controller.abort();
}, this.config.timeoutMs);
try {
return await withRetry(async () => {
try {
const response = await this.client.request<T>({
...requestConfig,
signal: controller.signal,
});
return response.data;
} catch (error) {
if (axios.isCancel(error)) {
throw new DependencyError('Upstream dependency timeout');
}
if (axios.isAxiosError(error) && error.response) {
throw new DependencyError('Upstream returned non-success response');
}
throw error;
}
}, {
...this.config.retryOptions,
isRetryable: (error: unknown) => {
if (error instanceof DependencyError && error.message === 'Upstream dependency timeout') {
return false;
}
if (this.config.retryOptions?.isRetryable) {
return this.config.retryOptions.isRetryable(error);
}
return true;
}
});
} catch (error) {
if (error instanceof DependencyError) {
throw error;
}
throw new DependencyError('Upstream dependency unavailable');
} finally {
clearTimeout(globalTimeout);
}
}
async get<T>(url: string, config?: Omit<AxiosRequestConfig, 'url' | 'method'>): Promise<T> {
return this.request<T>({ ...config, method: 'GET', url });
}
async post<T>(url: string, data?: any, config?: Omit<AxiosRequestConfig, 'url' | 'method' | 'data'>): Promise<T> {
return this.request<T>({ ...config, method: 'POST', url, data });
}
}