On Wed, Jan 15, 2014 at 12:18 PM, Nacho B <[email protected]> wrote:

> Hi, I am very, very interested in the non-blocking features of Mojolicious
> and its server, but I am blocked in my first try.
>
> This is a simple remote port test:
>
> use Socket;
>
> my ($ip, $protocol, $port, $myhouse, $yourhouse, $log);
>
> $ip = $site->{ip};
> $port = $site->{port};
> $protocol = getprotobyname('tcp');
>
> socket(SOCKET, PF_INET, SOCK_STREAM, $protocol);
> $yourhouse = inet_aton($ip);
> $myhouse = sockaddr_in($port, $yourhouse);
>
> if (!connect(SOCKET, $myhouse)) {
>     $site->{status} = 'KO';
> } else {
>     $site->{status} = 'OK';
>     close SOCKET || die "close: $!";
> }
>
>
> I am using Morbo for executing it. And I have other pages in the app.
>
> When the remote IP and port are OK, it works fine. But if I use a wrong
> (not responding or fake) IP, it hangs, waiting forever,* and I can't use
> any other pages in the application*. Those pages wait until I cancel this
> process.
>

I ran your code without Mojo and connect() blocked for 3 seconds.  I'm not
sure why your app would hang "forever".


> I do not want a timeout, but the non-blocking "promise".
>
> Do I need to manage events for such a little function (like I should do in
> ActionScript, for example)? If so, it seems that there is no such thing as
> a non-blocking feature, isn't it?
>

As sri has made me aware, if your app is going to block, give it more
processes and make sure that each process handles only one concurrent
connection.  I'm assuming you are using connect() within a route.  So, with
multiple processes, when someone connects to the connect route and it
blocks, the managing process of your app has additional unused processes to
send subsequent requests for other routes to and then *those* requests
won't block while your connect route is in the process of blocking.

See this code: https://gist.github.com/s1037989/8457175

And then run it like this: $ perl /tmp/socket.pl prefork -w 10 -c 1
And then test it like this: $ ab -n 3 -c 3 http://127.0.0.1:3000/connect |
grep "Time taken for tests"
And you'll see that the total time for ab to complete the 3 requests was 3
seconds, even tho each request took itself 3 seconds -- that's because
there were 10 processes available to handle the 3 connections, leaving
plenty more processes for you to hit up the other non-blocking routes.

Conversely, run it like this: $ perl /tmp/socket.pl daemon
And then test it like this: $ ab -n 3 -c 3 http://127.0.0.1:3000/connect |
grep "Time taken for tests"
And you'll see that the total time for ab to complete the 3 requests
was *9* seconds
because there was only one process available to handle each of the blocking
requests.  The first one takes 3 seconds, then the next one takes 3
seconds, and so on.  3 requests * 3 seconds each = 9 total seconds.

Or, better of course, is to do as Ben gave to you and replace your Socket
code with a Mojo::IOLoop->client implementation instead.


> I think that there is something very, very basic about the non-blocking
> "way of life" that I am missing. Any help very appreciated.
>

I hope you've been following this thread -- *very* informative!

-- 
*----------------------------------------------------------------*
*Keystone IT made the following notation*
*----------------------------------------------------------------*
*Email Confidentiality Notice: The information contained in this 
transmission is confidential, proprietary or privileged and may be subject 
to protection under the law, including the Health Insurance Portability and 
Accountability Act (HIPAA). *

*This message is for the sole use of the intended individual or entity to 
whom it is addressed. If you are not the intended recipient, you are 
notified that any use, distribution or copying of the message is strictly 
prohibited and may subject you to criminal or civil penalties. If you 
received this transmission in error, please contact the sender immediately 
at (314) 621-9500 and delete the material from all computers.*

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to