Cobra Enhancement Proposal: Actors
Posted: Thu Jul 09, 2009 8:21 am
Personally I do not like lock-based concurrency because of the traps is enables to unsuspecting users--for example, locking an entire object in C#, which often leads to slower performances. The Actor model on the other hand is message based and fairly intuitive: send and receive messages. This proposal is for the addition of native actor support in Cobra.
To bring this into perspective I will use a web application example. Assuming we have an application server which sits on a machine listening on some port for requests from a web server. Whenever messages come in from the web server the application server would need to handle each request. The naive implementation would be to have an infinite loop that processes a received message then return the results for every iteration of the loop. This would work in low traffic situations but how to we make an application server take advantage of the high concurrency existing in servers like lighttpd and nginx? Yes, actors. In our example our application server will communicate with MVCHandler objects by sending a message to them as soon as they come in from the web server. The MVCHandler object can then process these messages concurrently queuing some when over-burdened.
With this approach built-in it makes for some clean, concurrent code authoring. The user never has to worry about thread management or queuing.
Reading materials:
To bring this into perspective I will use a web application example. Assuming we have an application server which sits on a machine listening on some port for requests from a web server. Whenever messages come in from the web server the application server would need to handle each request. The naive implementation would be to have an infinite loop that processes a received message then return the results for every iteration of the loop. This would work in low traffic situations but how to we make an application server take advantage of the high concurrency existing in servers like lighttpd and nginx? Yes, actors. In our example our application server will communicate with MVCHandler objects by sending a message to them as soon as they come in from the web server. The MVCHandler object can then process these messages concurrently queuing some when over-burdened.
class ApplicationServer
receive # receives a bunch of messages to send to the server in its mailbox
# send the output to the web server
var _mvcHandler ...
def start
# get our app server to start listening on a port
loop # waits for no one
# in this loop the app server sends some arbitrary message object to the actor
_mvcHandler ! msgObject
class MVCHandler
receive # controls the mailbox for this actor
# perform mvc routing, parse views, etc. etc. in a new *blocked* thread
# 'sender' is an automatic variable like 'value' in properties
sender ! renderingResult
With this approach built-in it makes for some clean, concurrent code authoring. The user never has to worry about thread management or queuing.
Reading materials: