Psgi Plack Sfpm

  • 1,618 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Testing December 21st, 2009 V2
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,618
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
5
Comments
1
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PSGI and Plack Tatsuhiko Miyagawa San Francsico.pm Nov. 24, 2009
  • 2. Background
  • 3. Web Frameworks
  • 4. Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious Tripletail Konstrukt Reaction Jifty Cyclone3 WebGUI OpenInteract Squatting Dancer CGI::Application Nanoa Ark Angelos Noe Schenker Tatsumaki Web::Simple Apache2::REST SweetPea Hydrant
  • 5. Most of them run on mod_perl and CGI
  • 6. Some run on FastCGI
  • 7. Some run standalone
  • 8. Very few supports non-blocking
  • 9. Because:
  • 10. No common server environment layers
  • 11. CGI.pm
  • 12. Runs “fine” on: CGI, FastCGI, mod_perl (1 & 2) Standalone (with HTTP::Server::Simple)
  • 13. CGI.pm = LCD It’s also Perl core
  • 14. Catalyst The most popular framework as of today
  • 15. Catalyst::Engine::* Server abstractions. Well supported Apache, FCGI and Standalone No CGI.pm
  • 16. CGI.pm Jifty, CGI::Application, Spoon mod_perl centric Mason, Sledge, PageKit, WebGUI Adapters Catalyst, Maypole, Squatting
  • 17. Problems: Duplicated efforts No fair performance evaluations
  • 18. Question: Can we share those adapters?
  • 19. Answer: HTTP::Engine
  • 20. HTTP::Engine Lots of adapters (FCGI, Apache2, POE) Clean Request/Response API Written by Yappo, tokuhirom and others
  • 21. Problems
  • 22. Mo[ou]se everywhere Mouse is light but still overspec for some env.
  • 23. Monolithic All implementations share HTTP::Engine roles and builders, which is sometimes hard to adapt and has less place for optimizations.
  • 24. APIs everywhere Most frameworks have their request/response API Sometimes there are gaps. Annoying to write bridges and wrappers
  • 25. Solution
  • 26. Steal good stuff from Python/Ruby
  • 27. WSGI (Python) Rack
  • 28. WSGI (PEP-333) mod_wsgi, Tornado, Paste, GAE Django, CherryPy, Pylons
  • 29. Rack Passenger, Thin, rack, Heroku Rails, Merb, Sinatra
  • 30. WSGI/Rack Completely separate interface from the actual implementation
  • 31. Approach Split HTTP::Engine into three parts
  • 32. Interface Implementations Utilities
  • 33. PSGI (interface) Plack::Server (implementations) Plack::* (utilities)
  • 34. Who’s on board Benjamin Trott, Yuval Kogman, Stevan Little, Shawn M Moore, Mark Stosberg, Matt S Trout, Jesse Vincent, Chia-liang Kao, Dave Rolsky, John Beppu ...
  • 35. Who’s on board Authors of: Movable Type, WebGUI, Catalyst, Jifty, Moose, DateTime, DBIx::Class, CGI::Application, Squatting, SVK, Dancer
  • 36. PSGI Perl Web Server Gateway Interface
  • 37. Interface
  • 38. WARNING You DON’T need to care about these interface details as an app developer.
  • 39. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
  • 40. PSGI application code reference $app = sub {...};
  • 41. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
  • 42. environment hash $env: CGI-like env variables + psgi.input, psgi.errors etc.
  • 43. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
  • 44. Response array ref with three elements status code, headers (array ref) and body (IO-like or array ref)
  • 45. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
  • 46. $body IO::Handle-like getline() and close()
  • 47. IO::Handle-like We really envy Python/Ruby for built-in iterators (Perl’s filehandle is also an object, but it really sucks)
  • 48. WARNING You DON’T need to care about these interface details as an app developer. Becuase ...
  • 49. Frameworks Write an adapter to return PSGI application code ref. (and forget about servers!)
  • 50. Framework Adapters CGI::Application, Catalyst, Maypole Mason, Squatting, Mojo, HTTP::Engine etc.
  • 51. Applications MT::App, WebGUI
  • 52. use Foo; # is a Catalyst application Foo->setup_engine(‘PSGI’); my $app = sub { Foo->run };
  • 53. Servers Set up $env, run the app and emits response out of $res
  • 54. Plack namespace for servers and utilities
  • 55. Plack::Server reference server implementations Standalone, FCGI, Apache2, CGI Standalone, Prefork, AnyEvent, Coro
  • 56. Very fast 5000 QPS on standalone 15000 QPS with prefork :)
  • 57. Utilities
  • 58. Plackup Run PSGI app instantly from CLI (inspired by rackup)
  • 59. DEMO
  • 60. Middlewares
  • 61. my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; my $mw = sub { my $env = shift; # do something with $env my $res = $app->($env); # do something with $res; return $res; };
  • 62. Middlewares Static, AccessLog, ConditionalGET ErrorDocument, StackTrace etc.
  • 63. Plack::Middleware reusable and extensible Middleware framework Plack::Builder DSL in .psgi
  • 64. my $app = sub { return [ $status, $header, $body ]; }; use Plack::Builder; builder { enable “Static”, root => “/htdocs”, path => qr!^/static/!; enable “Deflater”; # gzip/deflate $app; }
  • 65. Plack::App::URLMap Multiplex multiple apps Integrated with Builder DSL
  • 66. use CatApp; use CGIApp; my $c1 = sub { CatApp->run }; my $c2 = sub { CGIApp->run_psgi }; use Plack::Builder; builder { mount “/cat” => $c1; mount “/cgi-app” => builder { enable “StackTrace”; $c2; }; }
  • 67. CGI::PSGI Easy migration from CGI.pm
  • 68. Plack::Request like libapreq (Apache::Request) wrapper APIs for framework developers
  • 69. use Plack::Request; my $app = sub { my $req = Plack::Request->new(shift); my $body = “Hello “ . $req->param(‘n’); my $res = $req->new_response(200); $res->content_type(‘text/plain’); $res->body($body); return $res->finalize; };
  • 70. Plack::Test Unified interface to write tests with Mock HTTP and Live HTTP
  • 71. use Plack::Test; use HTTP::Request::Common; my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; test_psgi app => $app, client => sub { my $cb = shift; my $req = GET “http://localhost/foo”; my $res = $cb->($req); # test $res; };
  • 72. use Plack::Test; use HTTP::Request::Common; $Plack::Test::Impl = “Server”; my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; test_psgi app => $app, client => sub { my $cb = shift; my $req = GET “http://localhost/foo”; my $res = $cb->($req); # test $res; };
  • 73. Streaming event loop / long-poll
  • 74. # Pull streaming for blocking server use IO::Handle::Util qw(io_from_getline); my $app = sub { my $env = shift; my $io = io_from_getline sub { return $chunk; # undef when done }; return [ $status, $header, $io ]; };
  • 75. # Push streaming for non-blocking server use AnyEvent; use JSON; my $app = sub { my $env = shift; return sub { my $respond = shift; my $w = $respond->([ 200, $headers ]); AnyEvent::Example->fetch(sub { $w->write(JSON::encode_json($_[0])); $w->close; }); }; };
  • 76. Non-blocking servers AnyEvent, Coro, POE, Danga::Socket
  • 77. Non-blocking framework Tatsumaki (Demo later)
  • 78. Other Servers
  • 79. nginx embedded perl http://github.com/yappo/nginx-psgi-patchs
  • 80. mod_psgi http://github.com/spiritloose/mod_psgi
  • 81. evpsgi http://github.com/sekimura/evpsgi
  • 82. Perlbal plugin http://github.com/miyagawa/Perlbal-Plugin-PSGI
  • 83. Cloud
  • 84. WSGI (PEP-333) mod_wsgi, Tornado, Paste, GAE Django, CherryPy, Pylons
  • 85. Rack Passenger, Thin, rack, Heroku Rails, Merb, Sinatra
  • 86. What if GAE Perl comes with PSGI ...
  • 87. Summary • PSGI is an interface, Plack is the code. • We have many (pretty fast) servers. • We have adapters and tools for most web frameworks. • Use it!
  • 88. http://github.com/miyagawa/Plack http://plackperl.org/
  • 89. Questions?