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

Go で Network Programming するためのよもやま話 / Network Programming with Go

Go で Network Programming するためのよもやま話 / Network Programming with Go

GDG DevFest Tokyo 2018 の発表に使ったスライドです。
Goについて以下の話をしました。

- netパッケージの基本的なインタフェース
- netpoll による I/O 多重化の仕組み
- ブロッキング I/O をアンブロックする方法
- TLS対応について
 - Public Key Pinning のやり方など

Tomohiro Takezawa

September 01, 2018
Tweet

More Decks by Tomohiro Takezawa

Other Decks in Programming

Transcript

  1. Go Ͱ Network
    Programming ͢ΔͨΊ
    ͷΑ΋΍·࿩
    Tomohiro Takezawa


    View full-size slide

  2. ࣗݾ঺հ
    • ஛ᖒ ༑ത
    • Github: ttakezawa
    • Twitter: @takezawa
    • גࣜձࣾKyashॴଐ
    • όοΫΤϯυશൠ
    • ಛʹ VISA ΍ QUICPay (Google Pay) ͷϓϩηγϯάγεςϜͳͲ

    View full-size slide

  3. ࣮຿ͱωοτϫʔΫϓϩάϥϛϯά
    • ࣮ࡍͷͱ͜Ζɺ࢓ࣄͰ࢖͏ػձ͋Δʁ
    • Kyash ͷۀ຿Ͱ͸ඞਢͳͱ͜Ζ͕͋Δ
    • ΫϨδοτΧʔυͷϓϩηγϯάۀ຿
    • ௿ϨΠϠʔͷཧղ͕ਂ·Δͱڧ͍

    View full-size slide

  4. ࿩͢͜ͱͱɺ࿩͞ͳ͍͜ͱ
    • ࿩͢͜ͱ
    • ιέοτϓϩάϥϛϯάશൠ
    • Go ʹ͓͚Δ I/O ͷ࢓૊Έ
    • ࿩͞ͳ͍͜ͱ
    • HTTP
    • νϟωϧ

    View full-size slide

  5. ࠓ೔ͷΞδΣϯμ
    • ωοτϫʔΫϓϩάϥϛϯάͷجຊతͳΠϯ
    λϑΣʔε
    • netpoll ʹΑΔ I/OଟॏԽͷ࢓૊Έ
    • ϒϩοΩϯά I/O ΛΩϟϯηϧ͢Δํ๏
    • TLSରԠʹ͍ͭͯ

    View full-size slide

  6. GoͷωοτϫʔΫϓϩάϥϛ
    ϯάͷجຊతͳΠϯλϑΣʔ
    εʹ͍ͭͯ

    View full-size slide

  7. ·ͣ৮Γ͔Β
    • جຊతͳΠϯλϑΣʔεʹ͍ͭͯ
    • ந৅౓ߴΊʹઆ໌

    View full-size slide

  8. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    αʔόଆ
    جຊతͳϓϩτίϧʹ͓͍ͯڞ௨ͷྲྀΕ
    ίωΫγϣϯ ΫϥΠΞϯτ
    αʔό͸ListenͰ଴ͪड͚͠ɺΫϥΠΞϯτ͸
    DialͰ઀ଓ͢Δ

    View full-size slide

  9. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    αʔόଆ
    جຊతͳϓϩτίϧʹ͓͍ͯڞ௨ͷྲྀΕ
    ίωΫγϣϯ ΫϥΠΞϯτ
    αʔό͸ListenͰ଴ͪड͚͠ɺΫϥΠΞϯτ͸
    DialͰ઀ଓ͢Δ

    View full-size slide

  10. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    αʔόଆ ίωΫγϣϯ ΫϥΠΞϯτ
    αʔό͸ListenͰ଴ͪड͚͠ɺΫϥΠΞϯτ͸
    DialͰ઀ଓ͢Δ
    جຊతͳϓϩτίϧʹ͓͍ͯڞ௨ͷྲྀΕ

    View full-size slide

  11. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    αʔόଆ ίωΫγϣϯ ΫϥΠΞϯτ
    ͓ޓ͍ʹίωΫγϣϯΦϒδΣΫτͰίϛϡχ
    έʔγϣϯΛߦ͏
    جຊతͳϓϩτίϧʹ͓͍ͯڞ௨ͷྲྀΕ

    View full-size slide

  12. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    netύοέʔδʹ͸ϓϩτίϧ͝ͱʹͦΕͧΕͷ࣮૷͕͋Δ
    net.DialIP(…)
    net.DialTCP(…)
    net.DialUDP(…)
    net.DialUnix(…)
    net.IPConn
    net.TCPConn
    net.UDPConn
    net.UnixConn
    net.ListenIP(…)
    net.ListenTCP(…)
    net.ListenUDP(…)
    net.ListenUnix(…)
    net.ListenUnixgram(…)

    View full-size slide

  13. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    netύοέʔδʹ͸ϓϩτίϧ͝ͱʹͦΕͧΕͷ࣮૷͕͋Δ
    net.DialIP(…)
    net.DialTCP(…)
    net.DialUDP(…)
    net.DialUnix(…)
    net.IPConn
    net.TCPConn
    net.UDPConn
    net.UnixConn
    net.ListenIP(…)
    net.ListenTCP(…)
    net.ListenUDP(…)
    net.ListenUnix(…)
    net.ListenUnixgram(…)

    View full-size slide

  14. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    netύοέʔδʹ͸ϓϩτίϧ͝ͱʹͦΕͧΕͷ࣮૷͕͋Δ
    net.DialIP(…)
    net.DialTCP(…)
    net.DialUDP(…)
    net.DialUnix(…)
    net.IPConn
    net.TCPConn
    net.UDPConn
    net.UnixConn
    net.ListenIP(…)
    net.ListenTCP(…)
    net.ListenUDP(…)
    net.ListenUnix(…)
    net.ListenUnixgram(…)

    View full-size slide

  15. ίωΫγϣϯཱ֬·ͰͷϓϦϛςΟϒ
    $POO
    -JTUFO %JBM
    netύοέʔδʹ͸ϓϩτίϧ͝ͱʹͦΕͧΕͷ࣮૷͕͋Δ
    net.ListenIP(…)
    net.ListenTCP(…)
    net.ListenUDP(…)
    net.ListenUnix(…)
    net.ListenUnixgram(…)
    net.DialIP(…)
    net.DialTCP(…)
    net.DialUDP(…)
    net.DialUnix(…)
    net.IPConn
    net.TCPConn
    net.UDPConn
    net.UnixConn

    View full-size slide

  16. ΠϯλϑΣʔεΛ࢖͓͏
    લϖʔδͰڍ͛ͨ΋ͷ͸͢΂ͯ۩ମ࣮૷
    ۩ମ࣮૷Ͱ͸ͳ͘ɺΠϯλϑΣʔεΛ࢖͑͹
    • ςετ͕͠΍͍͢
    • ൚༻Խ͕͠΍͍͢

    View full-size slide

  17. ΠϯλϑΣʔεΛ࢖͓͏
    ΠϯλʔϑΣʔεͰѻ͏৔߹ʹ͸ɺେ͖͘2छྨʹ෼͔ΕΔ
    • ετϦʔϜܕϓϩτίϧ
    • TCP
    • ετϦʔϜܕͷUnixυϝΠϯιέοτ
    • σʔλάϥϜܕϓϩτίϧ
    • UDP
    • IP
    • σʔλάϥϜܕͷUnixυϝΠϯιέοτ

    View full-size slide

  18. ΠϯλϑΣʔεΛ࢖͏ͱ
    $POO
    -JTUFO %JBM
    net.Listen(…)
    net.ListenPacket(…)
    net.Dial(…)
    net.Conn
    net.PacketConn
    • Listen
    • ετϦʔϜܕͷ৔߹: net.Listen() → net.Listener → net.Conn
    • σʔλάϥϜܕͷ৔߹: net.ListenPacket() → net.PacketConn
    • Dial͸͍ͣΕͷϓϩτίϧͰ΋ڞ௨
    • net.Dial() → net.Conn

    View full-size slide

  19. ΠϯλϑΣʔεΛ࢖͏ͱ
    $POO
    -JTUFO %JBM
    net.Listen(…)
    net.ListenPacket(…)
    net.Dial(…)
    net.Conn
    net.PacketConn
    • Listen
    • ετϦʔϜܕͷ৔߹: net.Listen() → net.Listener → net.Conn
    • σʔλάϥϜܕͷ৔߹: net.ListenPacket() → net.PacketConn
    • Dial͸͍ͣΕͷϓϩτίϧͰ΋ڞ௨
    • net.Dial() → net.Conn

    View full-size slide

  20. ΠϯλϑΣʔεΛ࢖͏ͱ
    $POO
    -JTUFO %JBM
    net.Listen(…)
    net.ListenPacket(…)
    net.Dial(…)
    net.Conn
    net.PacketConn
    • Listen
    • ετϦʔϜܕͷ৔߹: net.Listen() → net.Listener → net.Conn
    • σʔλάϥϜܕͷ৔߹: net.ListenPacket() → net.PacketConn
    • Dial͸͍ͣΕͷϓϩτίϧͰ΋ڞ௨
    • net.Dial() → net.Conn

    View full-size slide

  21. ΠϯλϑΣʔεΛ࢖͏ͱ
    $POO
    -JTUFO %JBM
    net.Listen(…)
    net.ListenPacket(…)
    net.Dial(…)
    net.Conn
    net.PacketConn
    • Listen
    • ετϦʔϜܕͷ৔߹: net.Listen() → net.Listener → net.Conn
    • σʔλάϥϜܕͷ৔߹: net.ListenPacket() → net.PacketConn
    • Dial͸͍ͣΕͷϓϩτίϧͰ΋ڞ௨
    • net.Dial() → net.Conn

    View full-size slide

  22. ΠϯλϑΣʔεΛ࢖͏ͱ
    $POO
    -JTUFO %JBM
    net.Listen(…)
    net.ListenPacket(…)
    net.Dial(…)
    net.Conn
    net.PacketConn
    • Listen
    • ετϦʔϜܕͷ৔߹: net.Listen() → net.Listener → net.Conn
    • σʔλάϥϜܕͷ৔߹: net.ListenPacket() → net.PacketConn
    • Dial
    • ͲͪΒͷ৔߹΋: net.Dial() → net.Conn

    View full-size slide

  23. ετϦʔϜܕϓϩτίϧͷ৔߹
    • αʔό/ΫϥΠΞϯτ͸ڞʹ net.Conn ΠϯλϑΣʔε
    ͱͯ͠ίωΫγϣϯΛѻ͏
    • αʔόଆ
    • net.Listen() Ͱ଴ͪड͚Λ࢝Ίɺฦ͖ͬͯͨListener
    ΠϯλϑΣʔεͷ Accept() ͰίωΫγϣϯͷ஋Λಘ
    Δ
    • ΫϥΠΞϯτଆ

    • net.Dial() Ͱ઀ଓ͢Δ


    View full-size slide

  24. ετϦʔϜܕϓϩτίϧͷ৔߹
    • TCPͰૹड৴Λ͢Δίʔυ͸ྫ͑͹͜͏ͳΔ
    αʔόଆ
    ΫϥΠΞϯτଆ
    • ετϦʔϜܕͷ৔߹ɺίωΫγϣϯཱ͕֬͢ΔͱɺαʔόଆɺΫϥΠΞϯ
    τଆͷ྆ํʹ net.Conn ΠϯλϑΣʔεΛຬͨ͢ΦϒδΣΫτ͕ฦͬͯ͘Δ

    View full-size slide

  25. ετϦʔϜܕϓϩτίϧͷ৔߹
    • TCPͰૹड৴Λ͢Δίʔυ͸ྫ͑͹͜͏ͳΔ
    αʔόଆ
    ΫϥΠΞϯτଆ
    • ετϦʔϜܕͷ৔߹ɺίωΫγϣϯཱ͕֬͢ΔͱɺαʔόଆɺΫϥΠΞϯ
    τଆͷ྆ํʹ net.Conn ΠϯλϑΣʔεΛຬͨ͢ΦϒδΣΫτ͕ฦͬͯ͘Δ

    View full-size slide

  26. ετϦʔϜܕϓϩτίϧͷ৔߹
    • TCPͰૹड৴Λ͢Δίʔυ͸ྫ͑͹͜͏ͳΔ
    αʔόଆ
    ΫϥΠΞϯτଆ
    • ετϦʔϜܕͷ৔߹ɺίωΫγϣϯཱ͕֬͢ΔͱɺαʔόଆɺΫϥΠΞϯ
    τଆͷ྆ํʹ net.Conn ΠϯλϑΣʔεΛຬͨ͢ΦϒδΣΫτ͕ฦͬͯ͘Δ

    View full-size slide

  27. ετϦʔϜܕϓϩτίϧͷ৔߹
    • TCPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ
    ΫϥΠΞϯτଆ
    • ετϦʔϜܕͷ৔߹ɺίωΫγϣϯཱ͕֬͢ΔͱɺαʔόଆɺΫϥΠΞϯ
    τଆͷ྆ํʹ net.Conn ΠϯλϑΣʔεΛຬͨ͢஋͕ฦͬͯ͘Δ

    View full-size slide

  28. ετϦʔϜܕϓϩτίϧͷ৔߹
    • αʔό/ΫϥΠΞϯτ͸ͱ΋ʹ net.Conn ΠϯλʔϑΣʔεͰίωΫ
    γϣϯΛѻ͏
    • networkͱͯ͠ "tcp"ɺ"unix"ɺ"unixpacket" ͳͲ͕ࢦఆՄೳ
    OFU-JTUFO

    αʔόଆ

    net.Listen("tcp", "127.0.0.1:8080")

    net.Listen("unix", "socketfile")
    ΫϥΠΞϯτଆ

    net.Dial("tcp", "127.0.0.1:8080")

    net.Dial("unix", "socketfile")
    OFU-JTUFOFS "DDFQU
    OFU$POO
    OFU%JBM
    OFU$POO


    View full-size slide

  29. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • αʔόଆ
    • net.PacketConn ΠϯλϑΣʔεͷ஋Λฦ͢
    • net.PacketConn ͸௨৴૬ख͕ݶఆ͞Ε͍ͯͳ͍৔߹ʹ
    ࢖༻͞ΕΔίωΫγϣϯͷΠϯλϑΣʔεͰ͋Γɺ
    net.PacketConn Λ࢖ͬͯૹ৴͢Δͱ͖͸ɺૹ৴ઌΛࢦ
    ఆ͢Δඞཁ͕͋Δ
    • ΫϥΠΞϯτଆ
    • ௨৴૬ख͕ݶఆ͞ΕΔͷͰɺ net.Conn ΠϯλϑΣʔεͷ
    ஋͕࢖͑Δ


    View full-size slide

  30. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  31. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  32. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  33. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  34. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  35. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  36. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • UDPͰૹड৴Λ͢Δίʔυͷྫ
    αʔόଆ ΫϥΠΞϯτଆ
    • ΫϥΠΞϯτଆ͸ɺΠϯλϑΣʔε্͸ετϦʔϜܕͱมΘΒͳ͍
    • αʔόଆ͸௨৴૬ख͕ఆ·Βͳ͍ͷͰɺ net.PacketConn ΠϯλϑΣʔεͷΦϒδΣΫτͰ
    ௨৴͢Δ͜ͱʹͳΔ
    • PacketConn.ReadFrom ͕௨৴૬खͷΞυϨε΋ฦͯ͘͠ΕΔ
    • PacketConn.WriteTo Ͱ૬खઌͷΞυϨεΛࢦఆ͢Δඞཁ͕͋Δ

    View full-size slide

  37. σʔλάϥϜܕϓϩτίϧͷ৔߹
    • σʔλάϥϜܕϓϩτίϧͷ৔߹
    • αʔόଆ͸ net.ListenPacket() Λ࢖ͬͯ଴ͪड͚Λ։࢝
    • αʔόଆ͸ net.PacketConnɺΫϥΠΞϯτଆ͸ net.Conn ͰίωΫγϣϯΛѻ͏
    • networkͱͯ͠ "udp"ɺ"unixgram"ɺ"ip" ͳͲ͕ࢦఆՄೳ
    OFU-JTUFO1BDLFU

    αʔόଆ

    net.ListenPacket("udp", "127.0.0.1:8080")

    net.ListenPacket("unixgram", "socketfile")
    ΫϥΠΞϯτଆ

    net.Dial("tcp", "127.0.0.1:8080")

    net.Dial("unix", "socketfile")
    OFU1BDLFU$POO
    OFU$POO
    OFU%JBM

    ΫϥΠΞϯτͱ௨৴Λ։࢝͢Δલ
    ʹ࡞ΒΕΔίωΫγϣϯͷΠϯλʔ
    ϑΣʔε

    ௨৴૬ख͕1ͭʹఆ·͍ͬͯͳ͍
    ͱ͖ʹ࢖ΘΕΔ
    ΫϥΠΞϯτ(Dial)ଆ͸ɺ௨৴૬
    ख͕ఆ·͓ͬͯΓɺ net.Conn Π
    ϯλʔϑΣʔεʹ౷Ұ͞Ε͍ͯΔ

    View full-size slide

  38. ωοτϫʔΫϓϩάϥϛϯάͷ
    جຊతͳΠϝʔδ͸Θ͔ͬͨ

    View full-size slide

  39. Go ͷ৔߹Ͳ͏΍ͬͯ I/O ͷ
    ฒྻԽΛͯ͠Δʁ

    View full-size slide

  40. I/O ͷॲཧΛฒྻԽ
    I/O ͸ඇৗʹ஗͍
    • ฒྻԽ
    • I/O Λ଴͍ͬͯΔؒʹผͷॲཧΛߦ͍͍ͨ
    • I/O ͷ଴ػΛ௿ίετͰ
    • ΫϥΠΞϯτ1ສ୆(C10K)໰୊

    View full-size slide

  41. I/O ʹؔ͢Δ༻ޠͷઆ໌
    • ϒϩοΩϯά I/O
    • I/O ॲཧ͕׬ྃɺΤϥʔɺλΠϜΞ΢τʹͳΔ͔ɺͳͲ௨஌͕͘Δ·Ͱ
    ࣮ߦΛઌʹਐ·ͳ͍ॲཧ
    • ϊϯϒϩοΩϯά I/O ͷ৔߹͸ɺ I/O ͕ॲཧதͰ΋࣮ߦΛ͙͢ʹਐΊΔ
    • I/O ͷଟॏԽ
    • ෳ਺ͷ I/O ͷ௨஌ΛҰ౓ʹ଴ͯΔΑ͏ʹ͢Δ͜ͱ
    • ଟॏԽ͍ͯ͠ͳ͍৔߹ʹ͸଴ͪड͚ର৅͝ͱʹγεςϜίʔϧ͕ඞཁ
    ʹͳΔ
    • OSʹΑΓɺ selectɺpollɺepoll(Linux)ɺkqueue(BSDܥ) ͳͲͷγες
    ϜίʔϧͰ࣮ݱ͞ΕΔ
    • ΠϕϯτۦಈϞσϧͷ࣮૷ʹ͸΄΅ඞਢͳ࢓૊Έ

    View full-size slide

  42. ฒྻԽΛ͢ΔͨΊͷ
    ϓϩάϥϛϯάϞσϧ

    View full-size slide

  43. ϚϧνεϨουϞσϧ
    • Α͋͘ΔҰൠతͳϓϩάϥϛϯάϞσϧ ͦͷ1
    • ϑϩʔۦಈϞσϧ
    • ฒྻʹॲཧ͍ͨ͠୯ҐͰεϨουΛ࡞ͬͯɺεϨου͝
    ͱʹ I/O Λߦ͏
    • ྫ͑͹ίωΫγϣϯ͝ͱʹεϨου΋͘͠͸ϓϩηε
    Λੜ੒͠ɺͦΕͧΕͷεϨουͰ I/O Λߦ͏ɺͱ͍͏
    Α͏ͳΞϓϩʔνʹͳΔ
    • ฒྻ౓͕ߴ͍ͱ͖(ίωΫγϣϯ͕ଟ͍ͱ͖)ʹɺͦͷ෼
    εϨουΛͨ͘͞Μ࢖͏͜ͱʹͳΔͷͰɺίετ͕େ͖
    ͍

    View full-size slide

  44. ΠϕϯτۦಈϞσϧ
    • Α͋͘ΔҰൠతͳϓϩάϥϛϯάϞσϧ ͦͷ2
    • ΠϕϯτΛ଴ػ͠ɺىͬͨ͜Πϕϯτʹ͍ͭͯΠϕϯτϋϯυϥΛ࣮ߦ͠ଓ͚
    Δɺͱ͍͏Ϟσϧ
    • Node.jsɺRubyͷEventMachineɺPythonͷTwisted ͳͲ
    • I/O ΛଟॏԽͯ͠γϯάϧεϨουͰͨ͘͞Μͷ I/O Λѻ͑Δ
    • ೉͍͠ɺͱݴΘΕ΍͍͢Ϟσϧ
    • ओʹγϯάϧεϨουͰಈ࡞͢ΔͷͰɺ
    • ͕͔͔࣌ؒΔॲཧΛ࣮ߦͯ͠͠·͏ͱଞͷॲཧ͕શ࣮͘ߦ͞Εͳ͘ͳΔ
    • ෳ਺ίΞͷCPUΛੜ͔ͤͳ͍
    • ॲཧͷ࣮ߦ͞ΕΔॱং͕Θ͔Βͳ͍ɺͳͲ

    View full-size slide

  45. GoͰͷ΍Γํ
    ϚϧνεϨουͱΠϕϯτۦಈͷϋΠϒϦουͷΑ͏ͳ࢓૊Έ
    • Go͸εϨουΑΓ΋ܰྔͳgoroutineΛɺεϨου্ʹׂΓ౰
    ͯͳ͕Β࣮ߦ͢Δ
    • goroutine্Ͱ I/O Λ࣮ߦ͢Δͱgoroutine͸ϒϩοΫ͞ΕΔ
    • ϒϩοΫ͍ͨ͠਺͚ͩgoroutine͕ඞཁ
    • GoͷϥϯλΠϜ͸ I/O ΛଟॏԽͯ͠଴ͪड͚͢Δ
    • ଴ͪड͚ͷͨΊʹେྔͷεϨουΛඞཁͱ͠ͳ͍
    • I/O ଴ͪͷͱ͖goroutine͸εϨουʹׂΓ౰ͯ͞Εͳ͍

    View full-size slide

  46. ϢʔβͷGoίʔυࢹ఺Ͱ͍͏ͱ
    • GoͷϓϩάϥϛϯάΠϯλϑΣʔεͱͯ͠͸ I/O ͸ϒ
    ϩοΩϯά
    • ྫ͑͹ conn.Read(buffer) ͱ͢ΔͱΠϕϯτ͕͋Δ·
    Ͱɺͦͷgoroutine͸εέδϡʔϥ͔Β֎͞Εͯ଴ͪঢ়
    ଶ(ϒϩοΩϯά)ʹͳΔ
    • ͜ͷ I/O ʹ͍ͭͯ௨஌͕͘Δͱɺ conn.Read(buffer) ͕
    ฦͬͯ͘Δ

    View full-size slide

  47. GoͷϥϯλΠϜ͕΍ͬͯ͘Ε͍ͯΔ
    • Ϣʔβͷίʔυ্Ͱ͸ϒϩοΩϯάૢ࡞ʹ
    ͳ͍ͬͯΔ΋ͷΛɺ಺෦తʹ͸ଟॏԽͯ͠औ
    Γѻͬͯ͘Ε͍ͯΔ
    • ϒϩοΩϯά I/O Λ ଟॏԽ I/O ʹ͍ͯ͠Δ
    netpoll ͱ͍͏࢓૊Έ͕͋Δ

    View full-size slide

  48. netpollͷ࢓૊Έʹ͍ͭͯ

    View full-size slide

  49. netpollͱ͸ʁ
    • goroutine ͷϒϩοΩϯάI/O ΛଟॏԽͯ͠؂ࢹ͢Δ࢓૊Έ
    • ଟॏԽʹ͍ͭͯͷ࣮૷ͰɺҎԼͷγεςϜίʔϧΛ࢖༻͍ͯ͠Δ
    • epoll(Linux)
    • kqueue(BSDܥ)
    • event ports(Solaris)
    • IOCP(Windows)

    View full-size slide

  50. netpollʹΑΔଟॏԽͷྲྀΕ
    ҎԼͷྲྀΕΛ Linux (epoll) ͷྫͰऔΓ্͛ͯઆ໌͢Δ
    1. ιέοτΛ࡞Δͱ͖ʹ४උ͢Δ
    2. I/O ॲཧ (ReadͳͲ) Λ։࢝͢Δ
    3. I/O ϒϩοΩϯά͢Δ goroutine Λεέδϡʔϥ͔Β֎͢
    4. ௨஌Λड͚औͬͨΒɺgoroutineΛ࣮ߦΩϡʔʹ໭͢
    5. εέδϡʔϥ͸࣮ߦ଴ͪͷΩϡʔ͔ΒgoroutineΛऔΓग़
    ͠ɺOSεϨουʹׂΓ౰࣮ͯͯߦ͢Δ

    View full-size slide

  51. 1. ιέοτΛ࡞Δͱ͖ͷ४උ
    ιέοτΛ࡞ΔλΠϛϯάͰڞ௨ͷinitؔ਺Λ࣮ߦ͍ͯ͠Δ
    ҎԼɺGoͷιʔείʔυ͔Βൈਮ
    go1.11/src/net/sock_posix.go go1.11/src/os/file_unix.go
    • DialɺListenɺListenPacketɺAcceptɺϑΝΠϧΛ։͘ɺͦΕͧΕͷͱ͖ʹڞ௨ͷॳظԽॲཧ͕ߦΘΕΔ
    • Go 1.9 ͔ΒωοτϫʔΫ I/O͚ͩͰͳ͘ϑΝΠϧ I/Oʹ͍ͭͯ΋ netpoll ͰଟॏԽ͢Δ࣮૷͕ೖͬͨ
    • ͱ͸͍͑ɺৗʹ͋ΒΏΔ I/O Λ netpoll ͰଟॏԽ͍ͯ͠Δͱ͍͏Θ͚Ͱ͸ͳ͘ɺos.Open() ΍
    os.Pipe() ͳͲݶఆ͞ΕͨϑΝΠϧσΟεΫϦϓλʹ͍ͭͯ netpoll ͰଟॏԽ͞ΕΔ
    go1.11/src/net/fd_unix.go

    View full-size slide

  52. 1. ιέοτΛ࡞Δͱ͖ͷ४උ
    • ڞ௨initؔ਺ : pollDesc.init
    • ιέοτ΍ϑΝΠϧͷσΟεΫϦϓλॳظԽʹݺͼग़͞ΕΔɻ
    • epoll ͕ॳظԽ͞Ε͍ͯͳ͍৔߹ʹ͸ epoll ͷॳظԽ epoll_create Λ͢Δ
    • epoll ͷ ؂ࢹ଴ͪʹొ࿥ EPOLL_CTL_ADD ͢Δ
    go1.11/src/internal/poll/fd_poll_runtime.go
    • runtime_pollServerInit ʹͯ
    epoll_create ͕1౓͚ͩ (sync.Once
    Ͱอূ) ͞ΕΔ
    • runtime_pollOpen Ͱ؂ࢹ଴ͪʹొ࿥
    (EPOLL_CTL_ADD)͢Δ

    View full-size slide

  53. 1. ιέοτΛ࡞Δͱ͖ͷ४උ
    go1.11/src/internal/poll/fd_poll_runtime.go
    • runtime_pollServerInit ʹͯ
    epoll_create ͕1౓͚ͩ (sync.Once
    Ͱอূ) ͞ΕΔ
    • runtime_pollOpen Ͱ؂ࢹ଴ͪʹొ࿥
    (EPOLL_CTL_ADD)͢Δ
    • ڞ௨initؔ਺ : pollDesc.init
    • ιέοτ΍ϑΝΠϧͷσΟεΫϦϓλॳظԽʹݺͼग़͞ΕΔɻ
    • epoll ͕ॳظԽ͞Ε͍ͯͳ͍৔߹ʹ͸ epoll ͷॳظԽ epoll_create Λ͢Δ
    • epoll ͷ ؂ࢹ଴ͪʹొ࿥ EPOLL_CTL_ADD ͢Δ

    View full-size slide

  54. 2. I/O ॲཧΛ։࢝͢Δ
    I/O ॲཧ͕ϒϩοΫ͢Δ৔߹ʹɺ pollDesc.wait() Λݺͼग़͢
    go1.11/src/internal/poll/fd_unix.go
    go1.11/src/internal/poll/fd_poll_runtime.go
    ReadFrom ΍ WriteTo ͳͲʹͭ
    ͍ͯ΋ɺଟॏԽͷඞཁ͕͋Δ৔
    ߹͸ಉ༷ʹ pollDesc.wait() ͢
    ΔΑ͏ʹͳ͍ͬͯΔ

    View full-size slide

  55. 3. I/OϒϩοΩϯά͢ΔgoroutineΛεέδϡʔϥ͔Β֎͢
    go1.11/src/internal/poll/fd_poll_runtime.go
    runtime.gopark ʹΑΓ
    goroutine͕εέδϡʔϥͷ
    ࣮ߦΩϡʔ͔ΒऔΓআ͔ΕΔ
    go1.11/src/runtime/netpoll.go
    runtime_pollWait͔Β
    netpollblockΛݺͼग़͍ͯ͠Δ
    ϢʔβͷGoίʔυ͔ΒݟΔͱɺ I/O ॲཧͷλΠϛ
    ϯάͰ goroutine ͕ϒϩοΫ͞ΕΔ

    View full-size slide

  56. go1.11/src/internal/poll/fd_poll_runtime.go
    runtime.gopark ʹΑΓ
    goroutine͕εέδϡʔϥͷ
    ࣮ߦΩϡʔ͔ΒऔΓআ͔ΕΔ
    go1.11/src/runtime/netpoll.go
    runtime_pollWait͔Β
    netpollblockΛݺͼग़͍ͯ͠Δ
    ϢʔβͷGoίʔυ͔ΒݟΔͱɺ I/O ॲཧͷλΠϛ
    ϯάͰ goroutine ͕ϒϩοΫ͞ΕΔ
    3. I/OϒϩοΩϯά͢ΔgoroutineΛεέδϡʔϥ͔Β֎͢

    View full-size slide

  57. go1.11/src/internal/poll/fd_poll_runtime.go
    runtime.gopark ʹΑΓ
    goroutine͕εέδϡʔϥͷ
    ࣮ߦΩϡʔ͔ΒऔΓআ͔ΕΔ
    go1.11/src/runtime/netpoll.go
    runtime_pollWait͔Β
    netpollblockΛݺͼग़͍ͯ͠Δ
    ϢʔβͷGoίʔυ͔ΒݟΔͱɺ I/O ॲཧͷλΠϛ
    ϯάͰ goroutine ͕ϒϩοΫ͞ΕΔ
    3. I/OϒϩοΩϯά͢ΔgoroutineΛεέδϡʔϥ͔Β֎͢

    View full-size slide

  58. 4. ௨஌Λड͚औͬͨΒgoroutineΛ࣮ߦΩϡʔʹ໭͢
    go1.11/src/runtime/proc.go
    ιέοτΛ؂ࢹͯ͠௨஌Λड͚औͬͨ৔߹ʹ͸ɺ֘౰͢ΔgoroutineΛ࣮
    ߦΩϡʔʹೖΕΔ
    • εέδϡʔϦϯάͰ͙͢ʹ࣮
    ߦͰ͖Δgoroutine͕ݟ͔ͭ
    Βͳ͍৔߹ʹ͸ɺnetpoll()
    ʹΑΓ epoll_pwait Λ࣮ߦ͠
    ͯɺI/O ͷ௨஌Λ଴ͭ
    • netpoll() ͸௨஌͕͋ͬͨ
    goroutine Λࢦ͢஋gpΛฦ
    ͢ͷͰɺ injectglist(gp) ʹΑ
    Γ࣮ߦΩϡʔʹೖΕΔ
    ͜ͷޙɺεέδϡʔϥ͸࣮ߦ଴ͪͷΩϡʔ͔ΒgoroutineΛऔΓग़͠ɺ࠶౓OS
    εϨουʹׂΓ౰࣮ͯͯߦ͢Δ

    View full-size slide

  59. 4. ௨஌Λड͚औͬͨΒgoroutineΛ࣮ߦΩϡʔʹ໭͢
    go1.11/src/runtime/proc.go
    • εέδϡʔϦϯάͰ͙͢ʹ࣮
    ߦͰ͖Δgoroutine͕ݟ͔ͭ
    Βͳ͍৔߹ʹ͸ɺnetpoll()
    ʹΑΓ epoll_pwait Λ࣮ߦ͠
    ͯɺI/O ͷ௨஌Λ଴ͭ
    • netpoll() ͸௨஌͕͋ͬͨ
    goroutine Λࢦ͢஋gpΛฦ
    ͢ͷͰɺ injectglist(gp) ʹΑ
    Γ࣮ߦΩϡʔʹೖΕΔ
    ιέοτΛ؂ࢹͯ͠௨஌Λड͚औͬͨ৔߹ʹ͸ɺ֘౰͢ΔgoroutineΛ࣮
    ߦΩϡʔʹೖΕΔ
    ͜ͷޙɺεέδϡʔϥ͸࣮ߦ଴ͪͷΩϡʔ͔ΒgoroutineΛऔΓग़͠ɺ࠶౓OS
    εϨουʹׂΓ౰࣮ͯͯߦ͢Δ

    View full-size slide

  60. 4. ௨஌Λड͚औͬͨΒgoroutineΛ࣮ߦΩϡʔʹ໭͢
    go1.11/src/runtime/proc.go
    • εέδϡʔϦϯάͰ͙͢ʹ࣮
    ߦͰ͖Δgoroutine͕ݟ͔ͭ
    Βͳ͍৔߹ʹ͸ɺnetpoll()
    ʹΑΓ epoll_pwait Λ࣮ߦ͠
    ͯɺI/O ͷ௨஌Λ଴ͭ
    • netpoll() ͸௨஌͕͋ͬͨ
    goroutine Λࢦ͢஋gpΛฦ
    ͢ͷͰɺ injectglist(gp) ʹΑ
    Γ࣮ߦΩϡʔʹೖΕΔ
    ͜ͷޙɺεέδϡʔϥ͸࣮ߦ଴ͪͷΩϡʔ͔ΒgoroutineΛऔΓग़͠ɺ࠶౓OS
    εϨουʹׂΓ౰࣮ͯͯߦ͢Δ
    ιέοτΛ؂ࢹͯ͠௨஌Λड͚औͬͨ৔߹ʹ͸ɺ֘౰͢ΔgoroutineΛ࣮
    ߦΩϡʔʹೖΕΔ

    View full-size slide

  61. I/O ͕ goroutine ΛϒϩοΫ
    ͢Δ͜ͱ͸Θ͔ͬͨɻ
    runtime ͕ଟॏԽ͢Δ͜ͱ΋
    Θ͔ͬͨɻ

    View full-size slide

  62. Ϣʔβͷ೚ҙͷλΠϛϯάͰ
    ϒϩοΩϯά I/O Λऴྃͤ͞
    ͯɺgoroutineΛಈ͔͢ʹ
    ͸ʁ

    View full-size slide

  63. ϒϩοΩϯά I/O Λ೚ҙͷλΠϛϯάͰऴ͍ྃͤͨ͞৔߹
    ೚ҙͷλΠϛϯάͰ I/O Λதஅ͍ͤͨ͜͞ͱ͕͋Δ
    • ΞϓϦέʔγϣϯΛऴྃ࣌ͷॲཧΛ࡞ΓࠐΉ৔߹
    • SetDeadline Ͱ͸ରԠͰ͖ͳ͍தஅॲཧΛೖΕ͍ͨ৔߹
    ͳͲ
    • ϒϩοΩϯάૢ࡞ (Read) ͷલʹϑϥάνΣοΫ
    Λ௥Ճ͢Δ͚ͩͰ͸ɺϒϩοΩϯάૢ࡞͕ղআ
    ͞ΕΔΘ͚Ͱ͸ͳ͍
    • ͜ͷΑ͏ͳ࣮૷Λͯ͠͠·͏ͱɺ goroutine
    ͷऴྃʹ͸࠷େͰ1෼Ҏ্଴ͬͯ͠·͏
    • SetDeadline Λ୹͘͢Δͱɺ଴ͭ࣌ؒ͸୹͘
    ͳΔ͕ɺγεςϜίʔϧΛݺͼग़͠·͘Δ͜
    ͱʹͳΔͷͰɺίετ͕ߴ͍
    ଴ͨ͞Εͯ͠·͏ྫ

    View full-size slide

  64. ϒϩοΩϯά I/O Λ೚ҙͷλΠϛϯάͰऴ͍ྃͤͨ͞৔߹
    ओʹ2ͭͷΞϯϒϩοΫํ๏͕͋Δ
    1. context.Context ΠϯλϑΣʔεͰΩϟϯηϧ͢Δ
    • DialͳͲɺ৽͘͠ιέοτΛ࡞੒͢Δ৔߹ͳͲ
    ͸ɺCloseର৅ʹͳΔΦϒδΣΫτ͕ଘࡏ͍ͯ͠ͳ
    ͍ͷͰ context Λ࢖ͬͨ΍ΓํʹͳΔ
    2. Read ΍ WriteΛ͍ͯ͠ΔΦϒδΣΫτΛ Close() ͢Δ

    View full-size slide

  65. contextΛ࢖ͬͯΞϯϒϩοΫͤ͞Δ৔߹
    context.Context ʹରԠ͍ͯ͠ΔΠϯλϑΣʔε͕͋Ε͹ɺͦΕΛ࢖͏
    • Dial() Ͱ͋Ε͹ net.Dialer ͷ DialContext() ͕࢖͑Δ
    • Listen() ΋ net.ListenConfig Ͱ ΩϟϯηϧରԠ
    • net.ListenConfig ͸ Go 1.11 Ͱ௥Ճ͞Εͨ࢓૊Έ
    • ͱ͸͍͑ɺListen()ͰϒϩοΫ͢Δͷ͸ DNS Ͱ໊લղܾ͢Δͱ͜ΖͷΈ

    View full-size slide

  66. context.Context ʹରԠ͍ͯ͠ΔΠϯλϑΣʔε͕͋Ε͹ɺͦΕΛ࢖͏
    • Dial() Ͱ͋Ε͹ net.Dialer ͷ DialContext() ͕࢖͑Δ
    • Listen() ΋ net.ListenConfig Ͱ ΩϟϯηϧରԠ
    • net.ListenConfig ͸ Go 1.11 Ͱ௥Ճ͞Εͨ࢓૊Έ
    • ͱ͸͍͑ɺListen()ͰϒϩοΫ͢Δͷ͸ DNS Ͱ໊લղܾ͢Δͱ͜ΖͷΈ
    contextΛ࢖ͬͯΞϯϒϩοΫͤ͞Δ৔߹

    View full-size slide

  67. Close() Λ࢖ͬͯΞϯϒϩοΫͤ͞Δ৔߹
    contextରԠ͕ͳ͍৔߹ʹ͸ɺClose() ͢Δ
    • ྫ͑͹ net.Listener ͷ Accept() ͸ context ΛҾ਺ʹऔΒͳ͍
    • Listener ʹ SetDeadline() Λ࢖͏͜ͱͰλΠϜΞ΢τͷઃఆͳΒՄೳ
    • Listener Λ Close() ͨ͠λΠϛϯάͰAccpet()͕ΞϯϒϩοΫ͞ΕΔ
    • Close() ͰΞϯϒϩοΫͨ͠ͱ͍͏͜ͱΛ఻͑ΔͨΊʹɺdoneνϟωϧͰ఻͍͑ͯΔ

    View full-size slide

  68. contextରԠ͕ͳ͍৔߹ʹ͸ɺClose() ͢Δ
    • net.Listener ͷ Accept() ʹ͸ context ରԠ͕ͳ͍
    • Listener ʹ SetDeadline() Λ࢖͏͜ͱͰλΠϜΞ΢τͷઃఆͳΒՄೳ
    • Listener Λ Close() ͨ͠λΠϛϯάͰAccept()͕ΞϯϒϩοΫ͞ΕΔ
    • Close() ͰΞϯϒϩοΫͨ͠ͱ͍͏͜ͱΛ఻͑ΔͨΊʹɺdoneνϟωϧͰ఻͍͑ͯΔ
    Close() Λ࢖ͬͯΞϯϒϩοΫͤ͞Δ৔߹

    View full-size slide

  69. ϒϩοΩϯά I/O Λ೚ҙͷλΠϛϯάͰऴ͍ྃͤͨ͞৔߹
    net.Conn ͱ os.File ͷΦϒδΣΫτʹ͍ͭͯ
    • net.Conn ͷ Read ΍ Write ʹ΋ context ͷ࢖͑Δ
    ݺͼग़͠͸ͳ͍
    • Accept() ͱಉ༷ʹ net.Conn ͷ஋Λ Close() ͢Δ
    ͜ͱʹΑΓ೚ҙͷλΠϛϯάͰΞϯϒϩοΫ
    • os.File ͷ Read ΍ Write ΋ಉ༷
    • os.File Λ Close() ͢Ε͹ΞϯϒϩοΫ͞ΕΔ

    View full-size slide

  70. ͜͜·ͰͰϒϩοΩϯά I/O
    ͷ࿩ʹ͍ͭͯҰ௨Γ͠·ͨ͠ɻ

    View full-size slide

  71. ҙ֎ͱ؆୯ͩ͠ɺॏཁͳ
    TLSʹ΋ରԠͯ͠ΈΑ͏ʂ

    View full-size slide

  72. TLSରԠ͸೉͘͠ͳ͍
    • ॳظԽॲཧΛআ͚͹ɺετϦʔϜܕϓϩτί
    ϧͱಉ͡ΠϯλϑΣʔε͕࣮૷͞Ε͍ͯΔ
    • ࠷ॳ͔ΒΠϯλϑΣʔεΛϕʔεʹ։ൃͯ͠
    ͍Δͱ؆୯
    • ࣮૷ͷखؒ͸গͳ͍ͷͰɺηΩϡΞʹͨ͠
    ͍ͱ͖ݕ౼͢΂͖

    View full-size slide

  73. TLSରԠ: αʔόଆ
    • net.Listen ͷ୅ΘΓʹ tls.Listen
    Λ࢖͏
    • αʔόଆ͸ূ໌ॻͷઃఆ͕ՃΘ
    Δ͘Β͍Ͱɺ͋ͱ͸ಉ͡Πϯλ
    ϑΣʔεͰѻ͑Δ
    αʔόଆͷίʔυྫ

    View full-size slide

  74. • net.Listen ͷ୅ΘΓʹ tls.Listen
    Λ࢖͏
    • αʔόଆ͸ূ໌ॻͷઃఆ͕ՃΘ
    Δ͘Β͍Ͱɺ͋ͱ͸ಉ͡Πϯλ
    ϑΣʔεͰѻ͑Δ
    αʔόଆͷίʔυྫ
    TLSରԠ: αʔόଆ

    View full-size slide

  75. • net.Listen ͷ୅ΘΓʹ tls.Listen
    Λ࢖͏
    • αʔόଆ͸ূ໌ॻͷઃఆ͕ՃΘ
    Δ͘Β͍Ͱɺ͋ͱ͸ಉ͡Πϯλ
    ϑΣʔεͰѻ͑Δ
    αʔόଆͷίʔυྫ
    TLSରԠ: αʔόଆ

    View full-size slide

  76. TLSରԠ: ΫϥΠΞϯτଆ
    • net.Dial ͷ୅ΘΓʹ tls.Dial Λ࢖͏
    • ࣗݾೝূہΛ࢖͏৔߹ʹ͸ɺূ໌ॻΛࢦ
    ఆ͢Δඞཁ͕͋Δ
    • αʔόূ໌ॻͷݕূΛεΩοϓ͢ΔΦ
    ϓγϣϯ΋͋Δ͕ɺӡ༻ͰࣄނΔϦε
    ΫΛߴΊΔͷͰɺݸਓతʹ͸։ൃ؀ڥ
    ͩͱͯ͠΋εΩοϓ͸Φεεϝ͠ͳ͍
    ΫϥΠΞϯτଆ

    View full-size slide

  77. TLSରԠ: ΫϥΠΞϯτଆ
    • net.Dial ͷ୅ΘΓʹ tls.Dial Λ࢖͏
    • ࣗݾೝূہΛ࢖͏৔߹ʹ͸ɺূ໌ॻΛࢦ
    ఆ͢Δඞཁ͕͋Δ
    • αʔόূ໌ॻͷݕূΛεΩοϓ͢ΔΦ
    ϓγϣϯ΋͋Δ͕ɺӡ༻ͰࣄނΔϦε
    ΫΛߴΊΔͷͰɺݸਓతʹ͸։ൃ؀ڥ
    ͩͱͯ͠΋εΩοϓ͸Φεεϝ͠ͳ͍
    ΫϥΠΞϯτଆ

    View full-size slide

  78. TLSରԠ: ΫϥΠΞϯτଆ
    • net.Dial ͷ୅ΘΓʹ tls.Dial Λ࢖͏
    • ࣗݾೝূہΛ࢖͏৔߹ʹ͸ɺূ໌ॻΛࢦ
    ఆ͢Δඞཁ͕͋Δ
    • αʔόূ໌ॻͷݕূΛεΩοϓ͢ΔΦ
    ϓγϣϯ΋͋Δ͕ɺӡ༻ͰࣄނΔϦε
    ΫΛߴΊΔͷͰɺݸਓతʹ͸։ൃ؀ڥ
    ͩͱͯ͠΋εΩοϓ͸Φεεϝ͠ͳ͍
    ΫϥΠΞϯτଆ

    View full-size slide

  79. TLSͰΑΓηΩϡΞʹ
    • TLSΛ࢖͏ͱͦΕ͚ͩͰ؆୯ʹ௨৴ͷ҉߸Խ͕Ͱ͖Δ͕ɺߟྀ͓͖ͯͨ͠
    ͍໰୊΋͋Δ
    • ௨৴ઌͷαʔό͸ຊ෺ʁ ͳΓ͢·͠͞Ε͍ͯͳ͍ʁ
    • SymantecύʔτφʔاۀʹΑΔূ໌ॻͷޡൃߦ໰୊ͳͲ
    • Webϒϥ΢βͳΒ CT (Certificate Transparency) ΍ HPKP (HTTP
    Public Key Pinning) ͱ͍͏࢓૊Έ͕͋Δ͕ɺࣗલͷTLSΞϓϦέʔγϣ
    ϯͰ͸Ͳ͏͢Δʁ
    • ࣗ෼Ͱಉ͡Α͏ͳ࢓૊ΈΛೖΕΑ͏ʂ

    View full-size slide

  80. ࣗ෼Ͱ Public Key Pinning
    ݕূ͚ͩ͢ΔΫϥΠΞϯτίʔυ

    View full-size slide

  81. ࣗ෼Ͱ Public Key Pinning
    ݕূ͚ͩ͢ΔΫϥΠΞϯτίʔυ
    ΫϥΠΞϯτʹݕূ͍ͨ͠pOHFSQSJOUΛؚΊ͓ͯ͘

    View full-size slide

  82. Public Key Pinning
    ݕূ͚ͩ͢ΔΫϥΠΞϯτίʔυ
    %JBM
    ͷ࣌ʹ$POpHͰݕূؔ਺Λࢦఆ͢Δ

    View full-size slide

  83. Public Key Pinning
    ݕূ͚ͩ͢ΔΫϥΠΞϯτίʔυ
    w ઀ଓʹ࢖ΘΕ͍ͯΔূ໌ॻͷpOHFSQSJOUΛܭࢉ
    w Ұக͍ͯ͠Δ͔ݕূ

    View full-size slide

  84. TLSରԠ
    • TLS΋ετϦʔϜܕͰ͸͋ΔͷͰɺ net.Conn
    ΠϯλϑΣʔεΛ࢖͑Δ
    • Public Key Pinning ͘Β͍͸ࣗ෼Ͱ࣮૷Ͱ͖Δ
    ͘Β͍ʹඪ४ϥΠϒϥϦͷॊೈੑ͸͋Δ

    View full-size slide

  85. ΠϯλϑΣʔεΛ࢖ͬͨϓϩάϥϛϯά
    • GoͷιέοτϓϩάϥϛϯάͰ͸ɺந৅౓ͷ
    ߴ͍ΠϯλϑΣʔε͕࢖͑Δ

    View full-size slide

  86. I/O ଟॏԽͷ࢓૊Έ
    • GoϥϯλΠϜ͸ netpoll ͱ͍͏ػೳͰ ϒ
    ϩοΩϯά I/O ΛଟॏԽ͍ͯ͠Δ

    View full-size slide

  87. ϒϩοΩϯά I/O ͷΞϯϒϩοΫ
    • context.Context Λ࢖͏৔߹
    • Close() Λ͢Δ৔߹

    View full-size slide

  88. TLSରԠͯ͠ΈΑ͏
    • ূ໌ॻΛࢦఆΛ͢Δ͙Β͍Ͱɺ͋ͱ͸ετϦʔ
    ϜܕͱΠϯλϑΣʔε͕ಉ͡ͳͷͰɺ͓खܰʂ
    • TLSΛ࢖ͬͯ΋ηΩϡϦςΟ͕֬อ͞ΕΔΘ͚
    Ͱ͸ͳ͍
    • Web ͡Όͳͯ͘΋ Public Key Pinning ͸؆
    ୯ʹ࣮ࢪͰ͖Δ

    View full-size slide