Prototype two workers: blocking and multiplexing

This commit is contained in:
Thibault Jouan 2015-04-17 21:25:00 +00:00
parent 0164b52465
commit 0176ab0010
5 changed files with 89 additions and 0 deletions

View File

@ -12,6 +12,10 @@ require 'uh/wm/logger_formatter'
require 'uh/wm/manager'
require 'uh/wm/run_control'
require 'uh/wm/runner'
require 'uh/wm/workers'
require 'uh/wm/workers/base'
require 'uh/wm/workers/blocking'
require 'uh/wm/workers/mux'
module Uh
module WM

21
lib/uh/wm/workers.rb Normal file
View File

@ -0,0 +1,21 @@
module Uh
module WM
module Workers
FACTORIES = {
block: ->(options) { Blocking.new(options) },
mux: ->(options) { Mux.new(options) }
}.freeze
class << self
def types
FACTORIES.keys
end
def build type, **options
(FACTORIES[type] or fail ArgumentError, "unknown worker: `#{type}'")
.call options
end
end
end
end
end

31
lib/uh/wm/workers/base.rb Normal file
View File

@ -0,0 +1,31 @@
module Uh
module WM
module Workers
class Base
def initialize **options
@ios = []
end
def watch io
@ios << io
end
def before_wait &block
if block_given? then @before_wait = block else @before_wait end
end
def on_timeout &block
if block_given? then @on_timeout = block else @on_timeout end
end
def on_read &block
if block_given? then @on_read = block else @on_read end
end
def on_read_next &block
if block_given? then @on_read_next = block else @on_read_next end
end
end
end
end
end

View File

@ -0,0 +1,15 @@
module Uh
module WM
module Workers
class Blocking < Base
def work_events
#until yield
# @on_events_read_bang.call
#end
#@on_events_read_bang.call until yield
@on_read_next.call
end
end
end
end
end

18
lib/uh/wm/workers/mux.rb Normal file
View File

@ -0,0 +1,18 @@
module Uh
module WM
module Workers
class Mux < Base
def initialize timeout: 1
super
@timeout = timeout
end
def work_events
@before_wait.call if @before_wait
if res = select(@ios, [], [], @timeout) then @on_read.call res
else @on_timeout.call if @on_timeout end
end
end
end
end
end