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

circuit_switch: small start to create gem

Cfbe23392787cb3ad4689c5f72463fcc?s=47 makicamel
October 10, 2021

circuit_switch: small start to create gem

at 2021.10.10. Kaigi on Rails _2021_ new

Cfbe23392787cb3ad4689c5f72463fcc?s=128

makicamel

October 10, 2021
Tweet

Transcript

  1. DJSDVJU_TXJUDI 
 ͍ͪ࢝͘͞ΊΔHFNͮ͘Γ 2021.10.10. Kaigi on Rails _2021_ new @makicamel

  2. w!NBLJDBNFM઒ݪສق w3VCZͱϏʔϧɹɹͱ͓ञ͕޷͖ w޷͖ͳ73ήʔϜ ࣗݾ঺հ

  3. ࠷ۙࠔ͍ͬͯΔ͜ͱ ͓࢓ࣄͰ

  4. ো֐͕ଟ͍

  5. •ӨڹൣғௐࠪΛ͕Μ͹Ζ͏ •ϨϏϡʔΛ͕Μ͹Ζ͏ •ࣗಈςετͷ௥ՃɾϝϯςΛ͕Μ͹Ζ͏ •खಈςετΛ͕Μ͹Ζ͏ •Τϥʔ؂ࢹΛ͕Μ͹Ζ͏ ো֐Λىͨ͜͠ਓྨͷߟ͕͑ͪͳ͜ͱ ΋ͬͱ ΋ͬͱ ΋ͬͱ ΋ͬͱ

    ΋ͬͱ
  6. ͕Μ͹Γͨ͘ͳ͍

  7. •͕Μ͹ΔͱϛεΔ •͕Μ͹Γ͸ݸਓʹґଘ •͕Μ͹ΔͱർΕΔ •͕Μ͹Δ͸Ӭԕʹ͸ܧଓͰ͖ͳ͍ ͕Μ͹Γͨ͘ͳ͍

  8. ࢓૊ΈͰͳΜͱ͔͍ͨ͠

  9. ɹো֐͕ଟ͍ΜͰ͢Α…😣 ɹ90% ϛε͕ͳ͍͜ͱͷอূ͸ͦͦ͜͜Ͱ͖Δ͚Ͳ 
 ɹ100% ϛε͕ͳ͍͜ͱͷอূ͸ͱͯ΋ߴίετ ɹϦϦʔεස౓Λ্͛Δͷ͕೉ͯ͘͠…😭 ɹ͙͢ʹ੾Γ໭ͤΔΑ͏ʹͨ͠ํ͕ 
 ɹ݈શ͔΋͠Ε·ͤΜͶ

  10. •ϦϦʔε͸िʹ 1 ճ ˞ 
 ˞ো֐ରԠΛআ͘

  11. •ϦϦʔε͸िʹ 1 ճ •100ʙ200 Pull Requests •500ʙ1,000 commits •10Kʙ60K lines

    ˞ ϏοάόϯϦϦʔε 
 ˞Ұ൪ݹ͘ػೳ͕ଟ͍ϞϊϦγοΫϦϙδτϦʹ͓͚Δ਺ɻଟ͘ͷଞϦϙδτϦ͸΋ͬͱԺ΍͔Ͱ͢ 
 ˞ো֐ରԠΛআ͘ ˞
  12. ϦϦʔείετ͕ߴͯ͘ো֐͕ଟ͘ɾେ͖͘ͳΓ͕ͪ ো֐͕ଟͯ͘ϦϦʔεස౓Λ্͛ΒΕͳ͍ ϏοάόϯϦϦʔε

  13. ͱ͍͏Θ͚Ͱ

  14. ͭͬͨ͘ NBLJDBNFMDJSDVJU@TXJUDI 
 IUUQTHJUIVCDPNNBLJDBNFMDJSDVJU@TXJUI

  15. circuit_switch

  16. ɹো֐͕ଟ͍ΜͰ͢Α…😣 ɹ90% ϛε͕ͳ͍͜ͱͷอূ͸ͦͦ͜͜Ͱ͖Δ͚Ͳ 
 ɹ100% ϛε͕ͳ͍͜ͱͷอূ͸ͱͯ΋ߴίετ ɹϦϦʔεස౓Λ্͛Δͷ͕೉ͯ͘͠…😭 ɹ͙͢ʹ੾Γ໭ͤΔΑ͏ʹͨ͠ํ͕ 
 ɹ݈શ͔΋͠Ε·ͤΜͶ

  17. circuit_switch ΍Γ͍ͨ͜ͱɿ ϦϦʔεස౓Λ͋͛Δ 
 ˞ϦϦʔεස౓΋͍͋͛ͨͰ͢

  18. circuit_switch ΍Γ͍ͨ͜ͱɿ ௿ίετʹ੾Γ໭͢

  19. circuit_switch  if CircuitSwitch.open?(close_if_reach_limit: false) do_something_experimental else call_existing_codes end 

    CircuitSwitch.report(if: unexpected_happen)
  20. circuit_switch  if CircuitSwitch.open?(close_if_reach_limit: false) do_something_experimental else call_existing_codes end

  21. circuit_switch  •CircuitSwitch ͕։͍͍ͯΔ࣌ do_something_experimental ΛݺͿ •CircuitSwitch ͕ด͡ΒΕͨ࣌ call_existing_codes ΛݺͿ

    if CircuitSwitch.open?(close_if_reach_limit: false) do_something_experimental else call_existing_codes end
  22. circuit_switch •CircuitSwitch ͷ։ด৚݅ نఆ্ݶճ਺ʹ౸ୡ ։์৚݅ͱҰக ด࠯৚݅ͱҰக ৗ࣮࣌ߦʴڧ੍ด࠯

  23. circuit_switch •CircuitSwitch ͷ։ด৚݅  if CircuitSwitch.open?(reach_limit: 100) do_something_experimental else call_existing_codes

    end نఆ্ݶճ਺ʹ౸ୡ ։์৚݅ͱҰக ด࠯৚݅ͱҰக ৗ࣮࣌ߦʴڧ੍ด࠯
  24. circuit_switch  if CircuitSwitch.open?(if: current_user.testing?) do_something_experimental else call_existing_codes end •CircuitSwitch

    ͷ։ด৚݅ نఆ্ݶճ਺ʹ౸ୡ ։์৚݅ͱҰக ด࠯৚݅ͱҰக ৗ࣮࣌ߦʴڧ੍ด࠯
  25. circuit_switch  if CircuitSwitch.open?( close_if: Date.today > some_period ) do_something_experimental

    else call_existing_codes end •CircuitSwitch ͷ։ด৚݅ نఆ্ݶճ਺ʹ౸ୡ ։์৚݅ͱҰக ด࠯৚݅ͱҰக ৗ࣮࣌ߦʴڧ੍ด࠯
  26. circuit_switch   rake circuit_switch:terminate_to_run 
 [/app/services/greetings_service:21 block 
 in

    validate] •CircuitSwitch ͷ։ด৚݅ if CircuitSwitch.open?(close_if_reach_limit: false) do_something_experimental else call_existing_codes end نఆ্ݶճ਺ʹ౸ୡ ։์৚݅ͱҰக ด࠯৚݅ͱҰக ৗ࣮࣌ߦʴڧ੍ด࠯
  27. circuit_switch  CircuitSwitch.report(if: unexpected_happen) •CircuitSwitch ͕։͍͍ͯΔ࣌ report ΛૹΔ •Slack ΍

    Bugsnag ͳͲ೚ҙͷϨϙʔτπʔϧΛࢦఆ •։ด৚݅͸ open? ͱྨࣅͷ৚݅Λࢦఆ
  28. circuit_switch •ґଘϥΠϒϥϦ •ActiveRecord •࣮ߦߦʢcallerʣɾCircuitSwitch ͷ։ดঢ়ଶΛ DB ʹอଘ •ActiveJob •DB ߋ৽ɾϨϙʔτ

  29. circuit_switch •ίʔυϦʔμʔͷΈͲ͜Ζ

  30. circuit_switch •ίʔυϦʔμʔͷΈͲ͜Ζ •ͳ͍

  31. circuit_switch •ίʔυϦʔμʔͷΈͲ͜Ζ •ͳ͍ •ͳ͍ͷ͕ΈͲ͜Ζʢʁʣ

  32. ΈͲ͜Ζ •Կ΋มΘͬͨ͜ͱΛ΍͍ͬͯͳ͍ •Rails ͷίʔυ͕ॻ͚ͨΒॻ͚Δ •ࠓ͙͢ॻ͖࢝ΊΒΕΔ

  33. ΈͲ͜Ζ •;ͭ͏ͷ Rails ΞϓϦ͕ґଘ͍ͯ͠ΔϥΠϒϥϦʹґଘ •ࠓ͙͢ಋೖͰ͖Δ

  34. ͱ͜ΖͰ͓ؾ͖͔ͮͱࢥ͍·͕͢

  35. Inspired by •Feature Toggles •CircuitBreaker 'FBUVSF5PHHMFTNBSUJOGPXMFSDPN 
 IUUQTNBSUJOGPXMFSDPNBSUJDMFTGFBUVSFUPHHMFTIUNM $JSDVJU#SFBLFSNBSUJOGPXMFSDPN 


    IUUQTNBSUJOGPXMFSDPNCMJLJ$JSDVJU#SFBLFSIUNM
  36. Feature Toggles •৽ػೳΛಛఆϢʔβʹ҆શɾ଎΍͔ʹఏڙ͢ΔͨΊͷ࢓૊Έ •ؔ࿈ɿDark Launch •🙅 େن໛ feature ϒϥϯν্Ͱͷ։ൃ 


    🙆 master(main) ϒϥϯνʹ߹ྲྀͨ͠։ൃ 'FBUVSF5PHHMFTNBSUJOGPXMFSDPN 
 IUUQTNBSUJOGPXMFSDPNBSUJDMFTGFBUVSFUPHHMFTIUNM
  37. Feature Toggles •ΧφϦΞϦϦʔε •ϥϯμϜબग़ͷۃҰ෦ͷҰൠϢʔβʹػೳ։์༷ͯ͠ࢠݟ •A / B ςετ 'FBUVSF5PHHMFTNBSUJOGPXMFSDPN 


    IUUQTNBSUJOGPXMFSDPNBSUJDMFTGFBUVSFUPHHMFTIUNM
  38. CircuitBreaker •αʔόʹಛఆճ਺Ҏ্ͷো֐͕ൃੜͨ࣌͠ʹ 
 ϦΫΤετΛड͚෇͚ͣʹΤϥʔΛฦ͢࢓૊Έ •ো֐ͷ࿈࠯తͳൃੜΛ๷͙ •αʔό͕վળͨ͠ΒࣗಈͰ෮ؼ͢Δ $JSDVJU#SFBLFSNBSUJOGPXMFSDPN 
 IUUQTNBSUJOGPXMFSDPNCMJLJ$JSDVJU#SFBLFSIUNM

  39. Inspired by … ?

  40. Inspired by … ? •ΠϯεύΠΞ͞Εͨͱ͍͏ʹ͸࢓૊Έ͕͠ΐ΅͍ •τάϧͷ؅ཧʹ೉͕͋Δ •ϒϨʔΧʔΛམͱ͢৚͕݅ͨͩͷճ਺ •ࣗಈ෮ؼ͠ͳ͍ •DB ΞΫηεΛ͢ΔͷͰΦʔόʔϔου͕ੜ͡Δ

  41. ɹো֐͕ଟ͍ΜͰ͢Α…😣 ɹ90% ϛε͕ͳ͍͜ͱͷอূ͸ͦͦ͜͜Ͱ͖Δ͚Ͳ 
 ɹ100% ϛε͕ͳ͍͜ͱͷอূ͸ͱͯ΋ߴίετ ɹϦϦʔεස౓Λ্͛Δͷ͕೉ͯ͘͠…😭 ɹ͙͢ʹ੾Γ໭ͤΔΑ͏ʹͨ͠ํ͕ 
 ɹ݈શ͔΋͠Ε·ͤΜͶ

  42. Ұ఺ूத •΄͍͠ͷ͸ʮ௿ίετʹ੾Γ໭ͤΔʯ࢓૊Έ •ͱʹ͔͘ࠓ΄͍͠ ػೳ͚ͩΛ࣮૷ͯ͠ϦϦʔε

  43. ॠൃྗ •ͱΓ͋͑ͣ΄͔ͬͨ͠ •ϨϏϡʔɾௐࠪɾςετίετ͕૿େ͍ͯ͠Δঢ়گΛվળ͍ͨ͠ •ݱ৔ͷࠓ͋Δ՝୊Λࠓ͙͢ղܾ͍ͨ͠ •଍Γͳ͍஌ࣝɾٕज़͸ͨ͘͞Μ͋Δ •४උ͕Ͱ͖Δ·Ͱ଴ͭͱ͋ͬͱ͍͏ؒʹ݄೔͕ա͗Δ

  44. ॠൃྗ •MVP •Minimum Viable Product •࣮༻࠷খݶͷ੡඼ •ఏڙ͠ͳ͕Β։ൃ͢Δ

  45. ΍Βͳ͍ཧ༝ •ͦ͏͔΋͠Εͳ͍ •ͦ͏Ͱͳ͍͔΋͠Εͳ͍ •OSS ʹ͓͚ͯ͠͹୭͔ͷ໾ʹཱ͔ͭ΋͠Εͳ͍ •OSS ʹ͠ͳ͍ཧ༝͸ͳ͍ ɹgem ʹ͠ͳͯ͘΋͍͍Μ͡Όͳ͍ʁ

  46. ՝୊ʹΞϓϩʔν͢Δ •ίʔυͷ೉͠͞ͱϥΠϒϥϦͷՁ஋͸ؔ܎ͳ͍ ɹ͜Μͳ͠ΐ΅͍ίʔυΛੈʹग़͢ͳΜͯ…

  47. ·ͣ͸͍ͪ࢝͘͞ΊΔ

  48. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠