chore: migrate to typescript

This commit is contained in:
Justin Starry
2021-03-15 11:01:35 +08:00
committed by Justin Starry
parent 3eb9f7b3eb
commit f912c63b22
51 changed files with 948 additions and 980 deletions

View 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);
}
}
}