jeffbski / wait-on

wait-on is a cross-platform command line utility and Node.js API which will wait for files, ports, sockets, and http(s) resources to become available
MIT License
1.87k stars 77 forks source link

Why so many connection attempts?? #68

Closed robstryker closed 4 years ago

robstryker commented 4 years ago

I have a simple java program running listening to a host:port combination, and when the socket is connected to, I print out the time of the connection. I repeat, this is already 100% up and running when my client uses your library.

My client uses your library as follows:

       var opts = {
            resources: [`tcp:localhost:8500`],
            delay: 1500, // initial delay in ms, default 0
            interval: 500, // poll interval in ms, default 250ms
        }
        return waitOn(opts);

What I expect to happen here is for the waitOn library to wait 1.5 seconds, make 1 connection, and, if it succeeds, to stop trying to connect and return.

What actually happens (according to output from my server) is the following:

15:01:48.439 [pool-2-thread-1] INFO  Connection made at 1594321308439
15:01:49.944 [pool-2-thread-1] INFO  Connection made at 1594321309944
15:01:50.444 [pool-2-thread-1] INFO  Connection made at 1594321310444
15:01:50.943 [pool-2-thread-1] INFO  Connection made at 1594321310943
15:01:51.444 [pool-2-thread-1] INFO  Connection made at 1594321311444
15:01:51.450 [pool-2-thread-1] INFO  Connection made at 1594321311450
15:01:51.942 [pool-2-thread-1] INFO  Connection made at 1594321311942

The timing seems mostly correct (every 500ms except for one which seems to have waited only 6ms) but what seems very very incorrect to me is that 7 attempts were made to connect, even though all 7 actually did connect. Why is the wait-on library making so many attempts? Why is it continuing to connect despite the first attempt succeeding?

In some cases, it is connecting 20 or 30 times instead of the expected 1 time. Are there any flags I can pass in to limit this? The comments in the code seem to indicate this is stability checking of some sort? Are there any limits here as to how many connections it expects to be stable?

jeffbski commented 4 years ago

Thanks for reaching out @robstryker.

If you want it to wait until a connection succeeds, fails, or times out then you can set the simultaneous option to limit how many concurrent connections to use. You could limit it to 1 (it would wait till first returns before making another) or whatever limit you want to allow at once.

Here is the CLI info

-s, --simultaneous

  Simultaneous / Concurrent connections to a resource, default Infinity
  Setting this to 1 would delay new requests until previous one has completed.
  Used to limit the number of connections attempted to a resource at a time.

and for using the API you would use

var opts = {
  resources: [`tcp:localhost:8500`],
  delay: 1500, // initial delay in ms, default 0
  interval: 500, // poll interval in ms, default 250ms
  simultaneous: 1 // limit connection attempts to one per resource at a time
};
return waitOn(opts);
jeffbski commented 4 years ago

I realized that I don't have that listed in the API section so I will add it now.

robstryker commented 4 years ago

Thanks so much for the quick response. This fixes my issue.