Skip to content

Commit 4af7d93

Browse files
committed
Revises Agent Model
This commit revises the agent model request and watch methods. This commit should solve issue #71 and causes newly added tests to pass. This commit revises the request method to eject any current intercepter when a new request is called. This commit also revises the agent watch method to only create a session when their are pending requests and wait until there is an available session before queuing requests. Signed-off-by: Lui de la Parra <Lui@mutesymphony.com>
1 parent 64c3ca9 commit 4af7d93

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/models/agent.model.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,17 @@ class Agent extends EmbeddedDocument {
210210
* @return {Object} request The configured axios instance to use for a request.
211211
*/
212212
request(data, parameters = {}) {
213-
instance.interceptors.request.use(
214-
({ httpAgent, httpsAgent, ...request }) =>
215-
new Promise((resolve, reject) =>
213+
const interceptor = instance.interceptors.request.use(
214+
({ httpAgent, httpsAgent, ...request }) => {
215+
instance.interceptors.request.eject(interceptor);
216+
return new Promise((resolve, reject) =>
216217
this.push({
217218
request: this.handleRequest(request),
218219
resolve,
219220
reject
220221
})
221-
)
222+
);
223+
}
222224
);
223225

224226
instance.interceptors.response.use(
@@ -401,15 +403,24 @@ class Agent extends EmbeddedDocument {
401403
const WATCHER = setTimeout(
402404
function watch() {
403405
this.connection.clear();
404-
if (this.queue.length > 0) {
406+
if (
407+
this.queue.length > 0 &&
408+
!this.connection.starting &&
409+
this.connection.available()
410+
) {
405411
this.shift();
406412
}
407413

408-
if (this.pending.length > 0 && this.connection.ready()) {
414+
if (
415+
this.pending.length > 0 &&
416+
!this.connection.starting &&
417+
this.connection.available()
418+
) {
409419
this.resolve();
410420
}
411421

412422
if (
423+
this.pending.length > 0 &&
413424
!this.connection.available() &&
414425
!this.connection.starting &&
415426
this.connection.sessions.length < this.concurrency

0 commit comments

Comments
 (0)