lightio 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2da560fd469b39e6d55ee4acbe7ef0bae81602ae
4
- data.tar.gz: 665f6fc976e45793695b3daa53810096f7db88a6
3
+ metadata.gz: 2adcb4321383299d719e6faa66696d14554e0d9c
4
+ data.tar.gz: 34ca8bb7b88f2f0753264016c21f7840dd48475d
5
5
  SHA512:
6
- metadata.gz: b1dbaf889b0b2649f110786e75aba70b42a02c0b887f3c9e5aff27bc5ee8049ffaec5ca911765492ddbff05ca24e773d0dcbff6859e05b40f1e64b571f0bad8c
7
- data.tar.gz: e3bf2ff085faf5c582d6e57be7c9aff14a144f614b259720c86663d4fa71f23da895499152d4746fe5f28212984c0367520e2d0c9e06adfb8fb04aeb3461908c
6
+ metadata.gz: 3ba852354c4797ec86547105b59e8364b6e51cacee2bf72bff882ec0e9a8f8a02c967e3a505766f2c6912dd50c628dbc1d339e1352177f532df86a1969e64187
7
+ data.tar.gz: 85b37be690bf476c772ca64082264447a3d07ea9fa2593b313ddca7c37174b618ea118b6e5f3ea99fe68f8edde6827d11ab7e9f84ef01f140e2b873417551711
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lightio (0.3.0)
4
+ lightio (0.3.1)
5
5
  nio4r (~> 2.2)
6
6
 
7
7
  GEM
@@ -1,6 +1,7 @@
1
1
  # use nio4r implement event loop, inspired from eventmachine/pure_ruby implement
2
2
  require 'nio'
3
3
  require 'set'
4
+ require 'forwardable'
4
5
  module LightIO::Core
5
6
  module Backend
6
7
 
@@ -49,6 +50,9 @@ module LightIO::Core
49
50
 
50
51
  # LightIO use NIO as default event-driving backend
51
52
  class NIO
53
+ extend Forwardable
54
+ def_delegators :@selector, :closed?, :empty?, :backend, :wakeup
55
+
52
56
  def initialize
53
57
  # @selector = NIO::Selector.new
54
58
  @current_loop_time = nil
@@ -93,14 +97,12 @@ module LightIO::Core
93
97
  end
94
98
 
95
99
  def stop
96
- return unless @running
100
+ return if closed?
97
101
  @running = false
98
- raise
102
+ @selector.close
99
103
  end
100
104
 
101
- def backend
102
- @selector.backend
103
- end
105
+ alias close stop
104
106
 
105
107
  def env_backend
106
108
  key = 'LIGHTIO_BACKEND'.freeze
@@ -15,7 +15,7 @@ module LightIO::Core
15
15
  end
16
16
 
17
17
  extend Forwardable
18
- def_delegators :@backend, :run, :add_timer, :add_callback, :add_io_wait, :cancel_io_wait, :backend
18
+ def_delegators :@backend, :run, :add_timer, :add_callback, :add_io_wait, :cancel_io_wait, :backend, :close, :closed?, :stop
19
19
 
20
20
  # Wait a watcher, watcher can be a timer or socket.
21
21
  # see LightIO::Watchers module for detail
@@ -118,6 +118,10 @@ module LightIO::Library
118
118
  @io_watcher.wait_readable
119
119
  end
120
120
 
121
+ def io_watcher
122
+ @io_watcher
123
+ end
124
+
121
125
  class << self
122
126
  def open(*args)
123
127
  io = self.new(*args)
@@ -141,14 +145,18 @@ module LightIO::Library
141
145
 
142
146
  def select(read_fds, write_fds=nil, _except_fds=nil, timeout=nil)
143
147
  timer = timeout && Time.now
144
- # run once ioloop
145
- LightIO.sleep 0
148
+ read_fds ||= []
149
+ write_fds ||= []
146
150
  loop do
147
- r_fds = (read_fds || []).select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.instance_variable_get(:@io_watcher).readable?}
148
- w_fds = (write_fds || []).select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.instance_variable_get(:@io_watcher).writable?}
151
+ # clear io watcher status
152
+ read_fds.each {|fd| fd.send(:io_watcher).clear_status}
153
+ write_fds.each {|fd| fd.send(:io_watcher).clear_status}
154
+ # run ioloop once
155
+ LightIO.sleep 0
156
+ r_fds = read_fds.select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.send(:io_watcher).readable?}
157
+ w_fds = write_fds.select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.send(:io_watcher).writable?}
149
158
  e_fds = []
150
159
  if r_fds.empty? && w_fds.empty?
151
- LightIO.sleep 0
152
160
  if timeout && Time.now - timer > timeout
153
161
  return nil
154
162
  end
@@ -1,3 +1,3 @@
1
1
  module LightIO
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -23,6 +23,8 @@ module LightIO::Watchers
23
23
  @waiting = false
24
24
  ObjectSpace.define_finalizer(self, self.class.finalizer(@monitor))
25
25
  @error = nil
26
+ # maintain socket status, see https://github.com/socketry/lightio/issues/1
27
+ @readiness = nil
26
28
  end
27
29
 
28
30
  # NIO::Monitor
@@ -41,18 +43,26 @@ module LightIO::Watchers
41
43
  extend Forwardable
42
44
  def_delegators :monitor, :interests, :interests=, :closed?
43
45
 
46
+ # this method return previous IO.select status
47
+ # should avoid to directly use
44
48
  def readable?
45
49
  check_monitor_read
46
- monitor.readable?
50
+ @readiness == :r || @readiness == :rw
47
51
  end
48
52
 
53
+ # this method return previous IO.select status
54
+ # should avoid to directly use
49
55
  def writable?
50
56
  check_monitor_write
51
- monitor.writable?
57
+ @readiness == :w || @readiness == :rw
52
58
  end
53
59
 
54
60
  alias :writeable? :writable?
55
61
 
62
+ def clear_status
63
+ @readiness = nil
64
+ end
65
+
56
66
  # Blocking until io is readable
57
67
  # @param [Numeric] timeout return nil after timeout seconds, otherwise return self
58
68
  # @return [LightIO::Watchers::IO, nil]
@@ -142,6 +152,8 @@ module LightIO::Watchers
142
152
  end
143
153
 
144
154
  def callback_on_waiting
155
+ # update readiness on callback
156
+ @readiness = monitor.readiness
145
157
  # only call callback on waiting
146
158
  return unless io_is_ready?
147
159
  if @error
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jiang Jinyang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-01 00:00:00.000000000 Z
11
+ date: 2018-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r