delvedor / hpagent

A ready to use http and https agent for working with proxies that keeps connections alive!
MIT License
181 stars 37 forks source link

Ignoring maxSockets #12

Open rasgele opened 3 years ago

rasgele commented 3 years ago

It seems maxSockets agent option is ignored when HttpProxyAgent is used.

const got = require('got');
const { HttpProxyAgent } = require('hpagent')
const http = require('http')

const opts = {
    maxSockets: 1,
    proxy: 'http://localhost:5555'
};
const hpagent = new HttpProxyAgent(opts);

const start = new Date();
const elapsed = () => { return (new Date().getTime() - start.getTime()) / 1000.0 }

// They run parallel on 2 different sockets, ignoring maxSockets
got('http://httpbin.org/delay/3', {
    agent: {
        http: hpagent
    }
}).then(() => console.log(`hpagent Request1 done in : ${elapsed()}`));

got('http://httpbin.org/delay/3', {
    agent: {
        http: hpagent
    }
}).then(() => console.log(`hpagent Request2 done in : ${elapsed()}`));

const agent = new http.Agent(opts);
// They run sequentially as maxSockets is 1
got('http://httpbin.org/delay/4', {
    agent: {
        http: agent
    }
}).then(() => console.log(`http.agent Request1 done in : ${elapsed()}`));

got('http://httpbin.org/delay/4', {
    agent: {
        http: agent
    }
}).then(() => console.log(`http.agent Request2 done in : ${elapsed()}`));

Console output is :

hpagent Request1 done in : 3.414
hpagent Request2 done in : 3.631
http.agent Request1 done in : 4.413
http.agent Request2 done in : 8.607

As you see, in contrast to http.Agent, hpagent sends second request immediately, without waiting for first socket to finish.

rasgele commented 3 years ago

Here is first 9 packages sent to proxy:

"No.","Time","Source","srcport","Destination","tgtport","Protocol","Length","Info"
"1","0.000000","127.0.0.1","53645","127.0.0.1","5555","TCP","108","53645  >  5555 [SYN] Seq=0 Win=64240 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"2","0.000978","127.0.0.1","5555","127.0.0.1","53645","TCP","108","5555  >  53645 [SYN, ACK] Seq=0 Ack=1 Win=65535 Len=0 MSS=65495 WS=1 SACK_PERM=1"
"3","0.001031","127.0.0.1","53645","127.0.0.1","5555","TCP","84","53645  >  5555 [ACK] Seq=1 Ack=1 Win=65536 Len=0"
"4","0.002656","127.0.0.1","53646","127.0.0.1","5555","TCP","108","53646  >  5555 [SYN] Seq=0 Win=64240 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"5","0.003438","127.0.0.1","5555","127.0.0.1","53646","TCP","108","5555  >  53646 [SYN, ACK] Seq=0 Ack=1 Win=65535 Len=0 MSS=65495 WS=1 SACK_PERM=1"
"6","0.003504","127.0.0.1","53646","127.0.0.1","5555","TCP","84","53646  >  5555 [ACK] Seq=1 Ack=1 Win=525568 Len=0"
"7","0.004306","127.0.0.1","53645","127.0.0.1","5555","HTTP","193","CONNECT httpbin.org:80 HTTP/1.1 "
"8","0.004346","127.0.0.1","5555","127.0.0.1","53645","TCP","84","5555  >  53645 [ACK] Seq=1 Ack=110 Win=65426 Len=0"
"9","0.005058","127.0.0.1","53646","127.0.0.1","5555","HTTP","193","CONNECT httpbin.org:80 HTTP/1.1 "

Packet 1 and 4 shows SYN packages over different sockets (from port 53645 and 53646).