SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
Don't Block
How to Mess Up Akka and Spray
Zach Cox - @zcox - zach.cox@banno.com
iascala - Feb 2014
Horse ebooks
@Horse_ebooks

Don t block
1:52 PM - 4 Aug 2013
467 RETWEETS 320 FAVORITES

Follow
Purpose
Scala provides great concurrency tools
Very easy to mess up
Demonstrate how blocking can prevent processing
Provide solutions
Things We Will Block
Threads (Java)
Futures (Scala)
Actors (Akka)
Spray
Spoiler
All about blocking threads
What is Blocking?
Code that takes a long time to run
Network I/O
HTTP requests
Database access
File I/O
Really heavy computation
Nothing after the blocking function can run on this thread until
it is done
Java: R n a l , E e u o
unbe xctr
tatRnal {
ri unbe
dfrn) Ui
e u(: nt
}
tatEeuo {
ri xctr
dfeeuecmad Rnal) Ui
e xct(omn: unbe: nt
}
TraPoEeuo
hedolxctr
casTraPoEeuo etnsEeuo {
ls hedolxctr xed xctr
vlpo:StTra]=Stepy
a ol e[hed
e.mt
vltss BoknQeeRnal]=??
a ak: lciguu[unbe
?
dfeeuecmad Rnal) Ui ={
e xct(omn: unbe: nt
/rncmado nwtra,o qeei,o rjc i,dpnigo stig..
/u omn n e hed r uu t r eet t eedn n etns.
}
}
How to block T r a P o E e u o
hedolxctr
Execute tasks that block in run() method
All threads in pool get blocked
Tasks are queued before execution (until queue is full)
How it is supposed to work...
Need to be aware of the blocking though
FrJiPo
okonol
E e u o S r i eimplementation
xctrevc
saacnurn.okon
cl.ocretfrji
j v . t l c n u r n (Java7)
aaui.ocret
Sub-dividing tasks, work queue, worker thread pool, work stealing, ...
https://www.google.com/search?q=java+fork+join
How to block F r J i P o
okonol
Same as T r a P o E e u o
hedolxctr
Solutions for Blocked E e u o
xctr
vleeuo =EeuosnwieTraPo(oehed)
a xctr
xctr.eFxdhedolmrTras

vleeuo1=EeuosnwieTraPo(olSz)
a xctr
xctr.eFxdhedolpo1ie
vleeuo2=EeuosnwieTraPo(olSz)
a xctr
xctr.eFxdhedolpo2ie
Java: C l a l , F t r ,
albe uue
EeuoSrie
xctrevc
tatClal[]{
ri albeV
dfcl(:V
e al)
}
tatFtr[]{
ri uueV
dfiDn(:Boen
e soe) ola
dfgt) V
e e(:
}
tatEeuoSrieetnsEeuo {
ri xctrevc xed xctr
dfsbi(ak Rnal) Ftr[]
e umtts: unbe: uue_
dfsbi[]ts:Clal[]:Ftr[]
e umtT(ak albeT) uueT
}
Ftr[]
uueT
Monad that eventually contains either:
A value of type T(success)
A T r w b e(failure)
hoal
F t r [ ]is the read-side; P o i e T is the write-side
uueT
rms[]
Value is computed and placed into promise/future on some other
thread (usually)
/TD cmeln eapeo Ftr..
/OO oplig xml f uue.
Eeuinotx
xctoCnet
Runs code that asynchronously completes futures
Scala version of E e u o /E e u o S r i e
xctr xctrevc
Implementations usually wrap one
E e u o => E e u i n o t x
xctr
xctoCnet
E e u o S r i e=> E e u i n o t x
xctrevc
xctoCnet
F t r . p l runs body function using E e u i n o t x
uueapy
xctoCnet
Wraps the body function in a R n a l
unbe
Executes that R n a l on an E e u i n o t x
unbe
xctoCnet
That R n a l completes a P o i e
unbe
rms
Eeuinotx.lbl
xctoCnetgoa
Tries to use F r J i P o
okonol
Falls back to T r a P o E e u o
hedolxctr
How to block F t r /E e u i n o t x
uue xctoCnet
E e u i n o t x just wraps an E e u o /E e u o S r i e
xctoCnet
xctr xctrevc
E e u i n o t x . l b lusually wraps either a
xctoCnetgoa
F r J i P o or a T r a P o E e u o
okonol
hedolxctr
We already know how to block those
tldr function passed to F t r . p l blocks the underlying thread,
uueapy
exhaust the pool
Solutions for blocked
F t r /E e u i n o t x
uue xctoCnet
jv ..
aa .
-saacnurn.otx.iTras8
Dcl.ocretcnetmnhed=
-saacnurn.otx.uTras1 
Dcl.ocretcnetnmhed=6
-saacnurn.otx.aTras2 
Dcl.ocretcnetmxhed=4
..
.

ipii vlc=Eeuinotx.rmxctrEeuosnwieTraPo(2)
mlct a
xctoCnetfoEeuo(xctr.eFxdhedol13)

ipii vldfutotx =Eeuinotx.lbl
mlct a ealCnet
xctoCnetgoa
vldtbsCnet=Eeuinotx.rmxctrnl)
a aaaeotx
xctoCnetfoEeuo(ul
Ftr(dfutpoesn"
uue"eal rcsig)
Ftr(dtbs oeain"(aaaeotx)
uue"aaae prtos)dtbsCnet
Actors and Dispatchers
atr!mg
co
s
m gplaced in actor''s M i b xqueue
s
alo
M i b xis a R n a l
alo
unbe
M i b xexecuted on dispatcher''s E e u o S r i e
alo
xctrevc
By default, all actors use the same default dispatcher
How to Block Actors
Remember how to block E e u o ? Do that.
xctr
Block in A t r r c i e
co.eev
Enough blocked actors will exhaust the dispatcher''s thread pool
Solutions for Blocked Actors
bokn2dsace {
lcig-ipthr
tp =Dsace
ye
ipthr
eeuo ="okji-xctr
xctr
fr-oneeuo"
}
vlbokn =sse.coO(rp[lcigco]
a lcig
ytmatrfPosBoknAtr
.ihotrFoCni()
wtRue(rmofg)
.ihipthr"lcig-ipthr) "lcig"
wtDsace(bokn2dsace", bokn2)

m-ipthr{
ydsace
tp =Dsace
ye
ipthr
eeuo ="okji-xctr
xctr
fr-oneeuo"
fr-oneeuo {
okji-xctr
prleimfco =1.
aalls-atr
00
prleimmx=10
aalls-a
0
}
}
Fun Fact
M s a e i p t h ris an E e u i n o t x
esgDsace
xctoCnet
vljbCnet=sse.ipthr.okp"dcdsace"
a dcotx
ytmdsaceslou(jb-ipthr)
Ftr(sTeaaae(dcotx)
uueuehDtbs)jbCnet
Spray
spray-io/akka-io: Java NIO + Actors
spray-can: HTTP server & client built on spray-io
spray-routing: HTTP request/response DSL
How to Block Spray
Built on actors
We know how to block those
S m l R u i g p uses a single actor to route all requests (!!!)
ipeotnAp
That actor synchronously calls runRoute - easily blocked!
That actor uses default Akka dispatcher - easily blocked!
Example code
Solutions for Blocking Spray
Do not call blocking functions directly in routes
Instead detach to F t r or A t r
uue
co
Spray can complete a response using a F t r
uue
Use separate dispatchers
Give Spray its own dispatcher(s)
Give your blocking code its own dispatcher(s)
Java NIO and spray-client
Blocking I/O: One thread per socket
Non-blocking I/O: One thread, many sockets
No network I/O (web service client, database, etc) libraries use it!
Except spray-client...
Start writing Scala web service clients using spray-can!
Other protocols (TCP, SMTP, XMPP, various DBs, etc) can use
akka-io
Banno is Hiring
Scala Developers!
zach.cox@banno.com

Weitere ähnliche Inhalte

Andere mochten auch

The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)Eugene Yokota
 
Improvements to Apache HBase and Its Applications in Alibaba Search
Improvements to Apache HBase and Its Applications in Alibaba Search Improvements to Apache HBase and Its Applications in Alibaba Search
Improvements to Apache HBase and Its Applications in Alibaba Search HBaseCon
 
Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster OpenCredo
 
I Heart Log: Real-time Data and Apache Kafka
I Heart Log: Real-time Data and Apache KafkaI Heart Log: Real-time Data and Apache Kafka
I Heart Log: Real-time Data and Apache KafkaJay Kreps
 
Akka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutesAkka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutesKonrad Malawski
 

Andere mochten auch (6)

The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
 
Improvements to Apache HBase and Its Applications in Alibaba Search
Improvements to Apache HBase and Its Applications in Alibaba Search Improvements to Apache HBase and Its Applications in Alibaba Search
Improvements to Apache HBase and Its Applications in Alibaba Search
 
Vert.x vs akka
Vert.x vs akkaVert.x vs akka
Vert.x vs akka
 
Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster
 
I Heart Log: Real-time Data and Apache Kafka
I Heart Log: Real-time Data and Apache KafkaI Heart Log: Real-time Data and Apache Kafka
I Heart Log: Real-time Data and Apache Kafka
 
Akka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutesAkka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutes
 

Kürzlich hochgeladen

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Kürzlich hochgeladen (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Don't Block - How to Mess Up Akka and Spray

  • 1. Don't Block How to Mess Up Akka and Spray Zach Cox - @zcox - zach.cox@banno.com iascala - Feb 2014
  • 2. Horse ebooks @Horse_ebooks Don t block 1:52 PM - 4 Aug 2013 467 RETWEETS 320 FAVORITES Follow
  • 3. Purpose Scala provides great concurrency tools Very easy to mess up Demonstrate how blocking can prevent processing Provide solutions
  • 4. Things We Will Block Threads (Java) Futures (Scala) Actors (Akka) Spray
  • 6. What is Blocking? Code that takes a long time to run Network I/O HTTP requests Database access File I/O Really heavy computation Nothing after the blocking function can run on this thread until it is done
  • 7. Java: R n a l , E e u o unbe xctr tatRnal { ri unbe dfrn) Ui e u(: nt } tatEeuo { ri xctr dfeeuecmad Rnal) Ui e xct(omn: unbe: nt }
  • 8. TraPoEeuo hedolxctr casTraPoEeuo etnsEeuo { ls hedolxctr xed xctr vlpo:StTra]=Stepy a ol e[hed e.mt vltss BoknQeeRnal]=?? a ak: lciguu[unbe ? dfeeuecmad Rnal) Ui ={ e xct(omn: unbe: nt /rncmado nwtra,o qeei,o rjc i,dpnigo stig.. /u omn n e hed r uu t r eet t eedn n etns. } }
  • 9. How to block T r a P o E e u o hedolxctr Execute tasks that block in run() method All threads in pool get blocked Tasks are queued before execution (until queue is full) How it is supposed to work... Need to be aware of the blocking though
  • 10. FrJiPo okonol E e u o S r i eimplementation xctrevc saacnurn.okon cl.ocretfrji j v . t l c n u r n (Java7) aaui.ocret Sub-dividing tasks, work queue, worker thread pool, work stealing, ... https://www.google.com/search?q=java+fork+join
  • 11. How to block F r J i P o okonol Same as T r a P o E e u o hedolxctr
  • 12. Solutions for Blocked E e u o xctr vleeuo =EeuosnwieTraPo(oehed) a xctr xctr.eFxdhedolmrTras vleeuo1=EeuosnwieTraPo(olSz) a xctr xctr.eFxdhedolpo1ie vleeuo2=EeuosnwieTraPo(olSz) a xctr xctr.eFxdhedolpo2ie
  • 13. Java: C l a l , F t r , albe uue EeuoSrie xctrevc tatClal[]{ ri albeV dfcl(:V e al) } tatFtr[]{ ri uueV dfiDn(:Boen e soe) ola dfgt) V e e(: } tatEeuoSrieetnsEeuo { ri xctrevc xed xctr dfsbi(ak Rnal) Ftr[] e umtts: unbe: uue_ dfsbi[]ts:Clal[]:Ftr[] e umtT(ak albeT) uueT }
  • 14.
  • 15. Ftr[] uueT Monad that eventually contains either: A value of type T(success) A T r w b e(failure) hoal F t r [ ]is the read-side; P o i e T is the write-side uueT rms[] Value is computed and placed into promise/future on some other thread (usually) /TD cmeln eapeo Ftr.. /OO oplig xml f uue.
  • 16. Eeuinotx xctoCnet Runs code that asynchronously completes futures Scala version of E e u o /E e u o S r i e xctr xctrevc Implementations usually wrap one E e u o => E e u i n o t x xctr xctoCnet E e u o S r i e=> E e u i n o t x xctrevc xctoCnet F t r . p l runs body function using E e u i n o t x uueapy xctoCnet Wraps the body function in a R n a l unbe Executes that R n a l on an E e u i n o t x unbe xctoCnet That R n a l completes a P o i e unbe rms Eeuinotx.lbl xctoCnetgoa Tries to use F r J i P o okonol Falls back to T r a P o E e u o hedolxctr
  • 17. How to block F t r /E e u i n o t x uue xctoCnet E e u i n o t x just wraps an E e u o /E e u o S r i e xctoCnet xctr xctrevc E e u i n o t x . l b lusually wraps either a xctoCnetgoa F r J i P o or a T r a P o E e u o okonol hedolxctr We already know how to block those tldr function passed to F t r . p l blocks the underlying thread, uueapy exhaust the pool
  • 18. Solutions for blocked F t r /E e u i n o t x uue xctoCnet jv .. aa . -saacnurn.otx.iTras8 Dcl.ocretcnetmnhed= -saacnurn.otx.uTras1 Dcl.ocretcnetnmhed=6 -saacnurn.otx.aTras2 Dcl.ocretcnetmxhed=4 .. . ipii vlc=Eeuinotx.rmxctrEeuosnwieTraPo(2) mlct a xctoCnetfoEeuo(xctr.eFxdhedol13) ipii vldfutotx =Eeuinotx.lbl mlct a ealCnet xctoCnetgoa vldtbsCnet=Eeuinotx.rmxctrnl) a aaaeotx xctoCnetfoEeuo(ul Ftr(dfutpoesn" uue"eal rcsig) Ftr(dtbs oeain"(aaaeotx) uue"aaae prtos)dtbsCnet
  • 19. Actors and Dispatchers atr!mg co s m gplaced in actor''s M i b xqueue s alo M i b xis a R n a l alo unbe M i b xexecuted on dispatcher''s E e u o S r i e alo xctrevc By default, all actors use the same default dispatcher
  • 20. How to Block Actors Remember how to block E e u o ? Do that. xctr Block in A t r r c i e co.eev Enough blocked actors will exhaust the dispatcher''s thread pool
  • 21. Solutions for Blocked Actors bokn2dsace { lcig-ipthr tp =Dsace ye ipthr eeuo ="okji-xctr xctr fr-oneeuo" } vlbokn =sse.coO(rp[lcigco] a lcig ytmatrfPosBoknAtr .ihotrFoCni() wtRue(rmofg) .ihipthr"lcig-ipthr) "lcig" wtDsace(bokn2dsace", bokn2) m-ipthr{ ydsace tp =Dsace ye ipthr eeuo ="okji-xctr xctr fr-oneeuo" fr-oneeuo { okji-xctr prleimfco =1. aalls-atr 00 prleimmx=10 aalls-a 0 } }
  • 22. Fun Fact M s a e i p t h ris an E e u i n o t x esgDsace xctoCnet vljbCnet=sse.ipthr.okp"dcdsace" a dcotx ytmdsaceslou(jb-ipthr) Ftr(sTeaaae(dcotx) uueuehDtbs)jbCnet
  • 23. Spray spray-io/akka-io: Java NIO + Actors spray-can: HTTP server & client built on spray-io spray-routing: HTTP request/response DSL
  • 24. How to Block Spray Built on actors We know how to block those S m l R u i g p uses a single actor to route all requests (!!!) ipeotnAp That actor synchronously calls runRoute - easily blocked! That actor uses default Akka dispatcher - easily blocked! Example code
  • 25. Solutions for Blocking Spray Do not call blocking functions directly in routes Instead detach to F t r or A t r uue co Spray can complete a response using a F t r uue Use separate dispatchers Give Spray its own dispatcher(s) Give your blocking code its own dispatcher(s)
  • 26. Java NIO and spray-client Blocking I/O: One thread per socket Non-blocking I/O: One thread, many sockets No network I/O (web service client, database, etc) libraries use it! Except spray-client... Start writing Scala web service clients using spray-can! Other protocols (TCP, SMTP, XMPP, various DBs, etc) can use akka-io
  • 27. Banno is Hiring Scala Developers! zach.cox@banno.com