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

つまらないシステム(1) Scala の Future と ExecutionContext

mashijp
November 13, 2015

つまらないシステム(1) Scala の Future と ExecutionContext

Scalaで詰まらないシステムを作るために覚えていてほしいこと。

mashijp

November 13, 2015
Tweet

More Decks by mashijp

Other Decks in Programming

Transcript

  1. Future ͷ࡞Γํ •  Future.apply Λ࢖͏ def apply(f: => T)(implicit ec:

    ExecutionContext): T •  f Ͱ౉ͨؔ͠਺͸ec: ExecutionContext ʹ Α࣮ͬͯߦ͞ΕΔ – ͜ͷ f Λࠓ͔Β Runnable ͱݺͼ·͢ •  ͍࣮ͭߦ͞ΕΔͷ͔ – Θ͔Μͳ͍ (ExecutionContext ͕ܾΊΔ) ৄ͘͠͸http://mashi.hatenablog.com/entry/2014/11/24/010417 Scala ExecutionContextͬͯԿ/ Future͸εϨου্ཱͪ͛͡Όͳ͍Α
  2. ExecutionContext ͷҰൠతͳ࣮૷ Thread 1 Thread 2 Thread 3 Thread 4

    •  ༩͑ͨ Runnable Λۭ͍ͯΔεϨουͰ͍ ͍ײ͡ʹ࣮ߦ͢Δ Runnable 1 Runnable 2 Runnable 3 Runnable n ۭ͖࣍ୈ࣮ߦ͢Δ
  3. ExecutionContext ͸༻్ผʹఆٛ͢΂͠ •  ྫ) memcached ͱ MySQL Ͱ෼͚Δ •  ྫ)

    MySQL ͷσʔλϕʔε1 ͱ 2 Ͱ෼͚Δ •  ྫ) MySQL ͷ master ͱ slaveͰ෼͚Δ •  ͳ͔ͥʁ
  4. 1つのExecutionContextΛ࢖͍·Θͨ͠৔߹ •  ྫ͑͹MySQL ͱmemcached͕৐͍ͬͯΔγεςϜ •  MySQLͷԠ౴͕஗͘ͳΔͱશ෦ॏ͘ͳΔ Thread 1 Thread 2

    Thread 3 Thread 4 MySQLૢ࡞ MySQLૢ࡞ MySQLૢ࡞ 1ͭͷExecutionContext Λྲྀ༻ memcached ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ ͦͷଞͷ ૢ࡞ ؔ܎ͳ͍ૢ࡞·Ͱಉ͡Ωϡʔʹ٧·ΕΔͷͰ memcached ͷૢ࡞΋ॏ͘ͳΔ MySQL͔Β ͷԠ౴଴ͪ…
  5. ExecutionContextΛ༻్ผʹ෼͚ͨ৔߹ •  ͋ΔՕॴ͕٧·ͬͯ΋ଞʹӨڹ͠ͳ͘ͳΔ Thread 1 Thread 2 MySQLૢ࡞ MySQLૢ࡞ MySQLૢ࡞

    memcached ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ ͦͷଞͷ ૢ࡞ Thread 1 Thread 2 Thread 1 Thread 2 ͬͪ͜ʹ͸Өڹ͠ͳ͍ MySQL͚͕ͩ٧·Δ
  6. ·ͱΊ •  ීஈؾʹ͍ͯ͠ͳͯ͘΋implicitͰ҉໧తʹ ExecutionContextΛ౉͍ͯ͠Δ͜ͱ͕͋Δ –  'VUVSFͷNBQ΍qBU.BQΛ͢Δͱඞͣ౉ͯ͠ ͍Δ •  map ΍flatMap

    Λ͢Δͱ͖ʹࠓ͜͜Ͱ౉͍ͯ͠ ΔExecutionContextΛ٧·ΒͤΔڪΕ͕ͳ͍͔Λ ৗʹҙࣝ͢΂͠ –  ࣮૷ɾϨϏϡʔ࣌ʹΑ͘ݟ·͠ΐ͏ •  ͪΌΜͱ࣮૷͢Ε͹ྫ͑͹ʮmemcached ͕յ Εͯ΋memcached ʹ͸ؔ܎ͳ͍ͱ͜Ζʹ͸Ұ ੾Өڹ͕Ͱͳ͍ʯΑ͏ͳϑΣʔϧιϑτͳγε ςϜΛ࡞Γ্͛ΒΕΔ