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

開発期間数年のサービスの完成間際にcontext対応をぶち込む / introduce context.Context into long term project

開発期間数年のサービスの完成間際にcontext対応をぶち込む / introduce context.Context into long term project

kamakura.go #5 @ 鎌倉

acidlemon

June 22, 2019
Tweet

More Decks by acidlemon

Other Decks in Technology

Transcript

  1. ࣮࿥ʂ։ൃظؒ਺೥ͷ
    αʔϏεͷ׬੒ؒࡍʹ
    DPOUFYUରԠΛͿͪࠐΉ
    LBNBLVSBHP

    !BDJEMFNPOɹ@

    View full-size slide

  2. ͜Μʹͪ͸ʙ
    w ໘ന๏ਓΧϠοΫιʔγϟϧήʔϜࣄۀ෦

    ήʔϜٕݚϦʔμʔ

    ֤ϓϩδΣΫτΛԣஅͰαϙʔτ͢ΔνʔϜ

    w ීஈ͸ओʹ(Pॻ͍ͨΓ1FSMॻ͍ͨΓ

    ௚ۙ͸$Λॻ͍͍ͯΔ 6OJUZ

    &NBDT೿͚ͩͬͨͲ࠷ۙ74$PEF࢖ͬͯΔ
    w ח૔ࡏॅ Ո͔Β͜ͷձ৔·Ͱెา෼

    !BDJEMFNPO

    View full-size slide

  3. ࣄͷ࢝·Γ
    w ͋Β͢͡
    w ɹɹɹΑ͍ͭ͠΋ͷΑ͏ʹෛՙࢼݧ͢Δͧ
    w ɹɹɹ"8493BZͰ෼ࢄτϨʔγϯάͩ
    ਺೥͔͚ͯ։ൃͨ͠(PͷαʔόϓϩάϥϜ͕ͦΖͦΖ

    αʔϏεΠϯ͢Δ͜ͱʹΊͰ͍ͨͰ͢Ͷʙ

    View full-size slide

  4. 93BZͰ෼ࢄτϨʔγϯά
    w ૊௕ !GVKJXBSB
    ͷ:"1$5PLZPͷൃදͳͲͰ͓ͳ͡Έ
    IUUQTTQFBLFSEFDLDPNGVKJXBSBZBQDUPLZP

    w ΧϠοΫͰ͸ࡢ೥͔Βෛՙࢼݧ΍ຊ൪؀ڥͷௐࠪʹ"8493BZΛ
    ར༻͍ͯ͠Δ
    w ૊௕͕࠷ॳʹೖΕͨࣄྫ͸1FSM͕ͩͬͨɺࠓճ͸(PϓϩμΫτ

    (PͳΒ"84͕4%,Λग़͍ͯ͠ΔͷͰࣗલͰϥΠϒϥϦ࡞ΔΈ͍ͨ
    ͳ͜ͱ͠ͳͯ͘΋େৎ෉͜Ε͸উͬͨͳʜʜ ϑϥάͷཱͭԻ

    View full-size slide

  5. 93BZ͸ԿΛ͢Δ͔
    w 8FCαʔϏεʹ͓͚Δ͔͔࣌ؒΔཁૉͷՄࢹԽ
    w جຊతʹ֎෦*0 σʔλϕʔεɺ,74ɺ)551ͳͲʜ
    ͕͔࣌ؒ
    ͔Δॲཧ
    w ηάϝϯτΛ࡞ͬͯ։࢝࣌ؒͱ

    ऴྃ࣌ؒɺΤϥʔͳͲΛه࿥ͯ͠ૹΔ
    w "84͞ΜʹૹΓ͚ͭΔͱ͍͍ײ͡ʹ

    ՄࢹԽͯ͘͠ΕΔˠ͜Μͳײ͡ˠ

    View full-size slide

  6. 93BZ4%,GPS(Pͷ৔߹
    DPOUFYUͷҾ͖ճ͠Ͱ࣮ݱ

    View full-size slide

  7. ͜Μͳײ͡
    w YSBZ$BQUVSFʹDUYͱ࣮ߦ͍ͨ͠ॲཧΛแΜͩؔ਺Λ౉͢ͱɺ
    $BQUVSF͕։࢝࣌ࠁͱऴྃ࣌ࠁΛDUYʹه࿥ͯ͘͠ΕΔ

    93BZ)551ΫϥΠΞϯτͳͲ͸಺෦Ͱ͜ΕΛ΍͍ͬͯΔ

    func criticalSection(ctx context.Context) {
    //this is an example of a subsegment
    xray.Capture(ctx, "GameModel.saveGame", func(ctx1 context.Context) error {
    var err error
    section.Lock()
    result := someLockedResource.Go()
    section.Unlock()
    xray.AddMetadata(ctx1, "ResourceResult", result)
    })
    }
    IUUQTEPDTBXTBNB[PODPNKB@KQYSBZMBUFTUEFWHVJEFYSBZTELHPTVCTFHNFOUTIUNM

    View full-size slide

  8. ͳΔ΄Ͳʜ
    w 93BZ4%,GPS(PΛ࢖͏ʹ͸DPOUFYU$POUFYU͕ඞཁ
    w ͜ͷϓϩδΣΫτ͸DPOUFYUᴈ໌ظʹ։ൃ͕ελʔτͨ͠ͷͰɺ%#
    ΞΫηε΍3FEJTૢ࡞ͷ෦෼ʹDPOUFYUΛར༻͍ͯ͠ͳ͍
    w ϦϑΝΫλϦϯάͯ͠DPOUFYUΛҾ͖ճ͍ͯ͘͠ඞཁ͕͋Δͳʜ

    View full-size slide

  9. DPOUFYUҾ͖ճ͠ϦϑΝΫλ
    w 3FEJT
    w SFEJHPΛར༻͍ͯͯ͠ɺࣗલͷ3FEJT$MJFOUͰXSBQ͍ͯͨ͠
    ͷͰͦͷҾ਺ʹDPOUFYU$POUFYUΛ௥Ճ

    SFEJHP͸3FEJT$MVTUFSඇରԠͳͷͰޙʹHPSFEJTʹೖΕସ͑ͨ

    w 3FEJTͷ13—
    w 'MVFOU-PHHFS
    w ͜Ε΋ࣗલͰXSBQͯͨ͠ͷͰҾ਺ʹDPOUFYU$POUFYUΛ௥Ճ
    w 'MVFOU-PHHFSͷ13—

    View full-size slide

  10. ͦͷ΄͔
    w )551ϦΫΤετ
    w ֎෦಺෦ؔ܎ͳ͘93BZ4%,ͷ)551$MJFOUʹࠩ͠ସ͑
    w "84ͷ֤छαʔϏε
    w 4244$MPVE4FBSDIͳͲͳͲ͸93BZ4%,ͰXSBQ
    ͯ͠ద༻Ͱ͖ΔΑ͏ʹͳ͍ͬͯΔ
    w ͜ͷลΛϞϩϞϩద༻ͯ͠—

    View full-size slide

  11. ͜͜·Ͱ͸!LPOCPJͷ࢓ࣄ
    ϝϧϖΠʹస৬͍ͯͬͯ͠͠·ͬͨʜ

    View full-size slide

  12. Ұ൪෺ྔ͋Γͦ͏ͳ
    %#ΞΫηεͷͱ͜Ζ͕
    ·ͩ࢒͍ͬͯΔ

    View full-size slide

  13. ਓൈ͚ͯϓϩδΣΫτଆ͸େ๩͠
    Θͨ͠ͷग़൪ͩ@

    View full-size slide

  14. %#ΞΫηεͷDPOUFYUରԠ
    w HPSN HJUIVCDPNKJO[IVHPSN
    Λར༻ͯͨ͠
    w ͦ΋ͦ΋HPSN͕DPOUFYUରԠ͍ͯ͠ͳ͍
    w W@EFWϒϥϯνͰDPOUFYUରԠ։ൃͯ͠Δ͚Ͳఀ଺த
    w ʜͱ͸͍͑93BZ࢖͏ͷʹ%#ΞΫηεݟΒΕͳ͍ͷ͸ࠔΔ
    w ͍Ζ͍Ζௐࠪͯ͠Έͨ

    View full-size slide

  15. 93BZ4%,͕༻ҙͯ͠Δ΋ͷ
    w 42-ͷτϨʔεʹ͍ͭͯͷυΩϡϝϯτ͕৴͡ΒΕͳ͍͘Β͍ബ͍
    w ΄Μͱʹ͜Ε͔͠ॻ͍ͯͳ͍

    View full-size slide

  16. 93BZ4%,͕༻ҙͯ͠Δ΋ͷ
    w ॻ͍ͯ͋Δ͜ͱʮ͓·͑ΒͨͿΜsql.OpenͰ%#ϓʔϧ࡞ͬͯΔ
    Αͳ ͦΕΛxray.SQLʹ͢Ε͹͍͍͚ͩͩͥʯ
    w ͳΔ΄Ͳ΍ͬͯΈͨˠίϯύΠϧΤϥʔ
    w γάωνϟΛ֬ೝˠ໭Γ஋TRM%#͡Όͳͯ͘YSBZ%#ͩʜ
    package xray
    func SQL(driver, dsn string) (*DB, error)

    View full-size slide

  17. YSBZ%#ͩ
    w HPSN͸42-$PNNPOͱ͍͏TRM%#ͷίϯςΩετͳ͠ϝιο
    υΛΠϯλϑΣʔεԽ͍ͯ͠Δ
    w YSBZ%#͸શ෦DUY෇͖ͰTRM%#ͷDUYϝιουΛ࣮૷͍ͯ͠Δ
    w ͭ·ΓΠϯλϑΣʔε͕ద߹͠ͳ͍ʜ
    type SQLCommon interface {
    Exec(query string, args ...interface{}) (sql.Result, error)
    Prepare(query string) (*sql.Stmt, error)
    Query(query string, args ...interface{}) (*sql.Rows, error)
    QueryRow(query string, args ...interface{}) *sql.Row
    }
    package xray
    func (db *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    func (db *DB) Prepare(ctx context.Context, query string) (*Stmt, error)
    func (db *DB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    func (db *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
    HPSN
    YSBZ

    View full-size slide

  18. ཧ૝ͱݱ࣮
    w HPSNʹ౉͢TRM%#తͳԿ͔ʹࡉ޻Λͯ͠ɺHPSNΛXSBQͨ͠ϓ
    ϩδΣΫτͷ%#ΞΫηεΦϒδΣΫτ͔ΒͦͷDUYΛ஫ೖ͍ͨ͠
    w ͱʹ͔͍͘Ζ͍Ζ΍ͬͯΈ·͕ͨ͠ɺͰ͖·ͤΜͰͨ͠

    HPSN͕DPOUFYUରԠͯ͠ͳ͍ͷͰҾ͖ճ͕͠HPSNͰ෼அ͞Εͯ
    ͠·ͬͯ౉ͤͳ͍ʜΧελϜύϥϝʔλΛ౉͢ํ๏΋༻ҙ͞Εͯͳ͍

    &YFD2VFSZͷBSHTͷ࠷ॳʹDUY౉ͯ͠ϥούʔ͕෼཭͢Δࠇຐ๏Έ͍ͨͳ΍ͭ΋ࢼ͚ͨ͠Ͳͪΐͬͱແཧ͕͋ͬͨ

    ͋ͱɺTUSVDUͷϝϯόʹDUY࣋ͨͤΔํࣜ͸TRM%#΍HPSN͕දݱ͢Δ΋ͷ͸ίωΫγϣϯϓʔϧͰ͋ͬͯίωΫγϣϯ
    Ͱ͸ͳ͍ʜͱ͍͏͜ͱ͔ΒHPSPVUJOFTBGFʹͳΒͳ͍ͷͰ΍ͬͯ͸͍͚ͳ͍Ϡπ

    View full-size slide

  19. ଥڠͨ͠ํ਑
    w %#ΞΫηε෦෼͕DPOUFYUରԠͯ͠ྫ͑͹EFBEMJOFͱ͔DBODFM
    ͰΫΤϦதஅͰ͖ΔΑ͏ʹ͢Δͷ͸ʜఘΊΔ
    w HPSNΛXSBQͨ͠΋ͷΛ࡞ͬͯDUY͕౉ΔΑ͏ʹͯ͠ɺ93BZͷ
    $BQUVSF͚ͩ͸ͪΌΜͱ͢Δ

    View full-size slide

  20. (PSNͷϥούΛ࡞ͬͯ࢓ࠐΉ
    w ಋೖखॱͱͯ͠͸͜Μͳ͔Μ͡
    HPSN%#ͷϝιουͷ͏ͪ࢖ͬͯΔ΍ͭΛશ෦ಉ͡γάωνϟ
    Ͱఆٛͯ͠ɺ·ͣ͸ίϯύΠϧΛ௨͢ த਎͸HPSNΛݺͿ͚ͩ

    ࣍ʹϥούͷୈҰҾ਺ʹશ෦ctx context.ContextΛ௥Ճ͢Δ
    (PSNΛݺͼग़ͯ͠Δ෦෼Ͱຊ౰ʹେྔͷίϯύΠϧΤϥʔ͕Ͱ
    ΔͷͰɺͦΕΛશ෦௚͢
    93BZͷ$BQUVSFΛϥούʔʹ࢓ࠐΉ
    w ͱʹ͔͕͘େมͳ΍ͭͰ͢

    View full-size slide

  21. ࣮ࡍͷ࡞ۀ

    View full-size slide

  22. ࣗಈੜ੒ܥͷରԠ
    w ϦϦʔε௚લͷ͍·DUYೖΕͳ͔ͬͨΒ͍ΘΏΔٕज़తෛ࠴͕υΧʔϯͱͳ
    ΔΑͶʜΈ͍ͨͳ͜ͱΛ13ίϝϯτʹॻ͍͍ͯͨ
    w ࣗಈੜ੒ܥ͸େͨۤ͠࿑ͯ͠ͳ͍͚ͲɺEJGG͸ —

    View full-size slide

  23. ࢒Γͷ࡞ۀ
    w ͱʹ͔͘%#ݺͼग़͕͠େྔʹ͋ΔͷͰςΩετஔ׵ͳͲ΋ۦ࢖ͭͭ͠
    ͣͬͱϦϑΝΫλϦϯάͯ͠ςετΛճ͍ͯͨ͠
    w ౰࣌ͷϝϞʹΑΔͱɺ$SFBUF%FMFUF&YFD'JOE'JSTU
    3BX4BWFͷछͷϝιου͕߹ܭՕॴͰݺ͹Ε͍ͯΔ
    w Օॴશ෦खͰ௚ͯ͠ɺ͞Βʹݺͼग़͠ݩ͕DUY࣋ͬͯͳ͔ͬͨ
    Βͦ͜·ͰDUYΛҾ͖ճͨ͢Ίʹݺͼग़͠ݩΛ૎ͬͯDUY͕ग़ͯ͘Δ·
    ͰୈҰҾ਺ʹDUY௥Ճ͢Δ࡞ۀ
    w ͞Βʹɺςετ΋શ෦context.Background()ͯ͠౉͢࡞ۀ͕ඞཁ

    View full-size slide

  24. ్தܦաͷ༷ࢠ
    w ҙ֎ͱͭΒ͘ͳͦ͞͏ʹݟ͑Δͷ͸ɺࣗ෼͕ϦϑΝΫλϦϯάେ޷͖
    ਓ͔ؒͩΒ ๨೥ձ͔Βؼ͖ͬͯͯਲͬ෷͍ͳ͕ΒϦϑΝΫλϦϯά͢Δఔ౓ʹ͸େ޷͖

    View full-size slide

  25. िؒ൒͔͔ͬͯແࣄ׬ྃ
    w HPSNΛXSBQ͢Δ࡞ۀͷຊਞϒϥϯν͸ —
    w ͜ΕͰҰ൪͕͔͔࣌ؒΔͱ͜Ζ͸͓ΘͬͨͷͰɺ93BZͷ$BQUVSF
    Λ࢓ࠐΜͰ͍͘

    View full-size slide

  26. ϥούʔʹ$BQUVSFΛ௥Ճ
    w 93BZͷ$BQUVSFΛͦΕͧΕͷϝιουͰ΍Δ
    w ͜ͷํࣜͰ͸HPSN͕࣮ࡍʹ࣮ߦ͢Δ42-ΛऔΓग़ͤͳ͍ͨΊɺ୅ΘΓ
    ʹݺͼग़͠ͷҾ਺ͱίʔϧελοΫΛه࿥
    // Exec ͩΑ
    func (g *GormDB) Exec(ctx context.Context, sql string, values ...interface{}) *GormDB {
    if seg := xray.GetSegment(ctx); seg == nil {
    return g.wrap(g.db.Exec(sql, values...))
    }
    var res *GormDB
    tmp := []interface{}{sql}
    tmp = append(tmp, values...)
    caller := traceCaller("Exec", tmp...)
    xray.Capture(ctx, g.name, func(ctx context.Context) error {
    g.recordSQL(ctx, caller)
    res = g.wrap(g.db.Exec(sql, values...))
    return res.Error
    })
    return res
    }
    93BZະ࢖༻࣌
    93BZ࢖༻࣌

    View full-size slide

  27. த਎
    w ίʔϧελοΫͱ42-ͷBSHTΛه࿥
    w ه࿥෦෼͸93BZ4%,ͷxray.DB.populate()Λࢀߟʹ
    func traceCaller(name string, args ...interface{}) string {
    pc, _, line, _ := runtime.Caller(2)
    f := runtime.FuncForPC(pc)
    return fmt.Sprintf("from %s(Line %d): %s(%s) ", f.Name(), line, name, argsString(args...))
    }
    func (g *GormDB) recordSQL(ctx context.Context, info string) {
    seg := xray.GetSegment(ctx)
    typ := "master"
    if g.slave {
    typ = "slave"
    }
    seg.Lock()
    seg.Namespace = "remote"
    seg.GetSQL().ConnectionString = g.name
    seg.GetSQL().DatabaseType = typ
    seg.GetSQL().SanitizedQuery = info
    seg.Unlock()
    }

    View full-size slide

  28. ແࣄ93BZରԠ׬ྃ
    w 93BZ4%,GPS(PͷYSBZ42-Λ࢖ͬͨͱ͖ͷΑ͏ʹ͸ߦ͔ͳ
    ͔͕ͬͨɺ࠷௿ݶ͔͔ͬͨ࣌ؒͱݺͼग़͍ͯ͠Δ৔ॴ಺༰͸Έ͑Δ
    Α͏ʹͳͬͨɻ͜ͷ13—
    w ࠓ΋ແࣄຊ൪؀ڥͰ93BZͷτϨʔε͕ಈ͍͍ͯ·͢

    View full-size slide

  29. DPOUFYUʹ͍ͭͯ
    ͦͷଞͷ࿩୊

    View full-size slide

  30. ΫϥΠΞϯτ੾அʹΑΔDBODFM
    w ΫϥΠΞϯτཁҼͳͷͰཧ༝͸Θ͔Βͳ͍͚ͲɺεϚϗΞϓϦͰ͸ͨ
    ·ʹඵͱ͔ͰϦΫΤετΛΩϟϯηϧ͢ΔΫϥΠΞϯτ͕͍Δ
    w %#ίϛοτޙͷϩάॻ͍ͯΔͱ͖ʹΫϥΠΞϯτཁҼͷDPOUFYU
    DBODFM͕ൃੜ͢Δ͜ͱ͕͋ͬͯɺࠔΔ
    w IUUQ4FSWFS͔ΒΩϟϯηϧ͞Εͳ͍΄͏͕͏Ε͍͠

    View full-size slide

  31. ΫϥΠΞϯτ੾அʹΑΔDBODFM
    w ͦ΋ͦ΋ႈ౳ͳ࡞Γͷ"1*ʹͳ͍ͬͯΕ͹ɺαʔόʹϦΫΤετ͕དྷ
    ͨΒΫϥΠΞϯτʹؔ܎ͳ͘૸Γ͖ͬͨ΄͏͕Α͍
    w SFRSFR8JUI$POUFYU OFX$UY
    Ͱࠩ͠ସ͑Ͱ͖Δ
    w ϦΫΤετࣗମͷλΠϜΞ΢τ͸ඞཁͳͷͰɺSFRͷDUYΛܧঝͤͣ
    ʹDPOUFYU#BDLHSPVOE
    Ͱ৽͍͠ίϯςΩετΛ࡞ͬͯλΠϜ
    Ξ΢τΛઃఆ͢Δ
    w 4FSWFSΛ(SBDFGVMʹTIVUEPXO͢Δͷ͸ϦΫΤετλΠϜΞ
    ΢τ෼͘Β͍଴ͯ͹৽͍͠ϦΫΤετड͚෇͚ͳ͍ͷͰେৎ෉

    View full-size slide

  32. ·ͱΊײ૝
    w ઌԆ͹͍ͯ͠͠Δͱޙ͔ΒϦϑΝΫλϦϯά͢Δίετ͕͍͢͝ࣄʹ
    ͳΔͷͰͬ͞͞ͱDPOUFYUରԠͨ͠΄͏͕Α͍
    w ࠓ͔Β։ൃΛ࢝ΊΔͳΒɺ࢖͍ͬͯΔ%#03.΍3FEJTΞΫηεͳ
    Ͳͷύοέʔδ͸DPOUFYUରԠ͍ͯ͠Δ΋ͷΛબͿ΂͖

    ٕज़બఆͷ༏ઌ౓ͱ͔ͯ͠ͳΓߴ͍ͱ͜ΖʹҐஔ͚ͮΔ΂͖

    w ઃܭํ਑ͱͯ͠ɺ ͨͱ͑ͦΕΛ࣮૷͢Δͱ͖͸࢖ͬͯͳͯ͘΋
    ϩ
    δοΫతʹ֎෦ͷ*0͕͋Δ΋ͷ͸DPOUFYUΛ౉͓ͯ͘͠ͱΑ͍

    ͦ͏ͳΔͱ'MVFOUEͰϩάऔΔՄೳੑ͕͋Δͱ͜Ζ͸શ෦౉ͦ͏ʜͱͳΔ͚Ͳͦ͏͍͏΋Μͩ

    View full-size slide

  33. ิ଍ ͕࣌ؒ͋Ε͹

    View full-size slide

  34. ิ଍
    w YSBZ42-ͱDPOUFYUඇରԠ03.ͷ૬ੑʹ͍ͭͯɺ13ʹϝϞ͕
    ͋ΔͷͰͦΕΛҾ༻

    View full-size slide

  35. ิ଍ ͖ͭͮ

    w HPSN͔Β࣮ߦ͢Δੜͷ42-ΛऔΓग़ͯ͠93BZʹه࿥Ͱ͖ͳ͍ཧ༝
    w ิ଍͢ΔͱɺXSBQ͍ͯ͠ΔͷͰHPSNͷ࣮ମΛݺ͹ͳ͍ͱ8)&3&ͷ৚݅Λ૊Έཱ
    ͯͯ͘Εͳ͍ͷͰɺ࣮ମΛݺͿલʹه࿥Ͱ͖ͳ͍

    ࣮ମΛݺΜͩ͋ͱʹ஌Δํ๏͸ίʔϧόοΫͳΜ͚ͩͲͦΕʹDUY౉͢ํ๏͕ͳ͍ʜ

    View full-size slide