Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Help designing a threaded service

by BrowserUk (Patriarch)
on Jan 24, 2014 at 00:29 UTC ( [id://1071857]=note: print w/replies, xml ) Need Help??


in reply to Help designing a threaded service

What you've described sounds eminently doable. Though I'm going to question a couple of things and I'd probably make a few adjustments.

  • Why does the client need to reconnect every 3 or 4 seconds?

    Constantly re-making connections is a costly affair. That why http (web) servers went over to persistent connections.

    Why not let the client continue to listen and just send it data as it arrives from the remote system?

    Or perhaps better would be for the client to open a local UDP port and once it has communicated the hostname/command pair/and port number to the service it just sits back and monitors the UDP port to retrieve the output.

  • Why accumulate the data in a queue, rather than just a (non-shared) array or even a scalar?

    I guess this is because you envisage the client re-connecting to the servers primary thread to retrieve the output and so you need accumulate the data in a shared structure so the primary thread can get access to it.

    It think this is a bad design. You are making it so that your primary thread has to be involved in all data retrieval by every client; thus setting yourself up with a bottleneck. It also complicates the processing, by requiring shared data structures and uses more memory because shared data is duplicated.

I think -- without being fully aware of your requirements -- that a better design would be to have the primary (listening) thread start dedicated thread(s) (one or two depending upon the answer to the above) for each client/command and accumulate the buffered output entirely local to that (those) threads.


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.

Replies are listed 'Best First'.
Re^2: Help designing a threaded service
by Tommy (Chaplain) on Jan 24, 2014 at 01:18 UTC

    *Grins widely* ...

    Why you ask would I design it this way? Because I'm planning something evil: IE6 support. And here's the convoluted setup which I wanted to just keep segmented for simplicity's sake, but here we go:

    I have a catalyst web app serving a number of people. The app polls the server using jQuery XMLHTTP (AJAX) every N seconds (usually 2) with a task ID. I have scrolling text panes to show the output of the tasks.

    The problem which I must solve is how to get this streaming data from remote server C, while catalyst server B answers AJAX requests from client A. It's a polling system, where the browser sends a request to the catalyst server which then contacts the listening service for either initiation of long running SSH commands, or for content updates

    WEBSOCKETS WOULD BE BETTER, but I'm supporting IE6 and 7 here. And I also can't have users in browsers hitting the more private daemon that's doing the remote SSH commands. In this situation, the catalyst app acts as a firewall for it and only sends "trusted" commands based on sanitized user input.

    Now to clarify one thing I may have miscommunicated: each client connection does NOT go to the master thread. The sole responsibility of the master thread is to enforce timeouts on running tasks. "Listener" threads will be responsible for taking connections, looking up the running tasks in a shared hashref, opening the queue for task ID foo, spewing the update via ->dequeue() to the client, and hanging up. Then catalyst sends the updated data back to the web client in a well-formed AJAX response which gets populated into the scrolling faux terminal windows in the browser.

    The idea is that the browser window is divided into panes which form a dashboard. Each pane scrolls different server health/activity statistics (IO/CPU/Network/Memory). Above all that will be a moving line graph using google charts or the like. It's a grand scheme, but the hard part is the back end plumbing that I'm trying to create. The front-end stuff is easy.

    Tommy
    A mistake can be valuable or costly, depending on how faithfully you pursue correction
      "Listener" threads will be responsible for taking connections,

      "Listener threads". Plural? On different ports?


      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.

        So I sheepishly take it from your reply that I can't have a multi-threaded listener setup the way I see multiple forks of a fork-based network server taking connections on one port. I had some proof-of-concept code barely forming last night but hadn't got far enough to find out if it "worked"...Thanks for saving me the trouble.

        A I wrong in my understanding that in either case it seems like there is still one listening "doorman" who hands off the business of respond()-ing to his subservients?

        The more I think about this, the more I'm leaning toward going with Net::Server and using some other way to share data between its forks than the much more (imo) elegant shared variables via threads::shared. I've used it many times and it's soooo stable. I just really didn't want to do it that way.

        Tommy
        A mistake can be valuable or costly, depending on how faithfully you pursue correction

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1071857]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found