JezK
Edit File: page-Application_Timeouts.ri
U:RDoc::TopLevel[ i I"Application_Timeouts:ETcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[/S:RDoc::Markup::Heading: leveli: textI"Application Timeouts;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"LThis article focuses on _application_ setup for Rack applications, but ;TI"Lcan be expanded to all applications that connect to external resources ;TI"%and expect short response times.;T@ o; ;[ I"GThis article is not specific to unicorn, but exists to discourage ;TI"!the overuse of the built-in ;TI"J{timeout}[link:Unicorn/Configurator.html#method-i-timeout] directive ;TI"in unicorn.;T@ S; ; i;I"5ALL External Resources Are Considered Unreliable;T@ o; ;[I"MNetwork reliability can _never_ be guaranteed. Network failures cannot ;TI"Kbe detected reliably by the client (Rack application) in a reasonable ;TI""timeframe, not even on a LAN.;T@ o; ;[I"MThus, application authors must configure timeouts when interacting with ;TI"external resources.;T@ o; ;[I"8Most database adapters allow configurable timeouts.;T@ o; ;[I"@Net::HTTP and Net::SMTP in the Ruby standard library allow ;TI"configurable timeouts.;T@ o; ;[ I"DEven for things as fast as {memcached}[http://memcached.org/], ;TI".{dalli}[http://rubygems.org/gems/dalli], ;TI"9{memcached}[http://rubygems.org/gems/memcached] and ;TI"N{memcache-client}[http://rubygems.org/gems/memcache-client] RubyGems all ;TI"!offer configurable timeouts.;T@ o; ;[I"EConsult the relevant documentation for the libraries you use on ;TI"%how to configure these timeouts.;T@ S; ; i;I"!Rolling Your Own Socket Code;T@ o; ;[I"JUse non-blocking I/O and IO.select with a timeout to wait on sockets.;T@ S; ; i;I"0Timeout module in the Ruby standard library;T@ o; ;[I"KRuby offers a Timeout module in its standard library. It has several ;TI"(caveats and is not always reliable:;T@ o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o; ;[I"J/Some/ Ruby C extensions are not interrupted/timed-out gracefully by ;TI"Fthis module (report these bugs to extension authors, please) but ;TI"$pure-Ruby components should be.;T@ o;;0;[o; ;[I"FLong-running tasks may run inside `ensure' clauses after timeout ;TI"2fires, causing the timeout to be ineffective.;T@ o; ;[ I"LThe Timeout module is a second-to-last-resort solution, timeouts using ;TI"KIO.select (or similar) are more reliable. If you depend on libraries ;TI"Nthat do not offer timeouts when connecting to external resources, kindly ;TI"@ask those library authors to provide configurable timeouts.;T@ S; ; i;I"A Note About Filesystems;T@ o; ;[I"CMost operations to regular files on POSIX filesystems are NOT ;TI"Minterruptable. Thus, the "timeout" module in the Ruby standard library ;TI"Ecan not reliably timeout systems with massive amounts of iowait.;T@ o; ;[ I"DIf your app relies on the filesystem, ensure all the data your ;TI"Mapplication works with is small enough to fit in the kernel page cache. ;TI"LOtherwise increase the amount of physical memory you have to match, or ;TI"=employ a fast, low-latency storage system (solid state).;T@ o; ;[I"JVolumes mounted over NFS (and thus a potentially unreliable network) ;TI"Hmust be mounted with timeouts and applications must be prepared to ;TI"$handle network/server failures.;T@ S; ; i;I"The Last Line Of Defense;T@ o; ;[ I"NThe {timeout}[link:Unicorn/Configurator.html#method-i-timeout] mechanism ;TI"Gin unicorn is an extreme solution that should be avoided whenever ;TI"Kpossible. It will help catch bugs in your application where and when ;TI"Iyour application forgets to use timeouts, but it is expensive as it ;TI")kills and respawns a worker process.;T: @file@:0@omit_headings_from_table_of_contents_below0