Share
Embed
Transcript
46 slides
-
Cleanup handling in Go খࢁ݈Ұ / GMO PEPABO inc. 2024.6.8
Go Conference 2024 1 -
ࣗݾհ ٕज़෦ɹٕज़ج൫νʔϜ 2018 த్ೖࣾ খࢁ ݈Ұ Ken’ichiro Oyama গ࣮͠༻తͰখ͞ͳOSSΛॻ͘ͷ͕झຯɻ •
GitHub : k1LoW • Twitter : @k1LoW 2 -
• GogoroutineʹΑΔฒߦॲཧΛ༰қʹ࣮ݱͰ͖Δɻ͔͠͠ɺͦͷฒߦॲཧͷऴྃॲཧ։ൃऀ ʹҕͶΒΕ͍ͯΔɻ • ॲཧΛͨͩఀࢭ͢Δ͚ͩͰࡁ·ͳ͍߹ଟ͘ɺదͳऴྃॲཧɺͭ·ΓʮΫϦʔϯΞοϓʯ ͕ඞཁɻ • ྫ͑golang.org/x/sync/errgroupʮ։࢝ͨ͠ฒߦॲཧͷྃʢʹऴྃʣΛͭʯͱ͍͏ ҙຯͰฒߦॲཧͷऴྃॲཧͷͨΊͷpackageͱݴ͑Δ •
ຊηογϣϯΛ௨ͯ͡ɺօ͞ΜʹΫϦʔϯΞοϓॲཧʹ͍ͭͯվΊͯߟ͑ͯΒ͍ɺ֤։ൃݱ ʹ͋ΔͰ͋Ζ͏ΫϦʔϯΞοϓॲཧʹ͍ͭͯGoίϛϡχςΟͰձ͕͞ΕΔ͜ͱΛظ͍ͯ͠ Δɻ 6 ຊηογϣϯͷϞνϕʔγϣϯ దͳऴྃॲཧ=ΫϦʔϯΞοϓ -
• ʮޙ࢝ॲཧʯʮదͳऴྃॲཧʯ • τϥϯβΫγϣϯॲཧ: த్ఀࢭ͢Δͱσʔλ߹ੑ͕ࣦΘΕΔͨΊɺ࠷ޙ·Ͱྃͤ͞Δ ʢϩʔϧόοΫؚΉʣඞཁ͕͋Δɻ • ֎෦Ϧιʔεͱͷଓ: Ϧιʔεղ์ଓղআΛߦ͏ඞཁ͕͋Δɻ •
Ұ࣌ϑΝΠϧσʔλ: ऴྃޙʹෆཁʹͳΔͨΊɺআ͢Δඞཁ͕͋Δɻ 8 ʮΫϦʔϯΞοϓʯͱ ຊηογϣϯʹ͓͚ΔʮΫϦʔϯΞοϓʯ -
• Goͷݴޠػೳͱͯ͠ఏڙ͞Ε͍ͯΔεςʔτϝϯτ • ొͨؔ͠Λݺͼग़͠ݩͷؔͷऴΘΓʢreturnʣ·Ͱ࣮ߦΛԆͤ͞Δ͜ͱ͕Ͱ͖Δ • A defer statement pushes a
function call onto a list. The list of saved calls is executed after the surrounding function returns. Defer is commonly used to simplify functions that perform various clean-up actions. Deferred function calls are executed in Last In First Out order after the surrounding function returns. • Ref: https://go.dev/blog/defer-panic-and-recover • 11 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ defer -
• ొ͢Δؔͷγάωνϟ • func() • ࣮ߦλΠϛϯά • ݺͼग़͠ݩͷؔͷऴΘΓ • ࣮ߦॱ൪
• LIFO͔ͭஞ࣮࣍ߦ • ొ͞Εͨؔͷ࣮ߦྃʢpanicҎ֎ʣ • ͭ 12 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ defer -
• net/httpύοέʔδͷ*ServerΛϨγʔόͱͯͭ͠ϝιου • RegisterOnShutdownͰొͨؔ͠Server.ShutdownͰ࣮ߦ͞ΕΔ • RegisterOnShutdown registers a function to
call on [Server.Shutdown]. This can be used to gracefully shutdown connections that have undergone ALPN protocol upgrade or that have been hijacked. This function should start protocol-specific graceful shutdown, but should not wait for shutdown to complete. • Ref: https://github.com/golang/go/blob/adbfb672ba485630d75f8b5598228a63f4af08a4/src/ net/http/server.go#L2985-L2994 • 14 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ func (*Server) RegisterOnShutdown -
• ొ͢Δؔͷγάωνϟ • func() • ࣮ߦλΠϛϯά • Server.Shutdown࣮ߦ࣌ • ࣮ߦॱ൪
• FIFO͔ͭฒߦ࣮ߦ • ొ͞Εͨؔͷ࣮ߦྃʢpanicҎ֎ʣ • ͨͳ͍ 15 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ func (*Server) RegisterOnShutdown -
• testingύοέʔδͷ*TΛϨγʔόͱͯͭ͠ϝιου • CleanupͰొͨؔͦ͠ͷςετͱαϒςετ͕શͯྃͨ͠ͱ͖ʹ࣮ߦ͞ΕΔ • t.Parallel()ʹΑͬͯςετ͕ฒߦ࣮ߦ͞Ε͍ͯͯͦͷ࣮ߦ݅มΘΒͳ͍ • Cleanup registers a
function to be called when the test (or subtest) and all its subtests complete. Cleanup functions will be called in last added, first called order. • Ref: https://github.com/golang/go/blob/adbfb672ba485630d75f8b5598228a63f4af08a4/src/ testing/testing.go#L1151-L1153 17 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ func (*T) Cleanup -
• ొ͢Δؔͷγάωνϟ • func() • ࣮ߦλΠϛϯά • ରͷςετͱͦͷαϒςετͷऴྃ࣌ • ࣮ߦॱ൪
• LIFO͔ͭஞ࣮࣍ߦ • ొ͞Εͨؔͷ࣮ߦྃʢpanicҎ֎ʣ • ͭ 18 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ func (*T) Cleanup -
• ctx͕ऴྃͨ͠ͱ͖ʹొͨؔ͠fΛ࣮ߦ͢Δ • Multiple calls to AfterFunc on a context
operate independently; one does not replace another. (ུ) The stop function does not wait for f to complete before returning. If the caller needs to know whether f is completed, it must coordinate with f explicitly. • Ref: https://github.com/golang/go/blob/adbfb672ba485630d75f8b5598228a63f4af08a4/src/ context/context.go#L297-L314 • 20 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ func AfterFunc(ctx Context, f func()) (stop func() bool) -
• ొ͢Δؔͷγάωνϟ • func() • ࣮ߦλΠϛϯά • ctxͷΩϟϯηϧ࣌ • ࣮ߦॱ൪
• FIFO͔ͭฒߦ࣮ߦ • ొ͞Εͨؔͷ࣮ߦྃʢpanicҎ֎ʣ • ͨͳ͍ 21 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ func AfterFunc(ctx Context, f func()) (stop func() bool) -
23 ݴޠػೳඪ४ύοέʔδʹ͋ΔΫϦʔϯΞοϓػߏ ֤ػೳͷൺֱ ൃදऀ͕૬ରతʹྑ͍ಛੑͱߟ͍͑ͯΔ ※ defer func (*Server) RegisterOnShutdown func
(*T) Cleanup func AfterFunc (ctx Context, f func()) (func() bool) ొ͢Δ ؔγάωνϟ func() func() func() func() ొՄೳͳείʔϓ ରͱ͢Δؔ *Server͕͍ͬͯΕͲ͜Ͱ *T͕͍ͬͯΕͲ͜Ͱ ctx͕͍ͬͯΕͲ͜Ͱ ࣮ߦλΠϛϯά ݺͼग़͠ݩͷ ؔͷऴΘΓ Server.Shutdown࣮ߦ࣌ ݺͼग़͠ݩͷ ςετؔͷऴΘΓ ctxͷΩϟϯηϧ࣌ ࣮ߦॱ൪ LIFO FIFO LIFO FIFO ஞ࣍/ฒߦ ஞ࣍ ฒߦ ஞ࣍ ฒߦ ొ͞Εͨؔͷ ࣮ߦྃ ʢpanicҎ֎ʣ ͭ ͨͳ͍ ͭ ͨͳ͍ -
✓ ෳͷύοέʔδʢNot ֎෦ύοέʔδʣͰߏ͞Ε͍ͯͯύοέʔδؒͰґଘ͕ؔ͋Δ ✓ GoroutineʹΑΔฒߦॲཧ͕׆༻͞Ε͍ͯΔ ✓ ͦͯͦ͠ΕΒ͕ࠓޙ૿͍͑ͯ͘༨͕͋Δ • ؆қͳWebΞϓϦέʔγϣϯ͕গ͠ͰෳࡶʹͳΔͱͯ·ΔՄೳੑ͕͋Δ 25
ෳࡶͳΞϓϦέʔγϣϯʹ͓͍ͯٻΊΒΕΔΫϦʔϯΞοϓ ຊηογϣϯʹ͓͚ΔʮෳࡶͳΞϓϦέʔγϣϯʯ -
✓ ෳͷύοέʔδؔΛ·͍ͨͰΫϦʔϯΞοϓॲཧΛొ͍ͨ͠ ✓ ฒߦॲཧͰؔͳ͘ΫϦʔϯΞοϓॲཧΛొ͍ͨ͠ ✓ ʢͰ͖ΕʣΞϓϦέʔγϣϯΞʔΩςΫνϟʹӨڹΛ༩͑ͳ͍ܗͰ࣮ݱ͍ͨ͠ ✓ ඞཁʹԠͯ͡ΫϦʔϯΞοϓॲཧͷྃΛ͍ͪͨ ✓ ΫϦʔϯΞοϓॲཧͷΤϥʔัଊ͍ͨ͠
• Ͳ͜ͰΫϦʔϯΞοϓॲཧΛొͰ͖ͯɺඞཁʹԠͯͦ͡ΕΒͷॲཧͷྃΛͯͯɺΤϥʔ ͷัଊͰ͖Δͱྑ͍ 26 ෳࡶͳΞϓϦέʔγϣϯʹ͓͍ͯٻΊΒΕΔΫϦʔϯΞοϓ ෳࡶͳΞϓϦέʔγϣϯʹ͓͍ͯٻΊΒΕΔΫϦʔϯΞοϓ -
+ ύοέʔδؔΛԣஅͨ͠ΫϦʔϯΞοϓॲཧͷ࣮ݱํ๏ • ෳͷύοέʔδؔΛ·͙ͨඞཁ͕͋Δɻ • Goͷίʔυʹ͓͍ͯෳͷύοέʔδؔΛ·͍ͨͰ͍Δͷ context.Context • context.ValueΛ͏͜ͱͰʮෳͷύοέʔδ~ʯʮฒߦॲཧʯղܾͰ͖ͦ͏ •
طଘͷ context.Context Λ͏͜ͱͰΞʔΩςΫνϟͷӨڹ࠷খݶͰࡁΉ + ΫϦʔϯΞοϓॲཧͷྃͪͷ࣮ݱํ๏ • ٻΊΔڍಈͱͯ͠ sync.WaitGroup golang.org/x/sync/errgroup.Group ͕͍ۙ = context.WithValueܦ༝Ͱ context.Context ʹ *sync.WaitGroup / *errgroup.Group Λ࣋ͨͤͯ ֤ॴͰΫϦʔϯΞοϓॲཧΛొ͢Δ 28 ఏҊख๏ ఏҊΞʔΩςΫνϟ -
• donegroup is a package that provides a graceful cleanup
transaction to context.Context when the context is canceled. • https://github.com/k1LoW/donegroup • طଘίʔυͰൖ͍ͤͯ͞Δcontext.ContextΛͬͯύοέʔδؔΛԣஅͨ͠τϥϯβΫ γϣϯʢಛʹॲཧͷྃͷཧʣΛఏڙ͢ΔύοέʔδͰ͢ɻ • ඪ४ύοέʔδͷΈͰ࣮͍ͯ͠ΔͷͰZero dependency 34 ఏҊख๏ donegroup -
• ొ͢Δؔͷγάωνϟ • func() error • ࣮ߦλΠϛϯά • ctxͷΩϟϯηϧ࣌ •
࣮ߦॱ൪ • FIFO͔ͭฒߦ࣮ߦ • ొ͞Εͨؔͷ࣮ߦྃʢpanicҎ֎ʣ • ʢdonegroup.WaitͰʣͭ 35 ఏҊख๏ donegroup.Cleanup -
37 ఏҊख๏ ֤ػೳͷൺֱ defer func (*Server) RegisterOnShutdown func (*T) Cleanup
func AfterFunc (ctx Context, f func()) (func() bool) dongroup.Cleanup ొ͢Δ ؔγάωνϟ func() func() func() func() func() error ొՄೳͳείʔϓ ରͱ͢Δؔ *Server͕͍ͬͯΕͲ͜Ͱ *T͕͍ͬͯΕͲ͜Ͱ ctx͕͍ͬͯΕͲ͜Ͱ ctx͕͍ͬͯΕͲ͜Ͱ ࣮ߦλΠϛϯά ݺͼग़͠ݩͷ ؔͷऴΘΓ Server.Shutdown࣮ߦ࣌ ݺͼग़͠ݩͷ ςετؔͷऴΘΓ ctxͷΩϟϯηϧ࣌ ctxͷΩϟϯηϧ࣌ ࣮ߦॱ൪ LIFO FIFO LIFO FIFO FIFO ஞ࣍/ฒߦ ஞ࣍ ฒߦ ஞ࣍ ฒߦ ฒߦ ొ͞Εͨؔͷ ࣮ߦྃ ʢpanicҎ֎ʣ ͭ ͨͳ͍ ͭ ͨͳ͍ ʢdongroup.WaitͰʣ ͭ ൃදऀ͕૬ରతʹྑ͍ಛੑͱߟ͍͑ͯΔ ※ -
• donegroup.AwaiterͱҟͳΓɺgoroutineͰฒߦ࣮ߦ͞ΕΔ • errgroup func (*Group) Goͷڍಈͱ΄΅ಉ͡ • ݸਓతʹerrgroupʹ׳Ε͍ͯΔͷͰΈͷγϯλοΫε •
context.ContextΛ௨ͯ͡ύοέʔδؔΛ·͙ͨ͜ͱ͕Ͱ͖Δ 41 ύοέʔδؔΛ·͍ͨͩ errgroup func (*Group) GoϥΠΫͳؔ donegroup.GoͰฒߦ࣮ߦΛ։࢝ͯ͠donegroup.WaitͰ࣮ߦྃΛͭ -
• context.WithCancelCause ͔ΒಘͨΛ context.Value + sync.WaitGroup ͰՃ • context.ContextͷࢠؔʹରԠ 43
donegroupͷίΞ࣮ context.Value + sync.WaitGroup -
45 ͓ΘΓʹ ͓ΘΓʹ • ຊηογϣϯʹ͓͚ΔʮΫϦʔϯΞοϓʯΛఆٛͨ͋͠ͱɺݴޠػߏඪ४ύοέʔδͰΫϦʔ ϯΞοϓΛϢʔεέʔεͷ1ͭʹͭػೳʹ͍ͭͯ͋ͨΒΊͯΈ͍ͯͬͨɻ • ࣍ʹɺΑΓෳࡶͳίʔυϕʔεΛ࣋ͭΞϓϦέʔγϣϯʹ͓͍ͯඞཁʹͳΔΫϦʔϯΞοϓॲཧ ʹ͍ͭͯߟ͑ཁ݅Λྻڍͨ͠ɻ •
্هཁ݅Λຬͨ͢ΞʔΩςΫνϟͱͯ͠ context.Value ͱ sync.WaitGroupʢ errgroup.Groupʣ ΛΈ߹ΘͤͨߏΛఏҊ͠ɺͦͷ۩ମత࣮ͱͯ͠ donegroup Λհͨ͠ɻ • ʢվΊͯʣʮΫϦʔϯΞοϓॲཧʯʮάϨʔεϑϧγϟοτμϯʯͷ࣮։ൃͷݱʹଟ ͘ଘࡏ͢ΔͣͳͷͰɺੋඇɺࠓճΛػձʹݟ͕GoίϛϡχςΟʹڞ༗͞Εͯ΄͍͠ʢੋඇ Γ͍ͨʣɻ