lightio 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.travis.yml +0 -1
- data/Gemfile.lock +2 -2
- data/README.md +39 -4
- data/lib/lightio/library/io.rb +1 -1
- data/lib/lightio/module/base.rb +9 -3
- data/lib/lightio/module/io.rb +32 -4
- data/lib/lightio/monkey.rb +7 -0
- data/lib/lightio/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d60e525147d0be04ceecef1b5d5707dbaeeba6d1
|
4
|
+
data.tar.gz: 69c611c348c2837484857e850834e6bc93fc6c44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a53025d4e89dcfab86d8c41af53b0a8ac078d86d9a220549a364638be22be4da133c303ce3e738a910b6a9844907a5c4ee506f27fa041728930770ea0ac1ea61
|
7
|
+
data.tar.gz: 60e88d7aed7cf946368cc1f2aa6786d644c00d30fa9f2c0855b5cb9fb1c41d4837e0e98b52beb85fbaa761af9c39beaa92dda8cfcbdea6afb584b0859bcc831f
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0-dev
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,28 +8,63 @@
|
|
8
8
|
|
9
9
|
LightIO provide green thread to ruby. Like Golang's goroutine, or Crystal's fiber. In LightIO it called beam.
|
10
10
|
|
11
|
+
Example:
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
require 'lightio'
|
15
|
+
|
16
|
+
start = Time.now
|
17
|
+
|
18
|
+
beams = 1000.times.map do
|
19
|
+
# LightIO::Beam is green-thread, use it instead Thread
|
20
|
+
LightIO::Beam.new do
|
21
|
+
# do some io operations in beam
|
22
|
+
LightIO.sleep(1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
beams.each(&:join)
|
27
|
+
seconds = Time.now - start
|
28
|
+
puts "1000 beams take #{seconds - 1} seconds to create"
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
|
11
33
|
LightIO ship ruby stdlib compatible library under `LightIO` or `LightIO::Library` namespace,
|
12
34
|
these libraries provide ability to schedule LightIO beams when IO operations occur.
|
13
35
|
|
14
|
-
See [Examples](/examples) for detail.
|
15
36
|
|
16
37
|
LightIO also provide a monkey patch, it replace ruby `Thread` with `LightIO::Thread`, and also replace `IO` related classes.
|
17
38
|
|
18
39
|
Example:
|
19
40
|
|
20
41
|
``` ruby
|
42
|
+
require 'lightio'
|
43
|
+
# apply monkey patch at beginning
|
21
44
|
LightIO::Monkey.patch_all!
|
22
|
-
|
45
|
+
|
46
|
+
require 'net/http'
|
47
|
+
|
48
|
+
host = 'github.com'
|
49
|
+
port = 443
|
50
|
+
|
23
51
|
start = Time.now
|
52
|
+
|
24
53
|
10.times.map do
|
25
54
|
Thread.new do
|
26
|
-
|
55
|
+
Net::HTTP.start(host, port, use_ssl: true) do |http|
|
56
|
+
res = http.request_get('/ping')
|
57
|
+
p res.code
|
58
|
+
end
|
27
59
|
end
|
28
60
|
end.each(&:join)
|
29
|
-
|
61
|
+
|
62
|
+
puts "#{Time.now - start} seconds"
|
30
63
|
|
31
64
|
```
|
32
65
|
|
66
|
+
See [Examples](/examples) for detail.
|
67
|
+
|
33
68
|
### You Should Know
|
34
69
|
|
35
70
|
In fact ruby core team already plan to implement `Thread::Green` in core language, see https://bugs.ruby-lang.org/issues/13618
|
data/lib/lightio/library/io.rb
CHANGED
data/lib/lightio/module/base.rb
CHANGED
@@ -4,9 +4,15 @@ module LightIO::Module
|
|
4
4
|
def find_library_class(klass)
|
5
5
|
return LightIO::Library::Base.send(:nameless_classes)[klass] if klass.name.nil?
|
6
6
|
name = klass.name
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
begin
|
8
|
+
LightIO::Library.const_get(name)
|
9
|
+
rescue NameError
|
10
|
+
# retry without namespace
|
11
|
+
namespace_index = name.rindex("::")
|
12
|
+
raise if namespace_index.nil?
|
13
|
+
class_name = name[(namespace_index + 2)..-1]
|
14
|
+
LightIO::Library.const_get(class_name)
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
12
18
|
|
data/lib/lightio/module/io.rb
CHANGED
@@ -12,10 +12,18 @@ module LightIO::Module
|
|
12
12
|
unless io.respond_to?(:to_io)
|
13
13
|
raise TypeError, "no implicit conversion of #{io.class} into IO"
|
14
14
|
end
|
15
|
-
to_io = io.to_io
|
15
|
+
to_io = io.is_a?(LightIO::Library::IO) ? io : io.to_io
|
16
16
|
unless to_io.is_a?(LightIO::Library::IO)
|
17
|
-
raise TypeError, "can't
|
18
|
-
|
17
|
+
raise TypeError, "can't convert #{io.class} to IO (#{io.class}#to_io gives #{to_io.class})" unless to_io.is_a?(::IO)
|
18
|
+
|
19
|
+
# try wrap raw io instead of raise error
|
20
|
+
wrapped_io = to_io.instance_variable_get(:@_lightio_wrapped_io)
|
21
|
+
unless wrapped_io
|
22
|
+
wrapped_io = LightIO::Library::IO._wrap(to_io)
|
23
|
+
to_io.instance_variable_set(:@_lightio_wrapped_io, wrapped_io)
|
24
|
+
end
|
25
|
+
to_io = wrapped_io
|
26
|
+
# raise TypeError, "can't process raw IO, use LightIO::IO._wrap(obj) to wrap it"
|
19
27
|
end
|
20
28
|
to_io
|
21
29
|
end
|
@@ -51,10 +59,30 @@ module LightIO::Module
|
|
51
59
|
r.close
|
52
60
|
end
|
53
61
|
end
|
54
|
-
# [r, w]
|
55
62
|
[wrap_to_library(r), wrap_to_library(w)]
|
56
63
|
end
|
57
64
|
|
65
|
+
def copy_stream(*args)
|
66
|
+
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 2..4)" unless (2..4).include?(args.size)
|
67
|
+
src, dst, copy_length, src_offset = args
|
68
|
+
src = src.respond_to?(:to_io) ? src.to_io : LightIO::Library::File.open(src, 'r') unless src.is_a?(IO)
|
69
|
+
dst = dst.respond_to?(:to_io) ? dst.to_io : LightIO::Library::File.open(dst, 'w') unless dst.is_a?(IO)
|
70
|
+
buf_size = 4096
|
71
|
+
copy_chars = 0
|
72
|
+
buf_size = [buf_size, copy_length].min if copy_length
|
73
|
+
src.seek(src_offset) if src_offset
|
74
|
+
while (buf = src.read(buf_size))
|
75
|
+
size = dst.write(buf)
|
76
|
+
copy_chars += size
|
77
|
+
if copy_length
|
78
|
+
copy_length -= size
|
79
|
+
break if copy_length.zero?
|
80
|
+
buf_size = [buf_size, copy_length].min
|
81
|
+
end
|
82
|
+
end
|
83
|
+
copy_chars
|
84
|
+
end
|
85
|
+
|
58
86
|
def select(read_fds, write_fds=nil, _except_fds=nil, timeout=nil)
|
59
87
|
timer = timeout && Time.now
|
60
88
|
read_fds ||= []
|
data/lib/lightio/monkey.rb
CHANGED
data/lib/lightio/version.rb
CHANGED
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.4.
|
4
|
+
version: 0.4.2
|
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-02-
|
11
|
+
date: 2018-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|
@@ -77,6 +77,7 @@ extra_rdoc_files: []
|
|
77
77
|
files:
|
78
78
|
- ".gitignore"
|
79
79
|
- ".rspec"
|
80
|
+
- ".ruby-version"
|
80
81
|
- ".travis.yml"
|
81
82
|
- CODE_OF_CONDUCT.md
|
82
83
|
- Gemfile
|
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
153
|
version: '0'
|
153
154
|
requirements: []
|
154
155
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.6.
|
156
|
+
rubygems_version: 2.6.14
|
156
157
|
signing_key:
|
157
158
|
specification_version: 4
|
158
159
|
summary: LightIO is a ruby networking library, that combines ruby fiber and fast IO
|