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

Actor Model in Reason

Actor Model in Reason

Avatar for Yuki YAMADA

Yuki YAMADA

August 02, 2018
Tweet

More Decks by Yuki YAMADA

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. Yuki Yamada @yamadayuki I’m an Engineer @

    Wantedly I write JS, Golang, Ruby, Swift … etc. Now I focus on Frontend Engineering at Feed squad. Self Introduction
  2. ©2018 Wantedly, Inc. Actor Model Actor Model ΞΫλʔͱ͍͏ঢ়ଶΛอͪॲཧΛ͢ΔΦϒδΣΫτ ͦΕͧΕ͕ Mailbox

    ͱ͍͏ΩϡʔͱΞυϨεΛ͍࣋ͬͯΔ ΞΫλʔୡ͸ͦΕͧΕࣗ෼ͷॲཧΛߦ͍ͭͭඞཁ͕͋Ε͹ଞ ͷΞΫλʔʹϝοηʔδΛૹͬͯॲཧΛ͓ئ͍͢Δ ͦΕͧΕͷॲཧ͸ඇಉظͰ࣮ߦ͞ΕΔͨΊɺฒߦॲཧͷͨΊ ͷϩοΫ΍ಉظ͕ෆཁʹͳΔ ݴޠϨϕϧͰ͸ Erlang ϥΠϒϥϦͰ͸ Akka (JVM) ͕୅දྫ
  3. ©2018 Wantedly, Inc. 1. OCaml • Statically typed, Functional, Pattern

    Matching 2. Erlang • Actor Model, Functional, Pattern Matching 3. JavaScript • Fun, Insanely flexible Actor Model
  4. ©2018 Wantedly, Inc. Actor Model Roppongi.js #1 ͰLT͠·ͨ͠ BuckleScript ϕʔεͰܕݕࠪͰ͖Δɺ

    Facebook ۘ੡ͷAltJS BuckleScript ͸ OCaml ΛίϯύΠϧ ͯ͠ JavaScript ʹ͢Δ΍ͭ https://speakerdeck.com/yamadayuki/react-with-reason
  5. ©2018 Wantedly, Inc. Actor Model in Reason JS Ͱ Actor

    Model ncthbrt/nact , ncthbrt/reason-nact “Nact is redux but for the server.” Akka ΍ Erlang ͷΞϓϩʔνΛࢀߟʹ Node.js Ͱ΋ Actor Model ͕࣮ݱͰ͖ΔΑ͏ʹ࡞ΒΕͨϥΠϒϥϦ DENQUE (https://github.com/Salakar/denque) ͱ Promise ϕʔεͰ࣮૷͞Ε͍ͯΔ Timeout ΍ Supervision ΋αϙʔτ͞Ε͍ͯΔ JavaScript ͔Β΋ Reason/BuckleScript ͔Β΋࢖͑Δ
  6. ©2018 Wantedly, Inc. Actor Model in Reason Getting Started ѫࡰΛฦͯ͘͠ΕΔΞΫλʔͷੜ੒

    “spawnStateless” ͱ͍͏ؔ਺ͰΞΫλʔ͕ੜ੒ Ͱ͖Δ ΞΫλʔʹରͯ͠ “dispatch” ͱ͍͏ؔ਺Ͱϝο ηʔδΛૹΔ͜ͱͰɺॲཧΛ࣮ߦͤ͞Δ͜ͱ͕ Ͱ͖Δ
  7. ©2018 Wantedly, Inc. Actor Model in Reason spawnStateless “Stateless” ͱ

    suffix ͕͍͍ͭͯΔ͚ͩ͋ͬͯɺ ͜ͷؔ਺Ͱ͸ State Λ࣋ͬͨΞΫλʔΛੜ੒Ͱ ͖ͳ͍ ͦͷͨΊɺ࣮ߦͯ͠΄͍͠ॲཧ͚ͩΛ೚ͤΔ͜ ͱ͔͠Ͱ͖ͳ͍ɻ࣮ߦͨ݁͠ՌͩͬͨΓΛ࣍ʹ ࢖͍ճ͢ͱ͔͸Ͱ͖ͳ͍
  8. ©2018 Wantedly, Inc. Actor Model in Reason spawn ݟͨ໨ “Stateless”

    ͷ suffix ͕औΕ͚ͨͩ ͜ͷؔ਺Ͱ͸ State Λ࣋ͬͨΞΫλʔΛੜ੒Ͱ ͖Δ ӈͷίʔυ͸ counter ͷ࣮૷ amount Λࢦఆͯ͠૿΍͢ྔΛϝοηʔδͰૹ Δͱɺ͍·ͷঢ়ଶͷ amount ʹՃࢉ͞Εͯίϯ ιʔϧʹग़ྗ͞ΕΔɻ
  9. ©2018 Wantedly, Inc. Actor Model in Reason spawn ॲཧΛهड़͢Δؔ਺ͷୈ1Ҿ਺ʹ State

    ͕౉ͬͯ ͘ΔͷͰɺͦΕΛར༻ͯ͠ॲཧΛߦ͑Δ 12ߦ໨͸͜ͷΞΫλʔ͕ॳظԽ͞Εͨ࣌఺Ͱͷ initial state Λฦ͍ͯ͠Δ ͳͷͰ state ʹ͸ {amount: int} ͱ͍͏ํ͕౉ͬ ͯ͘Δ
  10. ©2018 Wantedly, Inc. Actor Model in Reason Communication ΞΫλʔಉ࢜Ͱ௨৴͢Δͱ͖ɺྫ͑͹ϝοηʔ δͷૹΓओ͕ඞཁͳ৔߹͕͋Δ

    ϝοηʔδΛૹΔͱ͖ʹࣗ෼ࣗ਎Λϝοηʔδ ʹՃ͑Δ͜ͱͰɺϝοηʔδΛड͚औͬͨଆͰ ΋ૹΓओΛ஌Δ͜ͱ͕Ͱ͖Δ ΞΫλʔࣗମ͸ actorRef(msgType) ͷΑ͏ͳ ܕʹͳΔͷͰɺϝοηʔδͷܕΛఆٛ͢Δ࣌ʹ ར༻͢Δ
  11. ©2018 Wantedly, Inc. Actor Model in Reason Communication pong ͔Β

    ping ʹϝοηʔδΛૹΔͱ͜Ζ 7ߦ໨Ͱ sender(ping or pong) ʹରͯ͠ ctx.self Λϝοηʔδʹ֨ೲͯ͠ૹΔΑ͏ʹͳͬ ͍ͯΔ ctx.self ͸ΞΫλʔࣗ਎ʹͳ͍ͬͯͯɺϝοηʔ δΛड͚औΔଆͰ΋ૹΓओ͕Θ͔Δ “<-<” ͱ͍͏ͷ͸ಠࣗʹఆٛ͞ΕͨΦϖϨʔλͰ dispatch ͷγϣʔτϋϯυ
  12. ©2018 Wantedly, Inc. Actor Model in Reason Supervision ΋͠ΞΫλʔ͕Ϋϥογϡͨ͠৔߹ɺඇಉظॲ ཧͷҰ࿈ͷྲྀΕΛࢭΊͳ͍ͨΊʹ΋෮׆ͤ͞Δ

    ඞཁ͕͋Δ Ϋϥογϡͨ͠ͱ͖ʹ෮׆Ͱ͖ΔΑ͏ʹ supervisor Λ༻ҙ͢Δ͜ͱͰɺࣗಈతʹΞΫ λʔΛ෮׆ͤ͞ΒΕΔ
  13. ©2018 Wantedly, Inc. 1. Actor Model Λ࢖ͬͯඇಉظॲཧΛ͍͍ײ͡ʹ 2. nact ͸

    Reason Ͱهड़Ͱ͖ΔͷͰܕͷԸܙ͕͋Δ 3. Reason ָ͍͠ Actor Model in Reason ·ͱΊ