rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 64108a9ef5484c3f024aef82537f2c9f8e777253 1004 bytes (raw)
$ git show v1.0.0pre1:lib/rainbows/fiber/rev/methods.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
# -*- encoding: binary -*-
# :enddoc:
module Rainbows::Fiber::Rev::Methods
  class Watcher < Rev::IOWatcher
    def initialize(fio, flag)
      @f = fio.f || Fiber.current
      super(fio, flag)
      attach(Rev::Loop.default)
    end

    def on_readable
      @f.resume
    end

    alias on_writable on_readable
  end

  def initialize(*args)
    @f = Fiber.current
    super(*args)
    @r = @w = false
  end

  def close
    @w.detach if @w
    @r.detach if @r
    @r = @w = false
    super
  end

  def wait_writable
    @w ||= Watcher.new(self, :w)
    @w.enable unless @w.enabled?
    Fiber.yield
    @w.disable
  end

  def wait_readable
    @r ||= Watcher.new(self, :r)
    @r.enable unless @r.enabled?
    KATO << @f
    Fiber.yield
    @r.disable
  end
end

[
  Rainbows::Fiber::IO,
  Rainbows::Client,
  # the next two trigger autoload, ugh, oh well...
  Rainbows::Fiber::IO::Socket,
  Rainbows::Fiber::IO::Pipe
].each do |klass|
  klass.__send__(:include, Rainbows::Fiber::Rev::Methods)
end

git clone https://yhbt.net/rainbows.git