Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
つまらないシステム(1) Scala の Future と ExecutionContext
Search
mashijp
November 13, 2015
Programming
2.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
つまらないシステム(1) Scala の Future と ExecutionContext
Scalaで詰まらないシステムを作るために覚えていてほしいこと。
mashijp
November 13, 2015
More Decks by mashijp
See All by mashijp
幅広い環境でSSL対応するために知っておくべきこと
mashijp
7
3.2k
SSL勉強会 - 「クラウドを支えるこれからの暗号技術」を読んで
mashijp
0
500
つまらないシステム(2) - Scala 書きやすすぎるFutureの罠
mashijp
0
79
Other Decks in Programming
See All in Programming
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.5k
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
34
16k
Simple Storage Service(S3) is not simple
iwatsukayura
0
220
Workers Cache を知る
syumai
0
350
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.5k
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
460
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
400
Jetpack Compose Mechanisms
skydoves
2
560
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5k
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
21
21k
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
200
dotNET_vulnclicker_intro.pdf
deka_morita
0
130
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
350
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
My Coaching Mixtape
mlcsv
0
340
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
390
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
350
How to train your dragon (web standard)
notwaldorf
96
6.9k
Mobile First: as difficult as doing things right
swwweet
225
10k
Designing Powerful Visuals for Engaging Learning
tmiket
1
600
We Have a Design System, Now What?
morganepeng
55
8.4k
Transcript
ͭ·Βͳ͍γεςϜ(1) - FutureͱExecutionContext - mashi
ఆͯ͠Δಡऀ • ৽ଔͷΈͳ͞Μ • Scalaॻ͘ͷָ͕͘͠ͳ͖ͬͯͨ • Future͍͢͝ʂָ͍͠ʂ
Future[T] ͱ • ͍͔ͭT͕ฦͬͯ͘Δ • ͍ͭʁ – Θ͔Μͳ͍
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εϨου্ཱͪ͛͡Όͳ͍Α
ExecutionContext ͷҰൠతͳ࣮ Thread 1 Thread 2 Thread 3 Thread 4
• ༩͑ͨ Runnable Λۭ͍ͯΔεϨουͰ͍ ͍ײ͡ʹ࣮ߦ͢Δ Runnable 1 Runnable 2 Runnable 3 Runnable n ۭ͖࣍ୈ࣮ߦ͢Δ
ৗʹొ͢ΔExecutionContext • Future Λ͍͘͡Δʹৗʹ ExecutionContext ΛٻΊΒΕΔ • ීஈॻ͍͍ͯΔίʔυ val dbData:
Future[A] = getFromDB() val result: Future[B] = dbData.map({ e => convertToB(e) })
ৗʹొ͢ΔExecutionContext • ීஈԿؾͳ͘ॻ͍͍ͯΔίʔυ࣮ implicit ͰExecutionContextΛ͍ͯ͠Δ • mapʹͨؔ͠Λ࣮ߦ͢Δ ExecutionContext Λ҉తʹࢦఆ͍ͯ͠Δ val
dbData: Future[A] = getFromDB() val result: Future[B] = dbData.map({ e => convertToB(e) })(ec: ExecutionContext)
ExecutionContext ༻్ผʹఆٛ͢͠ • ྫ) memcached ͱ MySQL Ͱ͚Δ • ྫ)
MySQL ͷσʔλϕʔε1 ͱ 2 Ͱ͚Δ • ྫ) MySQL ͷ master ͱ slaveͰ͚Δ • ͳ͔ͥʁ
ExecutionContext ٧·Δ • ϒϩοΩϯά͢ΔͷΛ͏ͱ٧·Δ – ྫ͑ MySQL ͷૢ࡞٧·Δ ˞͜͜Ͱলུ͢Δ͕ɺscala-redis-nb ͷΑ͏ͳϊϯϒϩοΩϯάͳ࣮ Λ͏ͱجຊతʹ٧·Βͳ͍
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͔Β ͷԠͪ…
ExecutionContextΛ༻్ผʹ͚ͨ߹ • ͋ΔՕॴ͕٧·ͬͯଞʹӨڹ͠ͳ͘ͳΔ Thread 1 Thread 2 MySQLૢ࡞ MySQLૢ࡞ MySQLૢ࡞
memcached ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ ͦͷଞͷ ૢ࡞ Thread 1 Thread 2 Thread 1 Thread 2 ͬͪ͜ʹӨڹ͠ͳ͍ MySQL͚͕ͩ٧·Δ
·ͱΊ • ීஈؾʹ͍ͯ͠ͳͯ͘implicitͰ҉తʹ ExecutionContextΛ͍ͯ͠Δ͜ͱ͕͋Δ – 'VUVSFͷNBQqBU.BQΛ͢Δͱඞͣͯ͠ ͍Δ • map flatMap
Λ͢Δͱ͖ʹࠓ͜͜Ͱ͍ͯ͠ ΔExecutionContextΛ٧·ΒͤΔڪΕ͕ͳ͍͔Λ ৗʹҙࣝ͢͠ – ࣮ɾϨϏϡʔ࣌ʹΑ͘ݟ·͠ΐ͏ • ͪΌΜͱ࣮͢Εྫ͑ʮmemcached ͕յ Εͯmemcached ʹؔͳ͍ͱ͜ΖʹҰ Өڹ͕Ͱͳ͍ʯΑ͏ͳϑΣʔϧιϑτͳγε ςϜΛ࡞Γ্͛ΒΕΔ