Developing High Performance Asynchronous IO Applications
Pages: 1, 2, 3
Dying Is Not an Option
When writing async IO networking applications, you can't afford its death. When dying, the event loop will end and the application will quit. Instead, trap all exceptions and handle them gracefully without quitting the event loop. Do this by calling event_register_except_handler() before starting the event loop:
event_register_except_handler(\&event_exception_handler);
event_new(...)->add; # add the initial event(s)
event_mainloop(); # start the event loop
sub event_exception_handler {
my ($e, $exception, $e_type, @args) = @_;
# handle the exception
}
$e is the event object passed to the event handler when the exception was triggered. $exception is the exception variable, either a string generated from die() or an exception object. $e_type is the type of the event (EV_READ, EV_WRITE, etc.). @args will contain the arguments passed to event_new() following the callback. If something called event_new() with:
event_new($rfh, EV_READ, \&_handle_read_event, $ctx);
@args will contain the $ctx object.
In our application, an exception usually closes the file handle. We also use our own exception objects module, because we have found that all the existing exception handling systems available from CPAN, were too heavy for our needs. We found that throwing an exception object, rather than dying with a string, wasn't only more practical, it was also much faster, as in the exception handler we perform a numerical comparison, rather than matching strings.
Conclusion
Developing networking applications using event loops can be quite tricky at the beginning and requires a lot of well designed abstraction, to keep the code maintainable. Our MailChannels::AsyncConnection module that contains the abstraction went through many revisions. We hope to make it more generic and release it on CPAN once we become less busy.
We are extremely happy with the final product, because it is amazingly scalable and can handle thousands of concurrent long running connections. One of our customers recently underwent a heavy DoS attack on its mail servers, bombarding the server with thousands of requests from various botnets. Our single process non-threaded async IO application saved their systems from being brought down.
Stas Bekman co-authored two books on mod_perl: mod_perl2 User's Guide and Practical mod_perl.
Return to ONLamp.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 9 of 9.
-
Great article
2006-10-14 11:31:13 phredmoyer [Reply | View]
It's good to see more examples of event driven applications. Especially ones which allow you to leverage the power of mod_perl to do the actually processing work, and leave the connection pooling to a separate part of the architecture.
-
Think Twisted
2006-10-14 00:58:05 tekNico [Reply | View]
You could have done it with much less code, using a robust proven framework, and a cleaner language too, by using Twisted ( http://twistedmatrix.com/ ).
-
Not new or revolutionary
2006-10-13 07:13:17 c0desl1nger [Reply | View]
This idea is neither new nor revolutionary. The Symantec SMS 8160 does this now and the technology comes from their acquisition of TurnTide, which was doing this from the beginning of 2004. Prior to that it, was called SpamSquelcher which launched in late 2002. -
Not new or revolutionary
2006-10-14 19:57:13 msergeant1 [Reply | View]
You're right that Turntide does something fairly similar, but there's a big difference with the turntide and that's the amount of information you get back from it. When connections time out from an MTA based solution you not only know about it, you know at what stage they timed out, and what stage they timed out at.
This probably seems really unimportant to most people, but to anyone working on these kinds of solutions it is fairly vital information. -
Yes, new and revolutionary
2006-10-16 10:44:03 kensimpson [Reply | View]
[shillery alert, I'm the CEO of MailChannels] - It should be noted that Matt Sergeant works for MessageLabs, who use many TurnTide devices in their network, so his comments here are well founded in personal experience. And it should be noted that MessageLabs is a kick-ass company that does a fantastic job at fighting spam.
The main difference between TurnTide and our approach is that TurnTide traffic shapes at the network layer -- much like a firewall does. The TurnTide approach worked very well to punish high volume senders a few years ago, by forcing down their window size and backing up packets within their own infrastructure.
Today, however, most spam is sent by zombies, the owners of which could care less what's going on at the TCP layer -- since it's not their network that is being abused by high packets per second. What they _do_ care about is getting their messages dropped into the queue quickly so that they can move on. So what's important today is to 1) identify the zombies at zero-hour and 2) hold their SMTP connections open for as long as possible.
MailChannels Traffic Control is a hybrid of a traffic shaper and a proxy, with some nifty connection pooling features that help to reduce concurrency to the mail server. We believe this design is better suited to today's spamming reality where zombies are prevalent, rather than high volume senders, and high concurrency is the enemy, rather than high traffic volume.
Our directly in-field experience shows that spammers abort their connections within ten seconds, rather than hanging on -- because the economic proposition of spamming is predicated on fast delivery to the queue. -
Yes, new and revolutionary
2006-10-24 12:36:50 c0desl1nger [Reply | View]
[more shillery, I was a very early employee at TurnTide]
The TurnTide/SMS8160 is more than capable of handling zombies effectively. High concurrency has always been the enemy and the SMS8160 has always done both bandwidth _and_ connection shaping on ingress traffic. Since day one, way back in 2002, when it was called SpamSquelcher.
While Matt's points are salient, MailChannels' solution is in no way new or revolutionary, believe me. I've been doing this longer than anyone. I'm not knocking the solution, as I have no experience with MailChannels' software, just the characterization that its somehow groundbreaking.
-
Open Source implementations...
2006-10-12 17:59:46 msergeant1 [Reply | View]
The current trunk svn version of the qpsmtpd (http://qpsmtpd.org/) SMTP server implements this using Danga::Socket, supporting many thousands of concurrent connections.
For a web server in perl doing the same, see AxKit2 (http://www.apache.org/dist/xml/axkit/) . Very useful for those Ajax apps that produce high concurrency but you still want some perl code to be able to run.






