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

RubyのOSSコードリーディング / ruby-oss-code-reading

23190387d424e33d6a1a807cea1e0469?s=47 Osamtimizer
November 30, 2019

RubyのOSSコードリーディング / ruby-oss-code-reading

23190387d424e33d6a1a807cea1e0469?s=128

Osamtimizer

November 30, 2019
Tweet

Transcript

  1. ࣌ాཧ(.01FQBCP *OD ࣛࣇౡ3VCZձٞ 3VCZͷ044 ίʔυϦʔσΟϯά

  2. J048FCΞϓϦέʔγϣϯΤϯδχΞ ࣌ాཧ!PTBNUJNJ[FS 46;63*ࣄۀ෦ϓϩμΫτνʔϜ

  3. ͸͡Ίʹ wൃදࢿྉ͸ޙ΄ͲΠϯλʔωοτ্Ͱެ։͠·͢ wIUUQTTQFBLFSEFDLDPNPTBNUJNJ[FS

  4. ࠓ೔࿩͢͜ͱ wίʔυϦʔσΟϯάͱ͸ w3VCZ044ͷίʔυϦʔσΟϯάΛ࢝Ί͖͔͚ͨͬ wίʔυϦʔσΟϯάͷ΍Γํ wͲͷΑ͏ʹಡΜ͔ͩ wಡΜͰɺมΘͬͨ͜ͱ

  5. ίʔυϦʔσΟϯάͱ͸

  6. ཁ͢ΔʹɺίʔυΛಡΉ

  7. ͚ͩͰ͸ͳ͍

  8. ಡΉ͜ͱʹΑͬͯɺ ιϑτ΢ΣΞͷ஌ݟΛਂΊΔ ίʔυϦʔσΟϯάͷ໨త

  9. ಡΈ࢝Ί͖͔͚ͨͬ

  10. ಡΈ࢝Ί͖͔͚ͨͬ w3BJMTΞϓϦΛۀ຿Ͱ։ൃ͍ͯ͠Δ wʮͲ͏΍ͬͯಈ͍͍ͯΔΜͩΖ͏ʁʯͱࢥ͏ॠ͕ؒଟ͍ w͡Ό͋ɺ3BJMTͰ࢖ΘΕ͍ͯΔ044ΛಡΜͰΈΑ͏

  11. ίʔυϦʔσΟϯάͷ΍Γํ ࣗݾྲྀʂ

  12. ίʔυϦʔσΟϯάͷ΍Γํ wʮ͜͏͠ͳ͚Ε͹͍͚ͳ͍ʯͱ͍͏ͷ͸ͳ͍ wείʔϓΛܾΊΔ wϝΠϯͷػೳΛɺ࢝Ί͔ΒऴΘΓ·Ͱ wෳ਺ਓͰಡΉ wυϥΠόʔΛҰਓܾΊͯɺϞϒϓϩܗࣜͰਐΊΔ

  13. ίʔυϦʔσΟϯάͷ༷ࢠ

  14. ίʔυϦʔσΟϯά ͓͢͢Ίπʔϧ

  15. (JU)VC ͦΕ͸ͦ͏

  16. (JU)VC 8FCϒϥ΢β Ͱ΋ɺ ݁ߏಡΈ΍͍͢

  17. +VNQUP%FpOJUJPO ΫϦοΫͰϝιουͷఆٛ΁

  18. 'JMFTFBSDIJO3FQPTJUPSZ ϦϙδτϦͷͲ͜Ͱ΋ɺUΩʔͰىಈ

  19. 'JMFTFBSDIJO3FQPTJUPSZ wಛʹ3VCZͱ૬ੑ͕ྑ͍ wSFRVJSFͰࢦఆ͢Δͷ͕ϑΝΠϧ໊ͳͷͰ

  20. ͲͷΑ͏ʹಡΜ͔ͩ

  21. ࣮ࡍʹಡΜͩίʔυΛ ަ͑ͯղઆ

  22. 3BDL

  23. 3BDL w8FCΞϓϦέʔγϣϯͱ8FCαʔόʔΛܨ͛ΔΠϯλʔϑΣΠε w3BDLΞϓϦέʔγϣϯͱͯ͠ͷϓϩτίϧΛຬͨ͢ΞϓϦΛαϙʔτ w.JEEMFXBSFߏ੒ΛऔΔ wconfig.ruΛ༻ҙ্ͨ͠ͰrackupίϚϯυͰىಈ

  24. ΤϯτϦϙΠϯτ͔ΒಡΉ

  25. SBDLVQ

  26. SBDLVQ require "rack" Rack::Server.start bin/rackup wͨͬͨߦ wRack::Server.startΛ࣮ߦ͍ͯ͠Δ͚ͩ

  27. 3VCZ044ʹ͋Γ͕ͪͳ͜ͱ ߦ

  28. SBDLVQ def self.start(options = nil) new(options).start end lib/rack/server.rb wͨͬͨߦ w৽͍͠ServerΦϒδΣΫτΛੜ੒͠ɺServer#startΛ࣮ߦ

  29. 3VCZ044ʹ͋Γ͕ͪͳ͜ͱ ߦ

  30. SBDLVQ def start server.run wrapped_app, options, &blk end def server

    @_server ||= Rack::Handler.get(options[:server]) @_server end lib/rack/server.rb
  31. 3BDL)BOEMFS

  32. SBDLVQ # *Handlers* connect web servers with Rack. # #

    Rack includes Handlers for Thin, WEBrick, FastCGI, CGI, SCGI # and LiteSpeed. # # Handlers usually are activated by calling <tt>MyHandler.run(myapp)</tt>. # A second optional hash can be passed to include server-specific # configuration. module Handler def self.get(server) lib/rack/handler.rb
  33. SBDLVQ # *Handlers* connect web servers with Rack. # #

    Rack includes Handlers for Thin, WEBrick, FastCGI, CGI, SCGI # and LiteSpeed. # # Handlers usually are activated by calling <tt>MyHandler.run(myapp)</tt>. # A second optional hash can be passed to include server-specific # configuration. module Handler def self.get(server) lib/rack/handler.rb
  34. )BOEMFS ‑ 3BDLͱ8FCαʔόʔΛܨ͛Δ ίϝϯτʹΑΔͱ

  35. 3VCZ044ʹ͋Γ͕ͪͳ͜ͱ ҙ֎ͱίϝϯτ͸໾ʹཱͭ

  36. SBDLVQ def self.get(server) … if klass = @handlers[server] klass.split("::").inject(Object) {

    |o, x| o.const_get(x) } lib/rack/handler.rb: L21-22 wͲ͏΍ΒɺHandler.getͰ͸͜ͷߦͷ݁Ռ͕ฦ͍ͬͯΔΒ͍͠
  37. @handlersʁ

  38. inject(Object) { |o, x| o.const_get(x) }ʁ

  39. None
  40. ࠔͬͨΒbinding.irb

  41. SBDLVQ if klass = @handlers[server] binding.irb klass.split("::").inject(Object) { |o, x|

    o.const_get(x) }
  42. CJOEJOHJSC irb(Rack::Handler):001:0> server => "puma" irb(Rack::Handler):002:0> @handlers => {“cgi”=>”Rack::Handler::CGI”, ...

    , ”puma”=>"Rack::Handler::Puma"} irb(Rack::Handler):003:0> @handlers[server] => "Rack::Handler::Puma" irb(Rack::Handler):004:0> klass.split("::") => ["Rack", "Handler", "Puma"]
  43. ࣮ࡍʹಈ͔ͯ͠ΈΔͱɺ Θ͔Γ΍͍͢

  44. ͜͜Ͱ໰୊͕

  45. inject(Object) { |o, x| o.const_get(x) }

  46. CJOEJOHJSC irb(Rack::Handler):001:0> result = ["Rack", "Handler", “Puma"].inject(Object) { |o, x|

    o.const_get(x) } => Rack::Handler::Puma irb(Rack::Handler):002:0> result.class => Module
  47. inject(Object)…ʁ

  48. None
  49. ࠔͬͨΒ EPDTSVCZMBOHPSH

  50. ఆ਺ wఆ਺͸ͦͷఆ਺͕ఆٛ͞ΕͨΫϥεϞδϡʔϧఆٛͷத ϝιουຊ ମ΍ωετͨ͠ΫϥεϞδϡʔϧఆٛதΛؚΈ·͢ ɺΫϥεΛܧঝ ͍ͯ͠ΔΫϥεɺϞδϡʔϧΛΠϯΫϧʔυ͍ͯ͠ΔΫϥε·ͨ͸Ϟ δϡʔϧ͔Βࢀর͢Δ͜ͱ͕Ͱ͖·͢ɻΫϥεఆٛͷ֎ τοϓϨϕ ϧ Ͱఆٛ͞Εͨఆ਺͸0CKFDUʹॴଐ͢Δ͜ͱʹͳΓ·͢ɻ

    EPDTSVCZMBOHPSH
  51. ఆ਺ wఆ਺͸ͦͷఆ਺͕ఆٛ͞ΕͨΫϥεϞδϡʔϧఆٛͷத ϝιουຊ ମ΍ωετͨ͠ΫϥεϞδϡʔϧఆٛதΛؚΈ·͢ ɺΫϥεΛܧঝ ͍ͯ͠ΔΫϥεɺϞδϡʔϧΛΠϯΫϧʔυ͍ͯ͠ΔΫϥε·ͨ͸Ϟ δϡʔϧ͔Βࢀর͢Δ͜ͱ͕Ͱ͖·͢ɻΫϥεఆٛͷ֎ τοϓϨϕ ϧ Ͱఆٛ͞Εͨఆ਺͸0CKFDUʹॴଐ͢Δ͜ͱʹͳΓ·͢ɻ

    EPDTSVCZMBOHPSH
  52. ͳΔ΄Ͳʂ

  53. SBDLVQ irb(main):001:0> require 'rack/handler/puma' => true irb(main):002:0> result = Object.const_get('Rack')

    => Rack irb(main):003:0> result.class => Module
  54. if klass = @handlers[server] klass.split("::").inject(Object) { |o, x| o.const_get(x) }

    αʔό໊ʢserverʣΛจࣈྻͰड͚औΓɺ Ұக͢Δ3BDL)BOEMFSͷϞδϡʔϧΦϒδΣΫτΛฦ͢ lib/rack/handler.rb SBDLVQ
  55. def start server.run wrapped_app, options, &blk end # server =>

    Rack Handler lib/rack/server.rb SBDLVQ
  56. SBDLVQ ‑ )BOEMFSܦ༝Ͱ 4FSWFSΛ্ཱͪ͛Δ

  57. 3VCZͷ044ΛಡΉΑ͏ʹ ͳͬͯɺมΘͬͨ͜ͱ

  58. ಡΉΑ͏ʹͳͬͯɺมΘͬͨ͜ͱ wίʔυΛಡΉ͕࣌ؒ૿͑ͨ w࣮ࡍʹಈ͔ͨ͠ΓυΩϡϝϯτΛಡΉश׳͕Ճ଎ͨ͠ w3BJMTΞϓϦέʔγϣϯͷཪଆ͕ཧղͰ͖ͨ wʮͳͥʯΛҙ࣮ࣝͯ͠૷͕Ͱ͖ΔΑ͏ʹ

  59. ·ͱΊ

  60. ·ͱΊ w3VCZ044ΛಡΉ͜ͱͰɺ3VCZͷ༷ʑͳ͜ͱΛ஌Δ͖͔͚ͬʹͳΔ w8FCϒϥ΢β͚ͩͰ΋ίʔυϦʔσΟϯά͠΍͍͢؀ڥ͕͋Δ w࣮ࡍʹಈ͔ͨ͠Γɺίϝϯτ΍ϦϑΝϨϯεΛಡΜͰΈΔͱྑ͍

  61. ͓ΘΓʹ

  62. &OKPZ3VCZ044