|
| 1 | +require 'thread' |
| 2 | + |
| 3 | +require 'concurrent/actor_ref' |
| 4 | +require 'concurrent/event' |
| 5 | +require 'concurrent/ivar' |
| 6 | + |
| 7 | +module Concurrent |
| 8 | + |
| 9 | + class SimpleActorRef |
| 10 | + include ActorRef |
| 11 | + |
| 12 | + def initialize(actor, opts = {}) |
| 13 | + @actor = actor |
| 14 | + @mutex = Mutex.new |
| 15 | + @queue = Queue.new |
| 16 | + @thread = nil |
| 17 | + @stop_event = Event.new |
| 18 | + @abort_on_exception = opts.fetch(:abort_on_exception, true) |
| 19 | + @reset_on_error = opts.fetch(:reset_on_error, true) |
| 20 | + @exception_class = opts.fetch(:rescue_exception, false) ? Exception : StandardError |
| 21 | + @observers = CopyOnNotifyObserverSet.new |
| 22 | + end |
| 23 | + |
| 24 | + def running? |
| 25 | + ! @stop_event.set? |
| 26 | + end |
| 27 | + |
| 28 | + def shutdown? |
| 29 | + @stop_event.set? |
| 30 | + end |
| 31 | + |
| 32 | + def post(*msg, &block) |
| 33 | + raise ArgumentError.new('message cannot be empty') if msg.empty? |
| 34 | + @mutex.synchronize do |
| 35 | + supervise unless shutdown? |
| 36 | + end |
| 37 | + ivar = IVar.new |
| 38 | + @queue.push(Message.new(msg, ivar, block)) |
| 39 | + ivar |
| 40 | + end |
| 41 | + |
| 42 | + def post!(seconds, *msg) |
| 43 | + raise Concurrent::TimeoutError if seconds == 0 |
| 44 | + ivar = self.post(*msg) |
| 45 | + ivar.value(seconds) |
| 46 | + if ivar.incomplete? |
| 47 | + raise Concurrent::TimeoutError |
| 48 | + elsif ivar.reason |
| 49 | + raise ivar.reason |
| 50 | + end |
| 51 | + ivar.value |
| 52 | + end |
| 53 | + |
| 54 | + def shutdown |
| 55 | + @mutex.synchronize do |
| 56 | + return if shutdown? |
| 57 | + if @thread && @thread.alive? |
| 58 | + @thread.kill |
| 59 | + @actor.on_shutdown |
| 60 | + end |
| 61 | + @stop_event.set |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def join(timeout = nil) |
| 66 | + @stop_event.wait(timeout) |
| 67 | + end |
| 68 | + |
| 69 | + private |
| 70 | + |
| 71 | + Message = Struct.new(:payload, :ivar, :callback) |
| 72 | + |
| 73 | + def supervise |
| 74 | + if @thread.nil? |
| 75 | + @actor.on_start |
| 76 | + @thread = new_worker_thread |
| 77 | + elsif ! @thread.alive? |
| 78 | + @actor.on_reset |
| 79 | + @thread = new_worker_thread |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def new_worker_thread |
| 84 | + Thread.new do |
| 85 | + Thread.current.abort_on_exception = @abort_on_exception |
| 86 | + run_message_loop |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + def run_message_loop |
| 91 | + loop do |
| 92 | + message = @queue.pop |
| 93 | + result = ex = nil |
| 94 | + |
| 95 | + begin |
| 96 | + result = @actor.receive(*message.payload) |
| 97 | + rescue @exception_class => ex |
| 98 | + @actor.on_reset if @reset_on_error |
| 99 | + ensure |
| 100 | + now = Time.now |
| 101 | + message.ivar.complete(ex.nil?, result, ex) |
| 102 | + message.callback.call(now, result, ex) if message.callback |
| 103 | + observers.notify_observers(now, message.payload, result, ex) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments