chore: migrate to typescript
This commit is contained in:
committed by
Justin Starry
parent
3eb9f7b3eb
commit
f912c63b22
44
web3.js/src/agent-manager.ts
Normal file
44
web3.js/src/agent-manager.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import http from 'http';
|
||||
import https from 'https';
|
||||
|
||||
export const DESTROY_TIMEOUT_MS = 5000;
|
||||
|
||||
export class AgentManager {
|
||||
_agent: http.Agent | https.Agent;
|
||||
_activeRequests = 0;
|
||||
_destroyTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
_useHttps: boolean;
|
||||
|
||||
static _newAgent(useHttps: boolean): http.Agent | https.Agent {
|
||||
const options = {keepAlive: true, maxSockets: 25};
|
||||
if (useHttps) {
|
||||
return new https.Agent(options);
|
||||
} else {
|
||||
return new http.Agent(options);
|
||||
}
|
||||
}
|
||||
|
||||
constructor(useHttps?: boolean) {
|
||||
this._useHttps = useHttps === true;
|
||||
this._agent = AgentManager._newAgent(this._useHttps);
|
||||
}
|
||||
|
||||
requestStart(): http.Agent | https.Agent {
|
||||
this._activeRequests++;
|
||||
if (this._destroyTimeout !== null) {
|
||||
clearTimeout(this._destroyTimeout);
|
||||
this._destroyTimeout = null;
|
||||
}
|
||||
return this._agent;
|
||||
}
|
||||
|
||||
requestEnd() {
|
||||
this._activeRequests--;
|
||||
if (this._activeRequests === 0 && this._destroyTimeout === null) {
|
||||
this._destroyTimeout = setTimeout(() => {
|
||||
this._agent.destroy();
|
||||
this._agent = AgentManager._newAgent(this._useHttps);
|
||||
}, DESTROY_TIMEOUT_MS);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user