Psgi Plack Sfpm

3,066 views
2,892 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,066
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
9
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Psgi Plack Sfpm

    1. 1. PSGI and Plack Tatsuhiko Miyagawa San Francsico.pm Nov. 24, 2009
    2. 2. Background
    3. 3. Web Frameworks
    4. 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. 5. Most of them run on mod_perl and CGI
    6. 6. Some run on FastCGI
    7. 7. Some run standalone
    8. 8. Very few supports non-blocking
    9. 9. Because:
    10. 10. No common server environment layers
    11. 11. CGI.pm
    12. 12. Runs “fine” on: CGI, FastCGI, mod_perl (1 & 2) Standalone (with HTTP::Server::Simple)
    13. 13. CGI.pm = LCD It’s also Perl core
    14. 14. Catalyst The most popular framework as of today
    15. 15. Catalyst::Engine::* Server abstractions. Well supported Apache, FCGI and Standalone No CGI.pm
    16. 16. CGI.pm Jifty, CGI::Application, Spoon mod_perl centric Mason, Sledge, PageKit, WebGUI Adapters Catalyst, Maypole, Squatting
    17. 17. Problems: Duplicated efforts No fair performance evaluations
    18. 18. Question: Can we share those adapters?
    19. 19. Answer: HTTP::Engine
    20. 20. HTTP::Engine Lots of adapters (FCGI, Apache2, POE) Clean Request/Response API Written by Yappo, tokuhirom and others
    21. 21. Problems
    22. 22. Mo[ou]se everywhere Mouse is light but still overspec for some env.
    23. 23. Monolithic All implementations share HTTP::Engine roles and builders, which is sometimes hard to adapt and has less place for optimizations.
    24. 24. APIs everywhere Most frameworks have their request/response API Sometimes there are gaps. Annoying to write bridges and wrappers
    25. 25. Solution
    26. 26. Steal good stuff from Python/Ruby
    27. 27. WSGI (Python) Rack
    28. 28. WSGI (PEP-333) mod_wsgi, Tornado, Paste, GAE Django, CherryPy, Pylons
    29. 29. Rack Passenger, Thin, rack, Heroku Rails, Merb, Sinatra
    30. 30. WSGI/Rack Completely separate interface from the actual implementation
    31. 31. Approach Split HTTP::Engine into three parts
    32. 32. Interface Implementations Utilities
    33. 33. PSGI (interface) Plack::Server (implementations) Plack::* (utilities)
    34. 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. 35. Who’s on board Authors of: Movable Type, WebGUI, Catalyst, Jifty, Moose, DateTime, DBIx::Class, CGI::Application, Squatting, SVK, Dancer
    36. 36. PSGI Perl Web Server Gateway Interface
    37. 37. Interface
    38. 38. WARNING You DON’T need to care about these interface details as an app developer.
    39. 39. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
    40. 40. PSGI application code reference $app = sub {...};
    41. 41. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
    42. 42. environment hash $env: CGI-like env variables + psgi.input, psgi.errors etc.
    43. 43. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
    44. 44. Response array ref with three elements status code, headers (array ref) and body (IO-like or array ref)
    45. 45. my $app = sub { my $env = shift; # ... return [ $status, $header, $body ]; };
    46. 46. $body IO::Handle-like getline() and close()
    47. 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. 48. WARNING You DON’T need to care about these interface details as an app developer. Becuase ...
    49. 49. Frameworks Write an adapter to return PSGI application code ref. (and forget about servers!)
    50. 50. Framework Adapters CGI::Application, Catalyst, Maypole Mason, Squatting, Mojo, HTTP::Engine etc.
    51. 51. Applications MT::App, WebGUI
    52. 52. use Foo; # is a Catalyst application Foo->setup_engine(‘PSGI’); my $app = sub { Foo->run };
    53. 53. Servers Set up $env, run the app and emits response out of $res
    54. 54. Plack namespace for servers and utilities
    55. 55. Plack::Server reference server implementations Standalone, FCGI, Apache2, CGI Standalone, Prefork, AnyEvent, Coro
    56. 56. Very fast 5000 QPS on standalone 15000 QPS with prefork :)
    57. 57. Utilities
    58. 58. Plackup Run PSGI app instantly from CLI (inspired by rackup)
    59. 59. DEMO
    60. 60. Middlewares
    61. 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. 62. Middlewares Static, AccessLog, ConditionalGET ErrorDocument, StackTrace etc.
    63. 63. Plack::Middleware reusable and extensible Middleware framework Plack::Builder DSL in .psgi
    64. 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. 65. Plack::App::URLMap Multiplex multiple apps Integrated with Builder DSL
    66. 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. 67. CGI::PSGI Easy migration from CGI.pm
    68. 68. Plack::Request like libapreq (Apache::Request) wrapper APIs for framework developers
    69. 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. 70. Plack::Test Unified interface to write tests with Mock HTTP and Live HTTP
    71. 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. 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. 73. Streaming event loop / long-poll
    74. 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. 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. 76. Non-blocking servers AnyEvent, Coro, POE, Danga::Socket
    77. 77. Non-blocking framework Tatsumaki (Demo later)
    78. 78. Other Servers
    79. 79. nginx embedded perl http://github.com/yappo/nginx-psgi-patchs
    80. 80. mod_psgi http://github.com/spiritloose/mod_psgi
    81. 81. evpsgi http://github.com/sekimura/evpsgi
    82. 82. Perlbal plugin http://github.com/miyagawa/Perlbal-Plugin-PSGI
    83. 83. Cloud
    84. 84. WSGI (PEP-333) mod_wsgi, Tornado, Paste, GAE Django, CherryPy, Pylons
    85. 85. Rack Passenger, Thin, rack, Heroku Rails, Merb, Sinatra
    86. 86. What if GAE Perl comes with PSGI ...
    87. 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. 88. http://github.com/miyagawa/Plack http://plackperl.org/
    89. 89. Questions?

    ×