Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rack: A Case Study in Design

Rack: A Case Study in Design

Explores the nature of HTTP1 requests and how it relates the design and implementation of Rack in ruby. Concludes with a discussion on the future of, and successors to, rack.

Michael Cordell

October 01, 2015
Tweet

More Decks by Michael Cordell

Other Decks in Programming

Transcript

  1. Why? Rack underpins the lion-share of successful ruby web frameworks

    Understanding the implementation and API is crucial to better framework utilization Excellent case study of design
  2. Common Gateway Interface (CGI) Spec written in 1993 by National

    Center for Supercomputing Applications Allowed web servers to interact with executables and dynamically generate webpages Along with passed query params server also sets up a set of environmental variables
  3. SERVER_SOFTWARE SERVER_NAME GATEWAY_INTERFACE SERVER_PROTOCOL SERVER_PORT REQUEST_METHOD PATH_INFO PATH_TRANSLATED SCRIPT_NAME QUERY_STRING

    REMOTE_HOST REMOTE_ADDR AUTH_TYPE SERVER_PORT REMOTE_USER REMOTE_IDENT CONTENT_TYPE CONTENT_LENGTH HTTP_ACCEPT HTTP_ACCEPT_LANGUAGE HTTP_USER_AGENT HTTP_COOKIE
  4. The Client ɂ curl -X POST -H "Content-Type: application/json” \

    -d '{ "name": "michael" }' \ http://localhost:9292/hello
  5. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

    HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep. 2015.23:32:19.GMT.. Connection:.Keep-Alive....
  6. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

    HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep. 2015.23:32:19.GMT.. Connection:.Keep-Alive.... {"hello":"Welcome.michael"}
  7. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

    HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep. 2015.23:32:19.GMT.. Connection:.Keep-Alive.... {"hello":"Welcome.michael"}
  8. rack rules A rack app: implements a call method that

    accepts a hash on CGI-like environment call returns an array containing: a status code a hash of response headers an object that represents the body that responds to .each and contains strings for the body
  9. adapter pattern Without this we would have f * s

    implementations where f is the # of frameworks and s is the number of servers rails-puma rails-unicorn sinatra-puma sinatra-unicorn
  10. adapter pattern Instead we have f + s number of

    implementations Each framework speaks rack and each sever speaks rack
  11. pipeline pattern higher-level architecture pattern break a larger task in

    to discrete steps each step’s output is passed as the input to the next step also known as pipes and filters (step)
  12. An example: warden warden is a “general rack authentication framework”

    used by devise (the rails auth gem) redirects the user to a failure app with a 401 status code when not authenticated app can trigger auth failure by throwing a warden error: throw(:warden)
  13. ENV Despite the simple API, a lot of complexity hides

    behind that env/request hash The env hash is setup like a CGI environment but we are getting farther and farther away from CGI environment NoMethodError on an Object vs. nil from a hash
  14. Rack 2? Unlikely that there will ever be a re-write

    of rack by the core team Decided on a “hold” the torch approach to maintenance in Aug. 2014
  15. Rack 2? Unlikely that there will ever be a re-write

    of rack by the core team Decided on a “hold” the torch approach to maintenance in Aug. 2014
  16. Web sockets with Action Cable Will be released in Rails

    5, announced at rails conf 2015, code open-sourced in early July Offers a JS and backend integration
  17. “Action Cable seamlessly integrates websockets with the rest of your

    Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable.”
  18. twitter: @mike_cordell speaker_deck: Request and Response by tenderlove: https://www.youtube.com/1EeWXojdqvU José

    Valim: Why your web framework should not adopt Rack API: http://blog.plataformatec.com.br/2012/06/ why-your-web-framework-should-not-adopt- rack-api/