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

HTTP2とASGI Webサーバの関係 - Hypercornを例に

Junya Fukuda
October 10, 2020

HTTP2とASGI Webサーバの関係 - Hypercornを例に

PyCon mini Hiroshima 2020 登壇資料

Junya Fukuda

October 10, 2020
Tweet

More Decks by Junya Fukuda

Other Decks in Programming

Transcript

  1. "4(*썺썽쎁쎂 •Asynchronous Server Gateway Interface ͷུ •Webαʔό͕ΞϓϦέʔγϣϯͷ async/await ʹରԠ •WSGIͷޙܧ

    •WebαʔόͱΞϓϦέʔγϣϯͷΠϯλʔϑΣʔεͷ࢓༷ https://www.youtube.com/watch?v=EwG5EWKu8nw ASGIʢඇಉظαʔόήʔτ΢ΣΠΠϯλʔϑΣʔεʣͷ֓ཁ (Junya Fukuda) [PyCon JP 2020]
  2. "4(*썺썽쎁쎂 •Asynchronous Server Gateway Interface ͷུ •Webαʔό͕ΞϓϦέʔγϣϯͷ async/await ʹରԠ •WSGIͷޙܧ

    •WebαʔόͱΞϓϦέʔγϣϯͷΠϯλʔϑΣʔεͷ࢓༷ https://www.youtube.com/watch?v=EwG5EWKu8nw ASGIʢඇಉظαʔόήʔτ΢ΣΠΠϯλʔϑΣʔεʣͷ֓ཁ (Junya Fukuda) [PyCon JP 2020] •ෳ਺ͷҰൠతͳϓϩτίϧΛॲཧՄೳʮHTTP/1.1ʯʮHTTP/2ʯʮWebsocketʯ
  3. )551썺썽쎁쎂 •ετϦʔϜ ‣ ΫϥΠΞϯτ ‣ αʔόʔ ‣ ΫϥΠΞϯτ ‣ αʔόʔ

    ‣ 1ͭͷTCPͷίωΫγϣϯͷதͰॱ൪଴ͪΛ͢Δ͜ͱͳ͘ɺฒྻʹॲཧ HTTP/1.1 HTTP/2 ฒྻʹॲཧ
  4. )551썺썽쎁쎂 •ετϦʔϜ ‣ ΫϥΠΞϯτ ‣ αʔόʔ ‣ ΫϥΠΞϯτ ‣ αʔόʔ

    HTTP/1.1 HTTP/2 ฒྻʹॲཧ ‣ 1ͭͷTCPͷίωΫγϣϯͷதͰॱ൪଴ͪΛ͢Δ͜ͱͳ͘ɺฒྻʹॲཧ
  5. )551쎂ରԠ썮썶"4(*8FC4FSWFS •ASGI WebServer ͱ͍͑͹ Uvicorn •·ͩHTTP/2ະରԠ ʢରԠ༧ఆ͋Γʣ Uvicorn currently supports

    HTTP/1.1 and WebSockets. Support for HTTP/2 is planned. Uvicorn͸ݱࡏɺHTTP/1.1ͱWebSocketsΛαϙʔτ͍ͯ͠·͢ɻ HTTP/2ͷαϙʔτ͕༧ఆ͞Ε͍ͯ·͢ɻ
  6. )551쎂ରԠ썮썶"4(*8FC4FSWFS •ASGI࢓༷ͷରԠঢ়گ Framework HTTP WebSockets Server Push WebSocket HTTP Hypercorn

    1 ◦ 2 ◦ 1 ◦ 2 ◦ ◦ ◦ Uvicorn 1 ◦ 2 × 1 ◦ 2 × × × Daphne 1 ◦ 2 ◦ 1 ◦ 2 × × × Mangum 1 ◦ 2 × 1 ◦ 2 × × ×
  7. )551쎂ରԠ썮썶"4(*8FC4FSWFS •ASGI࢓༷ͷରԠঢ়گ Framework HTTP WebSockets Server Push WebSocket HTTP Hypercorn

    1 ◦ 2 ◦ 1 ◦ 2 ◦ ◦ ◦ Uvicorn 1 ◦ 2 × 1 ◦ 2 × × × Daphne 1 ◦ 2 ◦ 1 ◦ 2 × × × Mangum 1 ◦ 2 × 1 ◦ 2 × × ×
  8. )551쎂ରԠ썮썶"4(*8FC4FSWFS •ϋΠύʔίʔϯͱಡΈ·͢ •WSGI Web αʔόͷGunicornʹΠϯεύΠΞ͞Ε͍ͯΔ •HTTP / 1ɺHTTP / 2ɺWebSocketʢHTTP

    / 1͓ΑͼHTTP / 2ܦ༝ʣαϙʔτ •worker ͷλΠϓʹ asyncio,uvloop,trio ΛࢦఆՄೳ •worker ਺ΛࢦఆՄೳ
  9. ؀ڥ •Hypercorn •fastapi 0.61.1 0.11.0 Webαʔό Web Framework ⁶ ⁶

    ʢASGIʣ ʢHTTP/2ʣ ʢHTTP/1.1ʣ )ZQFSDPSO썾쏃쏒쏴썗쏪
  10. ؀ڥ •Hypercorn •fastapi 0.61.1 0.11.0 Webαʔό Web Framework ⁶ ⁶

    ʢASGIʣ ʢHTTP/2ʣ ʢHTTP/1.1ʣ )ZQFSDPSO썾쏃쏒쏴썗쏪
  11. 쎹쏳쎮쎬쏽쏒쎅ॲཧ requestButton.onclick = async function() { result = []; for

    (let i = 0; i < 24; i++) { result.push(fetch(`/message/${i}/`)); } await Promise.all(result); }
  12. ϋʔυ΢ΣΞ؀ڥ •Macbook Pro •2.6 GHz 6ίΞ Intel Core i7 •ϝϞϦ

    16GB )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ
  13. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ ҎԼ3ͭ৚݅ •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100 •ΫϥΠΞϯτಉ࣌઀ଓ 50, ϦΫΤετ਺ 5000

    •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000 ࢦඪ •ॲཧͷऴྃ·Ͱʹ͔͔ͬͨ࣌ؒ •1ඵ͋ͨΓͷॲཧͨ͠ϦΫΤετ਺
  14. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ ҎԼ3ͭ৚݅ •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100 •ΫϥΠΞϯτಉ࣌઀ଓ 50, ϦΫΤετ਺ 5000

    •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000 ࢦඪ •ॲཧͷऴྃ·Ͱʹ͔͔ͬͨ࣌ؒ •1ඵ͋ͨΓͷॲཧͨ͠ϦΫΤετ਺ → ૣ͍ͱྑ͍ → ଟ͍ͱྑ͍
  15. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ ऴྃ࣌ؒ [s]      •ΫϥΠΞϯτಉ࣌઀ଓ 10,

    ϦΫΤετ਺ 100      ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  16. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ ऴྃ࣌ؒ [s]      •ΫϥΠΞϯτಉ࣌઀ଓ 10,

    ϦΫΤετ਺ 100      ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  17. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ      •ΫϥΠΞϯτಉ࣌઀ଓ 50, ϦΫΤετ਺ 5000

         ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  18. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ      •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000

         ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  19. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ •h2load — help Max concurrent streams to issue per

    session. When http/1.1 is used, this specifies the number of HTTP pipelining requests in-flight. ηογϣϯ͝ͱʹൃߦ͢Δ࠷େಉ࣌ετϦʔϜ਺ɻ http/1.1 Λ࢖༻͢Δ৔߹ɺ HTTP ύΠϓϥΠϯϦΫΤετ ͷ਺Λࢦఆ͠·͢ɻ -m, --max-concurrent-streams=<N>
  20. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ ऴྃ࣌ؒ [s]      •ΫϥΠΞϯτಉ࣌઀ଓ 10,

    ϦΫΤετ਺ 100, ετϦʔϜ 5      ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  21. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ      •ΫϥΠΞϯτಉ࣌઀ଓ 50, ϦΫΤετ਺ 5000,

    ετϦʔϜ 5      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  22. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ      •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000,

    ετϦʔϜ 5      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  23. )551)551쏛쏟쎳썗쏨쏽쏃ൺֱ ετϦʔϜͳ͠ ετϦʔϜ͋Γ      •ΫϥΠΞϯτಉ࣌઀ଓ 100,

    ϦΫΤετ਺ 10000, ετϦʔϜ 5 ετϦʔϜͳ͠ ετϦʔϜ͋Γ      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  24. 쎕썿쎘 •HTTP/2Կ͕͓͍͍͠ͷʁ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython Web Application

    Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ → ଎౓͕վળͷՄೳੑ
  25. 쎕썿쎘 •HTTP/2Կ͕͓͍͍͠ͷʁ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython Web Application

    Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ
  26. 쎕썿쎘 •HTTP/2Կ͕͓͍͍͠ͷʁ •HTTP/2ͷWebserver Hypercorn ʹ͢Δ͜ͱͰ଎౓վળ͕ΈΒΕΔ •Typeʹ fetch ΍ xhr ͕ଟ͍

    •ࣗ͝਎ͷPython Web Application Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ → ଎౓͕վળͷՄೳੑ
  27. 쎕썿쎘 •HTTP/2Կ͕͓͍͍͠ͷʁ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython Web Application

    Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ ͔΋͠Ε·ͤΜ •HTTP/2ͷWebserver Hypercorn ʹ͢Δ͜ͱͰ଎౓վળ͕ΈΒΕΔ → ଎౓͕վળͷՄೳੑ
  28. 썡쎕써 •AWSͰσϓϩΠ͍ͯ͠Δ৔߹ •ALB ʹ ಺ଆ͸HTTP/2 ະରԠͷͨΊɺHTTP/̍.1 •NLB ʹ 2020೥6݄ʹ಺ଆHTTP/2ରԠ͍ͯ͠Δ ϒϥ΢β

    Ϧόʔε ϓϩΩγ Hyper corn Web FW )551 )551 "4(* IUUQTBXTBNB[PODPNKQBCPVUBXTXIBUTOFXOFUXPSLMPBECBMBODFSOPXTVQQPSUTUMTBMQOQPMJDJFT
  29. 썡쎕써̎ •WSGIΞϓϦέʔγϣϯͷ৔߹ •HypercornʢASGI Webαʔό͸WSGIରԠ͍ͯ͠·͢ʣͰσϓϩΠՄೳ •ࢼͯ͠ΈΔՁ஋͕͋Δ͔΋ʂʁ •Django 2ܥ - wsgi.pyΛϥοϓ͢Δ͚ͩ from

    hypercorn.middleware import AsyncioWSGIMiddleware import sampleproject.wsgi as wsgi_app asyncio_app = AsyncioWSGIMiddleware(wsgi_app.application) https://github.com/jrfk/django2-hypercorn
  30. ࢀߟࢿྉ •How to serve HTTP/2 using Python IUUQTNFEJVNDPNQZUIPOQBOEFNPOJVNIPXUPTFSWFIUUQVTJOHQZUIPOFCCEF⒎ •HTTP/1 should

    die IUUQTNFEJVNDPN!QHKPOFTIUUQTIPVMEEJFCEF •Hypercorn IUUQTQHKPOFTHJUMBCJPIZQFSDPSOJOEFYIUNM •jrfk / fastapi-hypercorn_Performance-measurement IUUQTHJUIVCDPNKSGLGBTUBQJIZQFSDPSO@1FSGPSNBODFNFBTVSFNFOU