rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 4175eb0396b00463f92f53f74b28b150876e8ea0 2187 bytes (raw)
$ git show v0.97.0:lib/rainbows/fiber/io.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
# -*- encoding: binary -*-
module Rainbows
  module Fiber

    # A partially complete IO wrapper, this exports an IO.select()-able
    # #to_io method and gives users the illusion of a synchronous
    # interface that yields away from the current Fiber whenever
    # the underlying IO object cannot read or write
    class IO < Struct.new(:to_io, :f)
      include Rainbows::ByteSlice

      # :stopdoc:
      LOCALHOST = Unicorn::HttpRequest::LOCALHOST

      # needed to write errors with
      def write_nonblock(buf)
        to_io.write_nonblock(buf)
      end

      # enough for Rainbows.addr
      def peeraddr
        to_io.respond_to?(:peeraddr) ? to_io.peeraddr : [ LOCALHOST ]
      end

      # for wrapping output response bodies
      def each(&block)
        if buf = readpartial(16384)
          yield buf
          yield buf while readpartial(16384, buf)
        end
        rescue EOFError
        self
      end

      def close
        fileno = to_io.fileno
        RD[fileno] = WR[fileno] = nil
        to_io.close unless to_io.closed?
      end

      def closed?
        to_io.closed?
      end

      def wait_readable
        fileno = to_io.fileno
        RD[fileno] = self
        ::Fiber.yield
        RD[fileno] = nil
      end

      def wait_writable
        fileno = to_io.fileno
        WR[fileno] = self
        ::Fiber.yield
        WR[fileno] = nil
      end

      def write(buf)
        begin
          (w = to_io.write_nonblock(buf)) == buf.bytesize and return
          buf = byte_slice(buf, w..-1)
        rescue Errno::EAGAIN
          wait_writable
          retry
        end while true
      end

      # used for reading headers (respecting keepalive_timeout)
      def read_timeout
        expire = nil
        begin
          to_io.read_nonblock(16384)
        rescue Errno::EAGAIN
          return if expire && expire < Time.now
          expire ||= Time.now + G.kato
          wait_readable
          retry
        end
      end

      def readpartial(length, buf = "")
        begin
          to_io.read_nonblock(length, buf)
        rescue Errno::EAGAIN
          wait_readable
          retry
        end
      end

    end
  end
end

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