SlideShare a Scribd company logo
1 of 25
An Actor Model in Go
Bryan Boreham, Weaveworks
@bboreham
According to Wikipedia…
The actor model in computer science is a
mathematical model of concurrent computation that
treats "actors" as the universal primitives of
concurrent computation.
Actors’ Advocacy
• Good for highly concurrent systems
• Easier to write
• Easier to reason about
This has been a learning
experience
Some predictions
• I’ll get to those later
My life as a Gopher
ConnectionMaker
Weave Net ConnectionMaker
ConnectionMaker
Weave Net ConnectionMaker
ConnectionMaker
Weave Net ConnectionMaker
ConnectionMaker Properties
• Async between callers and actions
• Sync during connection attempts
• Timer-driven events
Actor
Actors
Actor Outline in Go
• Actor → struct
+ goroutine
• Mailbox → chan
func StartActor() *Actor {
ch := make(chan action, size)
actor := &Actor{ ... }
go actor.actorLoop(ch)
return actor
}
func (actor *Actor) DoSomething() {
actor.actionChan <- something
}
func (actor *Actor) actorLoop(
ch <-chan action) {
for {
action := <-ch
// deal with action
}
}
🤔
🤔
ConnectionMaker in Go
type action func()
func (cm *ConnectionMaker) Add(address string) {
cm.actionChan <- func() {
cm.addConnection(address)
}
}
ConnectionMaker Loop
func (cm *ConnectionMaker) actorLoop(ch <-chan action) {
timer := time.NewTimer(duration)
for {
select {
case action := <-ch:
action()
case <-timer.C:
retryConnections()
}
}
}
Actions with return values
func (cm *ConnectionMaker) String() string {
resultChan := make(chan string, 0)
cm.actionChan <- func() {
resultChan <- cm.status()
}
return <-resultChan
}
What size should the chan be?
• 0
• 1
• N
• ∞
Non-blocking send
func (actor *SomeActor) TryTo(something) {
select {
case actor.actionChan <- func() {
// do the thing
}:
default:
// chan is full; throw it away
}
}
Back to the theory
• An actor can designate the behaviour to be used for the
next message it receives.
• Messages are sent to actors at addresses
• Compare CSP: processes are anonymous and
channels are named
• We use a pointer to the actor as its address
• Unbounded nondeterminism
In Practice
Pro:
• No lock/unlock
• Simple model
• Identifies goroutines
Con:
• Debugging/testing
• Single reader/writer
• No framework!
Deadlock, Feb 15 2015
localPeer
.queryLoop
connection
.queryLoop
handle
AddConnection
connection.
sendGossip
Message
handle
SetEstablished
localPeer.
ConnEstablished
chan
chan
Predictions
• You’ll say “just use a mutex”
• You’ll say “that’s what I’m doing anyway”
Summary
• “Do not communicate by sharing memory; instead,
share memory by communicating” - Effective Go
• Go provides the components to work in an actor style
• No framework - just type in a few lines
• Departs from Actor theory in a few ways
• In use in production for two years
- http://github.com/weaveworks/mesh
Thank You
Gopher images by Renee French http://reneefrench.blogspot.com/

More Related Content

What's hot

Actor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka FundamentalsActor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka FundamentalsNgoc Dao
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5Marcus Denker
 
A gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelA gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelMykhailo Kotsur
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoMarcus Denker
 
SF Front End Developers - Ember + D3
SF Front End Developers - Ember + D3SF Front End Developers - Ember + D3
SF Front End Developers - Ember + D3Ben Lesh
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threadsmperham
 
CSP: Huh? And Components
CSP: Huh? And ComponentsCSP: Huh? And Components
CSP: Huh? And ComponentsDaniel Fagnan
 
Clojure 1.8 Direct-Linking WWH
Clojure 1.8  Direct-Linking  WWHClojure 1.8  Direct-Linking  WWH
Clojure 1.8 Direct-Linking WWHdennis zhuang
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application TestingTroy Miles
 
Ruby Concurrency & Threads
Ruby Concurrency & ThreadsRuby Concurrency & Threads
Ruby Concurrency & ThreadsAnup Nivargi
 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksMarcus Denker
 
Pharo Status ESUG 2014
Pharo Status ESUG 2014Pharo Status ESUG 2014
Pharo Status ESUG 2014Marcus Denker
 
How we migrated Zalando app to Swift3?
How we migrated Zalando app to Swift3?How we migrated Zalando app to Swift3?
How we migrated Zalando app to Swift3?Vijaya Prakash Kandel
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Jalpesh Vadgama
 
Striking a Balance With UI Tests - ConnectTech
Striking a Balance With UI Tests - ConnectTechStriking a Balance With UI Tests - ConnectTech
Striking a Balance With UI Tests - ConnectTechstable|kernel
 
Pure functions using Javascript.
Pure functions using Javascript.Pure functions using Javascript.
Pure functions using Javascript.Leon Maia
 

What's hot (19)

Actor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka FundamentalsActor-based concurrency and Akka Fundamentals
Actor-based concurrency and Akka Fundamentals
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5
 
Javascript: master this
Javascript: master thisJavascript: master this
Javascript: master this
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
A gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelA gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor model
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
SF Front End Developers - Ember + D3
SF Front End Developers - Ember + D3SF Front End Developers - Ember + D3
SF Front End Developers - Ember + D3
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threads
 
CSP: Huh? And Components
CSP: Huh? And ComponentsCSP: Huh? And Components
CSP: Huh? And Components
 
Clojure 1.8 Direct-Linking WWH
Clojure 1.8  Direct-Linking  WWHClojure 1.8  Direct-Linking  WWH
Clojure 1.8 Direct-Linking WWH
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
 
Ruby Concurrency & Threads
Ruby Concurrency & ThreadsRuby Concurrency & Threads
Ruby Concurrency & Threads
 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
 
Pharo Status ESUG 2014
Pharo Status ESUG 2014Pharo Status ESUG 2014
Pharo Status ESUG 2014
 
How we migrated Zalando app to Swift3?
How we migrated Zalando app to Swift3?How we migrated Zalando app to Swift3?
How we migrated Zalando app to Swift3?
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
 
Striking a Balance With UI Tests - ConnectTech
Striking a Balance With UI Tests - ConnectTechStriking a Balance With UI Tests - ConnectTech
Striking a Balance With UI Tests - ConnectTech
 
Pure functions using Javascript.
Pure functions using Javascript.Pure functions using Javascript.
Pure functions using Javascript.
 

Similar to An Actor Model in Go

gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxsandeshshahapur
 
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherIntro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherBlue Elephant Consulting
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and AkkaYung-Lin Ho
 
Actors: Not Just for Movies Anymore
Actors: Not Just for Movies AnymoreActors: Not Just for Movies Anymore
Actors: Not Just for Movies AnymoreVictorOps
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrencyMosky Liu
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaYardena Meymann
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdfPowerfullBoy1
 
Introduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupIntroduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupRoy Russo
 
From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017Agustin Ramos
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptxMattMarino13
 
FP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit HoleFP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit HoleChristophe Grand
 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)Thinkful
 

Similar to An Actor Model in Go (20)

gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherIntro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
Actor Model Akka Framework
Actor Model Akka FrameworkActor Model Akka Framework
Actor Model Akka Framework
 
Actors: Not Just for Movies Anymore
Actors: Not Just for Movies AnymoreActors: Not Just for Movies Anymore
Actors: Not Just for Movies Anymore
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrency
 
Mobile Application Development class 008
Mobile Application Development class 008Mobile Application Development class 008
Mobile Application Development class 008
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
Introduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupIntroduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users Group
 
From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptx
 
FP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit HoleFP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit Hole
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)
 

Recently uploaded

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...drm1699
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfWSO2
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Henry Schreiner
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletAndrea Goulet
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfICS
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14VMware Tanzu
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdftimtebeek1
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Flutter Agency
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringPrakhyath Rai
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckMarc Lester
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxNeo4j
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdfSelfMade bd
 
Rapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and InsightsRapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and Insightsrapidoform
 

Recently uploaded (20)

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Rapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and InsightsRapidoform for Modern Form Building and Insights
Rapidoform for Modern Form Building and Insights
 

An Actor Model in Go

  • 1. An Actor Model in Go Bryan Boreham, Weaveworks @bboreham
  • 2.
  • 3. According to Wikipedia… The actor model in computer science is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent computation.
  • 4. Actors’ Advocacy • Good for highly concurrent systems • Easier to write • Easier to reason about
  • 5. This has been a learning experience
  • 6. Some predictions • I’ll get to those later
  • 7. My life as a Gopher
  • 11. ConnectionMaker Properties • Async between callers and actions • Sync during connection attempts • Timer-driven events
  • 12. Actor
  • 14. Actor Outline in Go • Actor → struct + goroutine • Mailbox → chan func StartActor() *Actor { ch := make(chan action, size) actor := &Actor{ ... } go actor.actorLoop(ch) return actor } func (actor *Actor) DoSomething() { actor.actionChan <- something } func (actor *Actor) actorLoop( ch <-chan action) { for { action := <-ch // deal with action } } 🤔 🤔
  • 15. ConnectionMaker in Go type action func() func (cm *ConnectionMaker) Add(address string) { cm.actionChan <- func() { cm.addConnection(address) } }
  • 16. ConnectionMaker Loop func (cm *ConnectionMaker) actorLoop(ch <-chan action) { timer := time.NewTimer(duration) for { select { case action := <-ch: action() case <-timer.C: retryConnections() } } }
  • 17. Actions with return values func (cm *ConnectionMaker) String() string { resultChan := make(chan string, 0) cm.actionChan <- func() { resultChan <- cm.status() } return <-resultChan }
  • 18. What size should the chan be? • 0 • 1 • N • ∞
  • 19. Non-blocking send func (actor *SomeActor) TryTo(something) { select { case actor.actionChan <- func() { // do the thing }: default: // chan is full; throw it away } }
  • 20. Back to the theory • An actor can designate the behaviour to be used for the next message it receives. • Messages are sent to actors at addresses • Compare CSP: processes are anonymous and channels are named • We use a pointer to the actor as its address • Unbounded nondeterminism
  • 21. In Practice Pro: • No lock/unlock • Simple model • Identifies goroutines Con: • Debugging/testing • Single reader/writer • No framework!
  • 22. Deadlock, Feb 15 2015 localPeer .queryLoop connection .queryLoop handle AddConnection connection. sendGossip Message handle SetEstablished localPeer. ConnEstablished chan chan
  • 23. Predictions • You’ll say “just use a mutex” • You’ll say “that’s what I’m doing anyway”
  • 24. Summary • “Do not communicate by sharing memory; instead, share memory by communicating” - Effective Go • Go provides the components to work in an actor style • No framework - just type in a few lines • Departs from Actor theory in a few ways • In use in production for two years - http://github.com/weaveworks/mesh
  • 25. Thank You Gopher images by Renee French http://reneefrench.blogspot.com/

Editor's Notes

  1. Inherently concurrent
  2. No deadlocks!
  3. I am not a computer scientist
  4. Joined Weaveworks August 2014 Weave Net largely coded as Actors: - Connection, Peer, ConnectionMaker
  5. These kind of attributes make coding it using locks more complicated release lock before long-running operation re-acquire lock after event
  6. Each actor has its own “mailbox” and state. Only the actor can modify its own state. Code is executed in response to messages received. Messages are sent asynchronously.
  7. There can be many actors. Erlang, Akka, Scala
  8. Only one goroutine accesses the data in the struct Everything is passed in as messages in the chan Question: what should we use as messages? Story of how Weave Net started with enums and moved forwards
  9. Take time to explain how this works
  10. 0 synchronises callers and actions N allows data to be processed in-order, up to length of queue - two messages from the same sender will always be handled in order
  11. Can also have a more complex data structure; merge/elide values
  12. We used mix of sync and async Changing the behaviour is not something we found we needed
  13. Single thread running an action - cannot have multiple readers in parallel or multiple threads accessing different parts of the same data structure (but can create multiple actors)
  14. Actor theory is never blocking; Go channels are finite size