Goroutine meets a signal

60e652d0244f47d2da4e1f7e712f9328?s=47 K
April 15, 2018

Goroutine meets a signal

Go Conference 2018 Spring (https://gocon.connpass.com/event/82515/) でシグナルを Goroutine へブロードキャストする & それを context.Context でラップする話をしました

60e652d0244f47d2da4e1f7e712f9328?s=128

K

April 15, 2018
Tweet

Transcript

  1. (PSPVUJOFNFFUTBTJHOBM !(P$POGFSFODF4QSJOH

  2. !DPEFIFY !$PEF)FY w *MJLF1FSM94(P w *MJWFJO5PLZPGSPN0LJOBXB w ❤$BQCBUUJOHDFOUFS w .FSDBSJ

    *OD/FX w IUUQTDPEFIFYIBUFCMPKQ
  3. None
  4. Έͳ͞Μ

  5. γάφϧ࢖ͬͯ·͔͢ʁ

  6. ࢖ͬͯ·͢ΑͶʂ

  7. ஌Βͳ͍ਓͷͨΊʹ

  8. None
  9. શ͘໘ന͘ͳ͍ʜ

  10. % man signal ʮࠓ͙࣮͢ߦʯ

  11. Ͳ͏͍͏࣌ʹ
 γάφϧΛ࢖͍·͔͢ʁ

  12. γάφϧΛ࢖͏৔໘ w ॲཧΛ தஅcऴྃc࠶ىಈ ͍ͨ͠ w ׂΓࠐΈॲཧΛߦ͍͍ͨ w γάφϧΛૹΔ͜ͱͰڍಈΛมߋ͍ͨ͠ͳͲ
 ʢϨΞέʔεʁʣ

  13. γάφϧ͸ϓϩηεʹ
 ૹΓ·͢ΑͶ

  14. ߋʹݴ͏ͱ

  15. ਌ϓϩηεࢠϓϩηε
 ΁ͷγάφϧૹ৴΋Մೳ

  16. ͔͠͠

  17. զʑ͕ѻ͏΋ͷ͸
 HPSPVUJOF

  18. HPSPVUJOF΁
 γάφϧΛૹΓ͍ͨ

  19. ݁࿦͔Β

  20. HPSPVUJOF΁
 γάφϧΛૹ৴Ͱ͖ͳ͍

  21. ͕ͩ
 ࣅͨΑ͏ͳ͜ͱ͸Ͱ͖Δ

  22. γάφϧΛ௨஌͢Δ શͯͷ(PSPVUJOF΁ϒϩʔυΩϟετ

  23. ඞཁͳ͜ͱ w ௨஌༻ͷDIBOOFM w ͲΜͳγάφϧ͕ૹΒΕ͖͔ͯͨ

  24. ඞཁͳ͜ͱ w ௨஌༻ͷDIBOOFM w ͲΜͳγάφϧ͕ૹΒΕ͖͔ͯͨ

  25. close(ch)

  26. close(ch) DIΛ͍࣋ͬͯΔશͯͷHPSPVUJOF΁
 DIBOOFMΛૹ৴͢Δ͜ͱ͕Ͱ͖Δ

  27. ඞཁͳ͜ͱ w ௨஌༻ͷDIBOOFM w ͲΜͳγάφϧ͕ૹΒΕ͖͔ͯͨ

  28. signal.Notify(ch)

  29. signal.Notify(ch) DI͔Βड͚औΕ͹ྑ͍

  30. ࣮ࡍͷίʔυ

  31. γάφϧ௨஌Λߦ͏ߏ଄ମ

  32. NBJOͷॲཧ

  33. ΊͬͪΌ؆୯

  34. ͔͠͠ɺզʑ͸
 context.Context ΋ཉ͍͠

  35. context.Context
 γάφϧϋϯυϥͷ࣮૷ γάφϧ΋ड͚औΕΔcontext.Context

  36. context.Context

  37. context.Context w جຊతʹΩϟϯηϧͷͨΊͷDIBOOFMͷड͚౉͠ Λߦ͏໨తʹ࢖͏ w context.Value ΋͋Δ͕ɺࠓճ͸ؾʹ͠ͳ͍ w JOUFSGBDFͰ͋Δ

  38. context.Context

  39. ͞͞΍͔ͳϧʔϧ΋ʜ

  40. IUUQTHPMBOHPSHQLHDPOUFYU l%POPUTUPSF$POUFYUTJOTJEFBTUSVDU UZQFJOTUFBEʜz func DoSomething(ctx context.Context, arg Arg) error {


    // ... use ctx ...
 }
  41. ͳΔ΄Ͳ

  42. ͜͜Ͱ
 ࣍ͷ63-Λݟ·͠ΐ͏ IUUQTHJUIVCDPN$PEF)FYHPMFUTZOPQTJT

  43. None
  44. None
  45. None
  46. context.Context
 ҰͭͰ྆ํड͚औΕΔ

  47. ࣮૷ IUUQTHJTUHJUIVCDPN$PEF)FY DCFECGDBGGECGB

  48. ߏ଄ମ΁DUYΛ֨ೲ͢Δ

  49. ൓ٯߦҝͰ͢Ͷ

  50. JOUFSGBDFͷ࣮૷

  51. NBJOͷॲཧ

  52. ͜ΕͰԿ͕خ͍͠ͷ͔ʁ

  53. خ͍͜͠ͱ w ड৴෦෼ͷίʔυ͕؆ܿʹͳͬͯಡΈ΍͘͢ͳΔ w γάφϧͷ௨஌ͱDPOUFYUΩϟϯηϧͷ௨஌ͷ྆ ํΛγϯϓϧʹॻ͚Δ w ϥοϓ͢ΔલͷDPOUFYUͱͯ͠΋ѻ͑ΔͷͰ҆৺

  54. DPOUFYUͷϥοϓʹ ஫ҙ͢Δ͜ͱ

  55. ஫ҙ͢Δ͜ͱ w ֎෦͔ΒԿ͔͠Β௨஌͕͋ͬͯɺͦΕΛτϦΨʔʹ ֤HPSPVUJOF΁΋௨஌͍ͨ͠৔߹͸ྑͦ͞͏ w Ή΍Έʹϥοϓ͢Δͱத਎͕෼͔Βͳ͍ɺҙਤ͕Θ ͔Βͳ͍໰୊͕ൃੜ͢Δ

  56. ༷ʑͳؾ͕࣋ͪ͋Δͱ
 ࢥ͍·͕͢

  57. ͝੩ௌ
 ͋Γ͕ͱ͏͍͟͝·ͨ͠ 1MFBTFEJTDVTTXJUINF