lightio 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/lightio/core/backend/nio.rb +7 -5
- data/lib/lightio/core/ioloop.rb +1 -1
- data/lib/lightio/library/io.rb +13 -5
- data/lib/lightio/version.rb +1 -1
- data/lib/lightio/watchers/io.rb +14 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2adcb4321383299d719e6faa66696d14554e0d9c
|
4
|
+
data.tar.gz: 34ca8bb7b88f2f0753264016c21f7840dd48475d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ba852354c4797ec86547105b59e8364b6e51cacee2bf72bff882ec0e9a8f8a02c967e3a505766f2c6912dd50c628dbc1d339e1352177f532df86a1969e64187
|
7
|
+
data.tar.gz: 85b37be690bf476c772ca64082264447a3d07ea9fa2593b313ddca7c37174b618ea118b6e5f3ea99fe68f8edde6827d11ab7e9f84ef01f140e2b873417551711
|
data/Gemfile.lock
CHANGED
@@ -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
|
100
|
+
return if closed?
|
97
101
|
@running = false
|
98
|
-
|
102
|
+
@selector.close
|
99
103
|
end
|
100
104
|
|
101
|
-
|
102
|
-
@selector.backend
|
103
|
-
end
|
105
|
+
alias close stop
|
104
106
|
|
105
107
|
def env_backend
|
106
108
|
key = 'LIGHTIO_BACKEND'.freeze
|
data/lib/lightio/core/ioloop.rb
CHANGED
@@ -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
|
data/lib/lightio/library/io.rb
CHANGED
@@ -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
|
-
|
145
|
-
|
148
|
+
read_fds ||= []
|
149
|
+
write_fds ||= []
|
146
150
|
loop do
|
147
|
-
|
148
|
-
|
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
|
data/lib/lightio/version.rb
CHANGED
data/lib/lightio/watchers/io.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2018-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|