Skip to content

Commit

Permalink
handle messages on behalf of receiving actor
Browse files Browse the repository at this point in the history
  • Loading branch information
niamster committed Feb 23, 2015
1 parent 919e116 commit 99c45ab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
1 change: 0 additions & 1 deletion lib/dcell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def setup(options = {})
addr: "tcp://127.0.0.1:*",
heartbeat_rate: 5,
heartbeat_timeout: 10,
async_pool_size: 50,
id: nil,
}.merge(options)

Expand Down
17 changes: 17 additions & 0 deletions lib/dcell/celluloid_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ def to_msgpack(pk=nil)
end
end

module InstanceMethods
def ____dcell_dispatch(message)
info = message.message
begin
value = nil
if info[:block]
send(info[:meth], *info[:args]) {|v| value = v}
else
value = send(info[:meth], *info[:args])
end
message.success value
rescue => e
message.exception e
end
end
end

module ClassMethods
def supervise_as(name, *args, &block)
DCell.add_local_actor name
Expand Down
34 changes: 12 additions & 22 deletions lib/dcell/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,22 @@ def initialize(sender, message)
@sender, @message = sender, message
end

def __dispatch(actor)
value = nil
mailbox = actor.mailbox
if @message[:async]
if mailbox.size > DCell.async_pool_size
Logger.info "Mailbox of actor '#{@message[:actor]}' is full, syncing"
Celluloid::Actor::call mailbox, @message[:meth], *@message[:args] {|v| value = v}
else
Celluloid::Actor::async mailbox, @message[:meth], *@message[:args]
end
return nil
end
if @message[:block]
Celluloid::Actor::call mailbox, @message[:meth], *@message[:args] {|v| value = v}
else
value = Celluloid::Actor::call mailbox, @message[:meth], *@message[:args]
end
SuccessResponse.new(@id, @sender[:address], value)
rescue => e
ErrorResponse.new(@id, @sender[:address], {class: e.class.name, msg: e.to_s})
def success(value)
respond SuccessResponse.new(@id, @sender[:address], value)
end

def exception(e)
respond ErrorResponse.new(@id, @sender[:address], {class: e.class.name, msg: e.to_s})
end

def dispatch
actor = DCell.get_local_actor @message[:actor].to_sym
rsp = __dispatch actor
respond rsp if rsp
begin
mailbox = actor.mailbox
Celluloid::Actor::async mailbox, :____dcell_dispatch, self
rescue => e
respond ErrorResponse.new(@id, @sender[:address], {class: e.class.name, msg: e.to_s})
end
end

def to_msgpack(pk=nil)
Expand Down

0 comments on commit 99c45ab

Please sign in to comment.