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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 924070966457c426a62607d4273deb386f2717bd
4
- data.tar.gz: e12903fe1825a6076d076e369a9a3e9c84d9281d
3
+ metadata.gz: d60e525147d0be04ceecef1b5d5707dbaeeba6d1
4
+ data.tar.gz: 69c611c348c2837484857e850834e6bc93fc6c44
5
5
  SHA512:
6
- metadata.gz: e7e4b6081e60112ed2d077439b13a2a3786c6e76dd6366262597dc656192e78e6b36514035943813b684aa308cf92ade03129d68c6cdfca86b1a8b695987c716
7
- data.tar.gz: 8bff2669c62c7b0ee425dc82ad2a32a0c64809b9caf3e4e29f2d30c46c142677102981fb73183a528f22b94259cbef2f5524128fa61f80ad6881371e4cc72c81
6
+ metadata.gz: a53025d4e89dcfab86d8c41af53b0a8ac078d86d9a220549a364638be22be4da133c303ce3e738a910b6a9844907a5c4ee506f27fa041728930770ea0ac1ea61
7
+ data.tar.gz: 60e88d7aed7cf946368cc1f2aa6786d644c00d30fa9f2c0855b5cb9fb1c41d4837e0e98b52beb85fbaa761af9c39beaa92dda8cfcbdea6afb584b0859bcc831f
@@ -0,0 +1 @@
1
+ 2.5.0-dev
@@ -16,5 +16,4 @@ matrix:
16
16
  allow_failures:
17
17
  - rvm: ruby-head
18
18
  - rvm: jruby-9.1.15.0
19
- - rvm: 2.5.0
20
19
  fast_finish: true
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lightio (0.4.1)
4
+ lightio (0.4.2)
5
5
  nio4r (~> 2.2)
6
6
 
7
7
  GEM
@@ -96,4 +96,4 @@ DEPENDENCIES
96
96
  rspec (~> 3.0)
97
97
 
98
98
  BUNDLED WITH
99
- 1.16.0
99
+ 1.16.1
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
- # now you can just write normal ruby code
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
- `sleep 1`
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
- puts Time.now - start
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
@@ -42,7 +42,7 @@ module LightIO::Library
42
42
  return outbuf
43
43
  end
44
44
  else
45
- return length.nil? ? outbuf : nil
45
+ return outbuf.empty? && length ? nil : outbuf
46
46
  end
47
47
  end
48
48
  end
@@ -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
- namespace_index = name.rindex("::")
8
- class_name = namespace_index.nil? ? name : name[(namespace_index + 2)..-1]
9
- LightIO::Library.const_get(class_name)
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
 
@@ -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 process raw IO, use LightIO::IO._wrap(obj) to wrap it" if to_io.is_a?(::IO)
18
- raise TypeError, "can't convert #{io.class} to IO (#{io.class}#to_io gives #{to_io.class})"
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 ||= []
@@ -10,6 +10,13 @@ module LightIO
10
10
 
11
11
  class << self
12
12
  def patch_all!
13
+ # Fix https://github.com/socketry/lightio/issues/7
14
+ begin
15
+ require 'ffi'
16
+ rescue LoadError
17
+ nil
18
+ end
19
+
13
20
  patch_thread!
14
21
  patch_io!
15
22
  patch_kernel!
@@ -1,3 +1,3 @@
1
1
  module LightIO
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
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.1
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-10 00:00:00.000000000 Z
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.11
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