ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


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.



Sponsored by: