If you want to stop taking new clients when you are "busy", where busy is some status that you decide upon. Maybe it is number of active connections, etc., I see 2 options:

(1). Close your listen socket. This will not affect currently active sockets. When you decide that you are "not busy", restart your listen socket. In this case, the client can't tell the difference between "server down" and "server busy". The OS will know that no listen socket is bound to the port and should send back to the client "connection refused". Although simple, I don't think this is the best for your situation.

(2). Accept the connection, send back a protocol defined error message and then hang up (close the active socket). In this case, I would not fork a child, just have the main server process send the "not now" message and hang up (close the active socket)

Things to consider: (a) think about how to handle SIGPIPE - normally the main server process doesn't talk to clients, but in this case it would. You should allow for the fact that the write to the active socket could, in a degenerate case trigger a SIGPIPE. (b) Think about what happens if you have some rogue client keeps trying to connect after very short intervals (like 1 ms). (c) Of course you have to decide what "busy" means.

If you are writing both ends (the client and the server) this is the way to go. A very clearly defined and performant action happens when you are "busy". You can put anything in that "I'm busy...go away" message that you want - maybe how many connections you can handle on a "good day", "other url's to go to", it could be anything that you want!

(3). I don't think that moving the listen queue to zero will work. A typical value for this would like 5 or something like that. Anyway, the queue are the other connections rather than the pending one. So a value of zero would still allow a "pending one". You don't want any connections being left unhandled quickly. I don't think that the behavior of this option is very well defined and therefore I believe that it is far inferior to option 2.

Summary: go with option 2 above if you have control of the server and the client. If you do not have control over the client, then consider just accepting the connection and then proceed directly to "hang up", ie. closing the active socket. The client will get a SIGPIPE and know right away that you rejected this connection, well or at least that something didn't work with it and it will know this bit of information very fast.


In reply to Re: TCP server: How to reject connections when busy? by Marshall
in thread TCP server: How to reject connections when busy? by squirrel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.