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

Golangで学ぶHot deployの仕組み

Golangで学ぶHot deployの仕組み

Server::Starterの仕組みを学び、Golangでミニマムで
 実装してみた

tsurubee

June 28, 2018
Tweet

More Decks by tsurubee

Other Decks in Programming

Transcript

  1. !UTVSVCFF(.01FQBCP *OD
    ۝भΠϯϑϥަྲྀษڧձ ,JYT
    7PM
    (PMBOHͰֶͿ
    )PUEFQMPZͷ࢓૊Έ

    View full-size slide

  2. (.0ϖύϘɹΠϯϑϥΤϯδχΞ
    !UTVSVCFF

    View full-size slide

  3. ೥݄(.0ϖύϘೖࣾ
    ΠϯϑϥΤϯδχΞྺϲ݄

    View full-size slide

  4. ໨࣍
    w)PUEFQMPZͱ͸ʁͳͥඞཁ͔ʁ
    w)PUEFQMPZͷ࢓૊Έ
    w(PMBOHͰ࣮૷ͯ͠Έͨ

    View full-size slide

  5. ఻͍͑ͨ͜ͱ
    wμ΢ϯλΠϜθϩΛࢧ͑Δ)PUEFQMPZͷ࢓૊ΈͷҰྫ
    w(PMBOHΛ௨ͯ͡௿ϨΠϠʔͷٕज़ΛֶͿ͜ͱͰɺ

    ΤϯδχΞͱͯ͠ͷ෯͕޿͕ΔͷͰ͸ʁ
    wٕज़ॻಡΜͰ;Ή;Ή͢ΔΑΓ࣮૷ͯ͠Έͨํ͕ཧղ

    ͠΍͍͢

    View full-size slide

  6. )PUEFQMPZͱ͸ʁ
    ͳͥඞཁ͔ʁ

    View full-size slide

  7. )PUEFQMPZͱ͸ʁ
    wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ
    ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓
    ө͢ΔͨΊͷखஈ

    View full-size slide

  8. )PUEFQMPZͱ͸ʁ
    wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ
    ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓
    ө͢ΔͨΊͷखஈ
    ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ
    4PVSDFDPEF
    DIBOHF
    (PMBOHͷ৔߹ɺ
    όΠφϦͷ্ॻ͖

    View full-size slide

  9. )PUEFQMPZͱ͸ʁ
    wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ
    ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓
    ө͢ΔͨΊͷखஈ
    ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ
    4PVSDFDPEF
    DIBOHF
    "QQMJDBUJPO
    4UPQ
    (PMBOHͷ৔߹ɺ
    όΠφϦͷ্ॻ͖

    View full-size slide

  10. )PUEFQMPZͱ͸ʁ
    wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ
    ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓
    ө͢ΔͨΊͷखஈ
    ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ
    4PVSDFDPEF
    DIBOHF
    "QQMJDBUJPO
    4UPQ
    "QQMJDBUJPO
    4UBSU
    (PMBOHͷ৔߹ɺ
    όΠφϦͷ্ॻ͖
    ࠶ىಈ

    View full-size slide

  11. )PUEFQMPZͱ͸ʁ
    wΞϓϦέʔγϣϯʹϩδοΫ΍ઃఆ౳ͷมߋΛՃ͑ͨࡍ
    ʹɺϦΫΤετͷॲཧΛଓ͚ଓ͚ͳ͕Βɺมߋͷ಺༰Λ൓
    ө͢ΔͨΊͷखஈ
    ҰൠతͳσϓϩΠʢ/05)PUEFQMPZʣ
    4PVSDFDPEF
    DIBOHF
    "QQMJDBUJPO
    4UPQ
    "QQMJDBUJPO
    4UBSU
    (PMBOHͷ৔߹ɺ
    όΠφϦͷ্ॻ͖
    ϦΫΤετΛॲཧ
    Ͱ͖ͳ͍͕࣌ؒൃੜ
    ࠶ىಈ

    View full-size slide

  12. )PUEFQMPZ͕ͳͥඞཁ͔ʁ
    wΘ͔ͣͳؒͰ΋Քಇ͍ͯ͠ΔαʔϏεΛࢭΊͨ͘ͳ͍
    w)PUEFQMPZΛಋೖ͢Δ͜ͱͰɺγεςϜӡ༻ʹ͓͚Δ

    อकੑ͕޲্͠ɺਝ଎ͳ։ൃαΠΫϧ͕࣮ݱͰ͖Δ

    View full-size slide

  13. )PUEFQMPZͷ࢓૊Έ

    View full-size slide

  14. 4FSWFS4UBSUFS
    w؆୯ʹ)PUEFQMPZͰ͖ΔΑ͏ʹ͢ΔͨΊͷDQBONPEVMF
    w1FSMͰ࣮૷͞Ε͍ͯΔ͕ɺγάφϧͱ؀ڥม਺Λ࢖ͬͨ

    ൚༻ੑͷߴ͍࢓૊ΈͷͨΊɺ͞·͟·ͳݴޠͰར༻Մೳ

    View full-size slide

  15. 4FSWFS4UBSUFS
    w؆୯ʹ)PUEFQMPZͰ͖ΔΑ͏ʹ͢ΔͨΊͷDQBONPEVMF
    w1FSMͰ࣮૷͞Ε͍ͯΔ͕ɺγάφϧͱ؀ڥม਺Λ࢖ͬͨ

    ൚༻ੑͷߴ͍࢓૊ΈͷͨΊɺ͞·͟·ͳݴޠͰར༻Մೳ
    ಈ࡞Λݴ༿Ͱઆ໌͢Δͱɾɾ
    ʮ৽͍͠ϓϩηεΛىಈͯ͠৽͍͠ϦΫΤετΛͦͪΒʹ
    ྲྀͭͭ͠ɺݹ͍ϓϩηεͷϦΫΤετ͕׬ྃͨ͠Βਖ਼͘͠
    ऴྃͤ͞Δʯ

    View full-size slide

  16. 4FSWFS4UBSUFS
    w؆୯ʹ)PUEFQMPZͰ͖ΔΑ͏ʹ͢ΔͨΊͷDQBONPEVMF
    w1FSMͰ࣮૷͞Ε͍ͯΔ͕ɺγάφϧͱ؀ڥม਺Λ࢖ͬͨ

    ൚༻ੑͷߴ͍࢓૊ΈͷͨΊɺ͞·͟·ͳݴޠͰར༻Մೳ
    ಈ࡞Λݴ༿Ͱઆ໌͢Δͱɾɾ
    ʮ৽͍͠ϓϩηεΛىಈͯ͠৽͍͠ϦΫΤετΛͦͪΒʹ
    ྲྀͭͭ͠ɺݹ͍ϓϩηεͷϦΫΤετ͕׬ྃͨ͠Βਖ਼͘͠
    ऴྃͤ͞Δʯ
    (SBDFGVM3FTUBSUʢ༏խͳ࠶ىಈʣ

    View full-size slide

  17. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ
    TUBSU@TFSWFS
    1BSFOU
    start_server —-port 8888 —- ./server
    TFSWFS
    $IJME
    TPDLFU
    GPSLFYFD
    TPDLFUΛڞ༗
    BDDFQU
    TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ
    TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ

    View full-size slide

  18. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ
    TUBSU@TFSWFS
    1BSFOU
    start_server —-port 8888 —- ./server
    TFSWFS
    $IJME
    TPDLFU
    GPSLFYFD
    TPDLFUΛڞ༗
    BDDFQU
    TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ
    TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ
    ᶃϓϩάϥϜʹมߋʂ

    View full-size slide

  19. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ
    kill -HUP
    TPDLFU
    BDDFQU
    TUBSU@TFSWFS
    1BSFOU
    TFSWFS
    $IJME
    TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ
    TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ
    ᶄ4*()61
    ᶃϓϩάϥϜʹมߋʂ

    View full-size slide

  20. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ
    kill -HUP
    TPDLFU
    GPSLFYFD
    TFSWFS
    $IJME
    ᶄ4*()61
    BDDFQU
    BDDFQU
    TUBSU@TFSWFS
    1BSFOU
    TFSWFS
    $IJME
    ᶅ৽ϓϩάϥϜͰىಈ
    ᶃϓϩάϥϜʹมߋʂ

    View full-size slide

  21. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ
    kill -HUP
    TPDLFU
    ऴྃ͢Δલʹʜ
    ᶃ৽نϦΫΤεड෇
    ɹͷఀࢭ
    ᶄݱࡏॲཧதͷ
    ϦΫΤετͷ׬ྃ
    GPSLFYFD
    (SBDFGVM4IVUEPXO
    BDDFQU
    BDDFQU
    TUBSU@TFSWFS
    1BSFOU
    TFSWFS
    $IJME
    ᶅ৽ϓϩάϥϜͰىಈ
    ᶄ4*()61
    ᶆ4*(5&3.
    TFSWFS
    $IJME
    ᶃϓϩάϥϜʹมߋʂ

    View full-size slide

  22. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ
    TUBSU@TFSWFS
    1BSFOU
    kill -HUP
    TPDLFU
    GPSLFYFD
    TFSWFS
    $IJME
    BDDFQU
    ᶆ4*(5&3.
    ᶅ৽ϓϩάϥϜͰىಈ
    ᶄ4*()61
    TFSWFS
    $IJME
    ᶃϓϩάϥϜʹมߋʂ
    ऴྃ͢Δલʹʜ
    ᶃ৽نϦΫΤεड෇
    ɹͷఀࢭ
    ᶄݱࡏॲཧதͷ
    ϦΫΤετͷ׬ྃ
    (SBDFGVM4IVUEPXO

    View full-size slide

  23. ࣮૷ͯ͠Έͨ

    View full-size slide

  24. ϛχϚϜͰ࣮૷ͯ͠Έͨ
    IUUQTHJUIVCDPNUTVSVCFFHSBDFGVMSFTUBSUHP

    View full-size slide

  25. ࣮૷ϙΠϯτ̍ʙγάφϧϋϯυϥ
    func waitSignal(l net.Listener) {
    c := make(chan os.Signal, 1)
    signal.Notify(c, syscall.SIGTERM)
    go func() {
    sig := <-c
    switch sig {
    case syscall.SIGTERM:
    signal.Stop(c)
    l.Close()
    }
    }()
    }
    γάφϧͱ͸ϓϩηεؒ௨৴ͷҰछ
    ϓϩηεʹγάφϧΛૹ৴͢Δͱɺͦͷϓϩηεͷਖ਼ৗॲཧʹׂΓࠐΜͰɺ
    γάφϧݻ༗ͷॲཧΛ࣮ߦͰ͖Δ
    ͓खܰʹγάφϧͷϋϯυϦϯά͕
    Ͱ͖Δ

    View full-size slide

  26. ࣮૷ϙΠϯτ̍ʙγάφϧϋϯυϥ
    func waitSignal(l net.Listener) {
    c := make(chan os.Signal, 1)
    signal.Notify(c, syscall.SIGTERM)
    go func() {
    sig := <-c
    switch sig {
    case syscall.SIGTERM:
    signal.Stop(c)
    l.Close()
    }
    }()
    }
    γάφϧͱ͸ϓϩηεؒ௨৴ͷҰछ
    ϓϩηεʹγάφϧΛૹ৴͢Δͱɺͦͷϓϩηεͷਖ਼ৗॲཧʹׂΓࠐΜͰɺ
    γάφϧݻ༗ͷॲཧΛ࣮ߦͰ͖Δ
    HPSPVUJOFͰγάφϧΛ଴ͪड͚Δ
    ͓खܰʹγάφϧͷϋϯυϦϯά͕
    Ͱ͖Δ

    View full-size slide

  27. ࣮૷ϙΠϯτ̎ʙϑΝΠϧσΟεΫϦϓλ
    func forkExec(l *net.TCPListener) (*os.Process, error) {
    progName, _ := exec.LookPath(os.Args[0])
    pwd, _ := os.Getwd()
    f, _ := l.File()
    defer f.Close()
    files := []*os.File{os.Stdin, os.Stdout, os.Stderr, f}
    fdEnv := fmt.Sprintf("%s=%d", "FD_KEY", len(files)-1)
    return os.StartProcess(progName, os.Args, &os.ProcAttr{
    Dir: pwd,
    Env: append(os.Environ(), fdEnv),
    Files: files,
    })
    }
    ϑΝΠϧσΟεΫϓϦΫλ͸ɺϑΝΠϧ΍ιέοτͳͲΛந৅Խͨ͠࢓૊Έ
    ϓϩηεΛGPSLͨ͠ࡍ͸ɺ਌ϓϩηεͷϑΝΠϧσΟεΫϦϓλΛҾ͖ܧ͙

    View full-size slide

  28. ·ͱΊ
    w4FSWFS4UBSUFSͷ࢓૊ΈΛֶͼɺ(PMBOHͰϛχϚϜͰ

    ࣮૷ͯ͠Έͨ
    wγάφϧ΍ϑΝΠϧσΟεΫϦϓλͱ͍ͬͨ֓೦͸ٕज़ॻΛಡ
    ΜͰ;Ή;Ή͢ΔΑΓɺ࣮૷ͯ͠Έͨ΄͏͕ཧղ͠΍͔ͬͨ͢
    w(PMBOH͸ଟ͘ͷ04ͷػೳΛ؆ܿͳίʔυͰѻ͑ΔͨΊɺ

    ࣗ෼ͷؔ৺ྖҬΛΑΓ௿͍ϨΠϠʔʹ޿͍͛ͯͨ͘Ίʹ

    దͨ͠ݴޠͩ

    View full-size slide

  29. ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂʂ

    View full-size slide