@@ -25,15 +25,15 @@ import HTTPClientSidecar from "./sidecar";
25
25
export default class HTTPClient implements IClient {
26
26
private isInitialized : boolean ;
27
27
28
- private readonly client : typeof fetch ;
28
+ private static client : typeof fetch ;
29
29
private readonly clientHost : string ;
30
30
private readonly clientPort : string ;
31
31
private readonly clientUrl : string ;
32
32
private readonly options : DaprClientOptions ;
33
33
private readonly logger : Logger ;
34
34
35
- private readonly httpAgent ;
36
- private readonly httpsAgent ;
35
+ private static httpAgent : http . Agent ;
36
+ private static httpsAgent : https . Agent ;
37
37
38
38
constructor (
39
39
host = Settings . getDefaultHost ( )
@@ -45,7 +45,6 @@ export default class HTTPClient implements IClient {
45
45
this . options = options ;
46
46
this . logger = new Logger ( "HTTPClient" , "HTTPClient" , this . options . logger ) ;
47
47
this . isInitialized = false ;
48
-
49
48
// fallback to default
50
49
if ( this . options . isKeepAlive === undefined ) {
51
50
this . options . isKeepAlive = true ;
@@ -57,17 +56,21 @@ export default class HTTPClient implements IClient {
57
56
this . clientUrl = `${ this . clientHost } :${ this . clientPort } /v1.0` ;
58
57
}
59
58
60
- this . client = fetch ;
59
+ if ( ! HTTPClient . client ) {
60
+ HTTPClient . client = fetch ;
61
+ }
61
62
62
63
// Add a custom agent so we can decide if we want to reuse connections or not
63
64
// we use an agent so we can reuse an open connection, limiting handshake requirements
64
65
// Note: when using an agent, we will encounter TCPWRAP since the connection doesn't get destroyed
65
- if ( this . options . isKeepAlive ) {
66
- this . httpAgent = new http . Agent ( { keepAlive : true , keepAliveMsecs : 30 * 1000 } ) ;
67
- this . httpsAgent = new https . Agent ( { keepAlive : true , keepAliveMsecs : 30 * 1000 } ) ;
68
- } else {
69
- this . httpAgent = new http . Agent ( ) ;
70
- this . httpsAgent = new https . Agent ( ) ;
66
+ const keepAlive = this . options . isKeepAlive ;
67
+ const keepAliveMsecs = 30 * 1000 ; // it is applicable only when keepAlive is set to true
68
+
69
+ if ( ! HTTPClient . httpAgent ) {
70
+ HTTPClient . httpAgent = new http . Agent ( { keepAlive : keepAlive , keepAliveMsecs : keepAliveMsecs } ) ;
71
+ }
72
+ if ( ! HTTPClient . httpsAgent ) {
73
+ HTTPClient . httpsAgent = new https . Agent ( { keepAlive : keepAlive , keepAliveMsecs : keepAliveMsecs } ) ;
71
74
}
72
75
}
73
76
@@ -78,7 +81,7 @@ export default class HTTPClient implements IClient {
78
81
await this . start ( ) ;
79
82
}
80
83
81
- return this . client ;
84
+ return HTTPClient . client ;
82
85
}
83
86
84
87
getClientHost ( ) : string {
@@ -114,8 +117,8 @@ export default class HTTPClient implements IClient {
114
117
}
115
118
116
119
async stop ( ) : Promise < void > {
117
- this . httpAgent . destroy ( ) ;
118
- this . httpsAgent . destroy ( ) ;
120
+ HTTPClient . httpAgent . destroy ( ) ;
121
+ HTTPClient . httpsAgent . destroy ( ) ;
119
122
}
120
123
121
124
async start ( ) : Promise < void > {
@@ -171,7 +174,7 @@ export default class HTTPClient implements IClient {
171
174
}
172
175
173
176
const urlFull = url . startsWith ( "http" ) ? url : `${ this . clientUrl } ${ url } ` ;
174
- const agent = urlFull . startsWith ( "https" ) ? this . httpsAgent : this . httpAgent ;
177
+ const agent = urlFull . startsWith ( "https" ) ? HTTPClient . httpsAgent : HTTPClient . httpAgent ;
175
178
params . agent = agent ;
176
179
177
180
this . logger . debug ( `Fetching ${ params . method } ${ urlFull } with body: (${ params . body } )` ) ;
0 commit comments