SlideShare uma empresa Scribd logo
1 de 27
Baixar para ler offline
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

Mais conteúdo relacionado

Destaque

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
 

Destaque (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
 

Último

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 

Último (20)

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 

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