Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to count the number of pending clients in socket queue?

by mrbark (Novice)
on Nov 17, 2013 at 04:01 UTC ( #1062965=perlquestion: print w/ replies, xml ) Need Help??
mrbark has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I use IO::Socket from where I accept TCP clients with accept().

Is there a way to know how many clients are still waiting in the queue to be accepted?
(I would like to avoid the use of other modules if possible, because I fork a lot...)

Thank you!

Comment on How to count the number of pending clients in socket queue?
Re: How to count the number of pending clients in socket queue?
by Anonymous Monk on Nov 17, 2013 at 04:14 UTC

    Is there a way to know how many clients are still waiting in the queue to be accepted?

    No

Re: How to count the number of pending clients in socket queue?
by BrowserUk (Pope) on Nov 17, 2013 at 05:54 UTC

    What would you do with the information if you could get it?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to count the number of pending clients in socket queue?
by kschwab (Priest) on Nov 17, 2013 at 21:05 UTC

    You can't tell how many are waiting to be accepted, but you can tell if there are "zero" or "one or more" waiting. You don't provide much detail, but I assume you might be asking this so that you can avoid blocking on accept(). If that's the case, this example might be helpful.

      Thanks for your answer guys.

      I have no blocking problem, my process has nothing to do while waiting. I would just need this information to know when I need to add a new server to the load balancing network...

      In theory my system shouldn't have more than 1 client waiting to be accepted, otherwise I want to add a server.

      Is there any way to know in /proc e.g. or any other system/kernel way?

      Thanks!
        Greetings, mrbark.

        As it appears you're on a *NIX box. Aside from poll. The man pages for ifconfig, lsof, netstat, etc. Might give you some interesting information, that you could ask Perl to provide you in informative, and useful ways. :)

        HTH

        --Chris

        #!/usr/bin/perl -Tw
        use Perl::Always or die;
        my $perl_version = (5.12.5);
        print $perl_version;

        No, there's not an easy way to see that. The /proc info, lsof, ss, etc, don't show the current listenq depth. However, there are pretty robust pieces of software that are able to determine when to spin up more theads/instances/processes without using that information. Most of them use the "I'm out of spare workers...they are all busy" condition as the trigger to add more resources..not "work is piling up at the front door" event, which is what the current listenq depth is.

        I believe (not 100% sure but seems to work on my Debian with 3.10.3 kernel) that in /proc/net/tcp the sockets in listen-state have a state-code of "0A" and for those the receive-queue entry holds the number of pending (i.e. not yet accepted) connections.

        This is how such an entry looks like:

        1: 00000000:1A0B 00000000:0000 0A 00000000:00000002 00:00000000 00000000  1000        0 1117932 1 ffff880031e70040 100 0 0 10 0

        The fist column is the local address (here a server-socket listening on port 6667) the next the remote address, then the state (here 0A for listening) then the transmit and receive queue-lengths.

        Here my server has 2 connections pending.

        So using the address your server listens you should be able to extract the information you need from /proc/net/tcp.

        Maybe Linux::Proc::Net::TCP can help here, but I have never used it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1062965]
Approved by chacham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-12-21 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls