Slide 1

Slide 1 text

ContextΞϯνύλʔϯ @__timakin__ golang.tokyo#5

Slide 2

Slide 2 text

ࣗݾ঺հ

Slide 3

Slide 3 text

ࣗݾ঺հ • twitter: @__timakin__ • github: timakin • גࣜձࣾGunosy ৽نࣄۀ։ൃࣨ(Go/Swift) • ओͳGoϥΠϒϥϦ։ൃ෺ • gopli (DBϨϓϦέʔγϣϯπʔϧ) • gonvert (จࣈίʔυม׵ϥΠϒϥϦ) • octop (githubͷissue, PRϏϡʔ༻CLIπʔϧ) • ts (ٕज़ɾϏδωεܥχϡʔε८ճCLIπʔϧ) • ϓϥΠϕʔτͰΫϩʔϥʔ࡞੒ɺ࠷ۙಈը഑৴पΓ͕ؾʹͳͬͯ·͢ɻ

Slide 4

Slide 4 text

Copyright© Gunosy Inc. All Rights Reserved 4 Go / Python ΤϯδχΞืूத ▶https://gunosy.co.jp/recruit/ Gunosy͸ɺ౦ژେֶʹ௨͏3ਓͷֶੜͷ
 ʮ৘ใΛੈքதͷਓʹ࠷దʹಧ͚͍ͨʯͱ͍͏૝͍͔Β࢝·Γ·ͨ͠ɻ ౦ূϚβʔζ্৔ɺ࿡ຊ໦ώϧζ΁ͷΦϑΟεҠసΛܦͯɺ
 େ͖͘੒௕͍ͯ͠ΔձࣾͰ׆༂͍ͨ͠ϝϯόʔΛืू͍ͯ͠·͢ɻ

Slide 5

Slide 5 text

ຊ೔ͷओࢫ

Slide 6

Slide 6 text

ContextΛ͋͋ͯ͜͠͏ͯͨ͠Β
 ΰουΦϒδΣΫτͰ͖ͨ ͢·Μ

Slide 7

Slide 7 text

Զ͕Ξϯνύλʔϯͩ ٩( ᐛ )و

Slide 8

Slide 8 text

ΞδΣϯμ • context.ContextͷऔΓѻ͍ʹ͍ͭͯ • Ξϯνύλʔϯͷ࣮ྫʢ΍ͬͪΌͬͨʂ٩( ᐛ )وʣ • ެࣜͷGoDocͰਪ঑͞ΕΔίϯηϓτ • ൓লͱվળ

Slide 9

Slide 9 text

Ξϯνύλʔϯͷ࣮ྫ

Slide 10

Slide 10 text

@GoConference 2017

Slide 11

Slide 11 text

͍ͭ͜Λݟͯ͘ΕɺͲ͏ࢥ͏ʁ

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

<- echoͷcustom context <- context in context (!!)

Slide 14

Slide 14 text

͞Βʹ͍ͭ͜Λݟͯ͘Εʂ

Slide 15

Slide 15 text

͍ͭ͜Λݟͯ͘ΕɺͲ͏ࢥ͏ʁ

Slide 16

Slide 16 text

͍ͭ͜Λݟͯ͘ΕɺͲ͏ࢥ͏ʁ <- repository in context (!?)

Slide 17

Slide 17 text

͏͢͏͢ײ͍ͮͯ͸͍ͨΑʂʂ ̍̍

Slide 18

Slide 18 text

͢·ΜͱࢥͬͯΔ ٩( ᐛ )و

Slide 19

Slide 19 text

ݴ͍༁

Slide 20

Slide 20 text

context(context.Context) in context(ϑϨʔϜϫʔΫͷContext) • echoͷϑϨʔϜϫʔΫͷcontextͰ͸ͳ͘ඪ४ύοέʔδ ͷcontextΛ࢖ͬͯɺ಺෦ͷϩδοΫͰར༻͍ͯ͠Δ
 goroutineͷλΠϜΞ΢τ੍ޚΛ͍ͨ͠ • ͕ɺϦΫΤετϋϯυϥʹ౉ͬͯ͘Δͷ͸
 echo.Contextͩ͠ɺGAEͩͱhttp.Request͔Β
 contextΛऔΓग़ͤͳ͍ɻ • ϋϯυϥʹcontextΛ౉͢ʹ͸echo.Contextʹ
 context.ContextΛؚΊɺͦΕΛϋϯυϥҎԼͷ
 ϨΠϠʔͰར༻͢Δඞཁ͕͋Δ

Slide 21

Slide 21 text

<- echoͷcustom context <- context in context (!!)

Slide 22

Slide 22 text

repository in context • ਃ͠։͖͍͟͝·ͤΜʂʂ̍̍
 ͍͕ͭ͜ਆͩʂ • ·͊ͿͬͪΌ͚طଘͷ࣮૷(or ࢿྉ)ࢀߟʹ
 ࡞ͬͨΜͰ͕͢ɺέʔεόΠέʔεͰɺ
 ΘΓͱ͜͏͍͏globalͳΦϒδΣΫτΛೖΕͯΔ
 ݹ͍࣮૷ͱ͔͋ͬͯɺਅࣅͪΌͬͨɻ • ඪ४ύοέʔδͱͯ͠૊Έࠐ·ΕΔલͷ࣮૷Ͱ͸
 ͜͏͍͏ͷ΋͋ͬͨΑɺ͋ͬͨ͸ͣͳΜͩɻ • ਖ਼௚repository͚ͩ͡Όͳ͘configͱ͔΋٧ΊͪΌͬͨ

Slide 23

Slide 23 text

Ҏ্ʹରͯ͠օ͞Μौ͍൓Ԡ

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

ͦ͏͍͑͹GoDocͰ͸

Slide 26

Slide 26 text

GoDoc • Package context defines the Context type, 
 which carries deadlines, cancelation signals, and other request-scoped values across API boundaries and between processes. • Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions. • Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. • ͢·Μͯɻ

Slide 27

Slide 27 text

GoDoc • ओʹgoroutine͕བྷΉॲཧɺAPIΛ·͙ͨॲཧΛߦ͏࣌ɺ
 λΠϜΞ΢τ੍ޚΛద੾ʹߦ͏ͨΊʹ࢖͏ • UserIDɺଞJWT౳ͷɺͦͷϦΫΤετʹݶఆ͞ΕΔ஋ΛGet/Set͢Δ ͱ͖ʹͷΈValueΛ࢖͏ • Go1.7Ҏલ(ࠓͷGAEͱ͔)
 func GetHandler(ctx context.Context, 
 w http.ResponseWriter, r *http.Request)
 
 Go1.7Ҏ߱
 ctx := r.Context() • ଞͷ༻్ɺड͚౉͠ํͰ࢖Θͳ͍ɻ
 ઈରͩͧɻ

Slide 28

Slide 28 text

൓লͱվળ

Slide 29

Slide 29 text

GoDoc • ແବʹߏ଄ମʹcontextೖΕͳ͍ • echo -> net/http • ContextΛద੾ʹϋϯυϥʹ౉͢ • λΠϜΞ΢τઃఆ͸ServeHTTPͰߦ͏ʢMiddlewareͰ΋Մʣ • func Handler(ctx context.Context, w http.ResponseWriter, r *http.Request) • request-scopedͳ஋ʹݶఆ • repository.FromContextͱ͔શ෦ফ͢ • ϋϯυϥͰ౎౓ctxΛ౉ͯ͠ੜ੒ • ֤छΫϥΠΞϯτ΍Βlogger͸HandlerͷϨγʔόΛఆٛͯͦ͠ͷதʹೖΕΔ • function (app *App) GetItemHandler(ctx context.Context, …)

Slide 30

Slide 30 text

ແବʹߏ଄ମʹcontextೖΕͳ͍ • echoΛnet/httpʹશஔ͖׵͑ • GAE(SE)ͩͱrequestʹContext͕ೖͬͯͳ͍ɻ
 ͞Βʹɺecho.ContextͷͲ͔͜Β΋context.Context͕ͱ Γͩͤͳ͍ɻ • ͦ΋ͦ΋GAEΛ࢖͍ͬͯͨͷͰɺechoඋ͑෇͚ͷϩΨʔ ͱ͔࢖Θͳ͍͠ɺrouting͘Β͍Ͱ͔͠Ըܙͳ͍ • ϑϨʔϜϫʔΫ͕଍͔ͤʹͳ͍ͬͯͨͷͰɺ֎͢ɻ • ϋϯυϥʹඪ४ͷcontext͚ͩ౉͢Լ४උɻ

Slide 31

Slide 31 text

ContextΛద੾ʹϋϯυϥʹ౉͢ • λΠϜΞ΢τઃఆ͸ServeHTTPͰߦ͏ʢMiddleware Ͱ΋Մʣ ServeHTTPΛ࣮૷ͨ͠AppHandlerΛఆٛͯ͠ɺ
 தͰappengineͷContext(context.Context)Λ λΠϜΞ΢τઃఆ෇͖Ͱϋϯυϥʹ౉͢

Slide 32

Slide 32 text

request-scopedͳ஋ʹݶఆ • ͔ͯrequest-scopedͳ஋ͱ͔ಛʹͳ͍APIͩͬͨɻ • ͳͷͰWithValueશ෦ফͨ͠ɻ • repository.FromContextͱ͔શ෦ফ͢ • ϋϯυϥͰ౎౓ctxΛ౉ͯ͠ੜ੒ • GAEͩͱloggerͱ͔datastoreͰcontext͕ඞཁ • ͳͷͰɺglobalʹѻ͍͍ͨ஋ͩͱͯ͠΋ɺ
 ϋϯυϥҎ߱Ͱຖճݺͼग़ͯ͠·͢ɻ
 ʢ͜Ε͸ϕετϓϥΫςΟε͋Δͷ͔ʁʣ

Slide 33

Slide 33 text

Ϩγʔόʹglobalͳ஋ΛೖΕΔ

Slide 34

Slide 34 text

• (app *App)ΛϨγʔόͱͯ͠ઃఆ͢Δʹ͸handlerʹ ౰ͨΔϝιου͸શͯಉҰpackageͱͯ͠ఆٛ͞ΕΔ ඞཁ͕͋Δ • cannot define new methods on non-local type • ͳͷͰɺݸผͷυϝΠϯϩδοΫΛ࣋ͭσΟϨΫτϦ ͱ͸ผʹɺhandlerσΟϨΫτϦΛஔ͖ɺͦ͜ʹApp ͷఆٛͱݸผͷϦΫΤετϋϯυϥΛ࣋ͨͤΔɻ Ϩγʔόʹglobalͳ஋ΛೖΕΔ

Slide 35

Slide 35 text

app.go

Slide 36

Slide 36 text

main.go handler.AppHandler{Impl: app.HogeHandler}
 ͱ͍͏ܗࣜͰɺServeHTTPΛ࣋ͭAppHandlerʹ App structΛ࣋ͭHogeHandlerΛ౉͢ɻ

Slide 37

Slide 37 text

·ͱΊ • ਆ͸ࢮΜͩɻcontextড়Խ׬ྃɻ • request-scopedͳ஋ͱɺλΠϜΞ΢τҎ֎࢖Θͳ͍ɻ • άϩʔόϧͳΦϒδΣΫτΛར༻͢ΔͨΊʹ
 ϋϯυϥڞ௨ͷstructΛ༻ҙ͢Δɻ
 ͦͷӨڹͰϋϯυϥ͸ಉҰpackageʹ·ͱΊΔɻ