em-midori 0.1.11 → 0.1.12

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: 5026a74972dc1290c62c3fcc076c881164527ead
4
- data.tar.gz: b93b97c69b109b6e28b9d05d46be862ff13447a5
3
+ metadata.gz: 4ad463d2a5c54f9995812d347d0f428752414d77
4
+ data.tar.gz: 9a9ea6eb52f59bf540e7dc37d24a6581393a439d
5
5
  SHA512:
6
- metadata.gz: 885500b149d1153fededb86c8610be89361c2a3992d0f1a2ace635226121137f9beb5c32c9a519998ffeaff210f25b2a14d47176a5b031e487e8d36d7019d17d
7
- data.tar.gz: 6d1563e43233277c2d41ef1bdb732e1131714449f67b0505d64777a6228d15804564b7e158b2a709cd8c9455588c0316ddd2a10b026bca213831a976f43b1e0c
6
+ metadata.gz: bf12420ff582c13a143a872462284f3232472decff29a686286d931eb5fac60371182a313e0327c41281eea5a8a673913dd59eeebf560a8ecc99a4245aeaca9b
7
+ data.tar.gz: b40da8f51f4c39e5b9dd46a5944c17b0672425ffa5869a14a5880df85d0ce9bd92c19daa7433131624e4b93308ecf69d09f8c0e5afb3c0edcc3b4111957c3066
data/lib/midori.rb CHANGED
@@ -10,6 +10,7 @@ require 'socket'
10
10
  require_relative 'midori/core_ext/configurable'
11
11
  require_relative 'midori/core_ext/string'
12
12
  require_relative 'midori/core_ext/event_loop'
13
+ require_relative 'midori/core_ext/timer'
13
14
  require_relative 'midori/core_ext/promise'
14
15
  require_relative 'midori/core_ext/define_class'
15
16
  require_relative 'midori/core_ext/proc'
@@ -20,8 +20,11 @@ class Midori::Connection
20
20
  end
21
21
  if monitor.writable?
22
22
  if !@data.empty?
23
+ # :nocov:
24
+ # Leave for corner cases
23
25
  monitor.io.write_nonblock(@data)
24
26
  @data = ''
27
+ # :nocov:
25
28
  elsif @close_flag
26
29
  close_connection
27
30
  end
@@ -34,7 +37,7 @@ class Midori::Connection
34
37
  end
35
38
 
36
39
  def close_connection
37
- EventLoop.unregister @socket
40
+ EventLoop.deregister @socket
38
41
  @socket.close
39
42
  end
40
43
 
@@ -1,19 +1,42 @@
1
+ require 'timeout'
2
+
1
3
  module EventLoop
2
4
  class << self
3
5
  SELECTOR = NIO::Selector.new
6
+ TIMERS = []
7
+
8
+ def add_timer(timer)
9
+ timer.start_time = Time.now.to_f + timer.time
10
+ TIMERS << timer
11
+ end
4
12
 
5
13
  def register(io, interest=(:rw), &callback)
6
14
  monitor = SELECTOR.register(io, interest)
7
- monitor.value = callback
15
+ monitor.value = {
16
+ callback: callback,
17
+ }
18
+
8
19
  end
9
20
 
10
- def unregister(io)
21
+ def deregister(io)
11
22
  SELECTOR.deregister(io)
12
23
  end
13
24
 
14
25
  def run_once
15
- SELECTOR.select(1) do |monitor| # Timeout for 1 secs
16
- monitor.value.call(monitor)
26
+ SELECTOR.select(0.2) do |monitor| # Timeout for 1 secs
27
+ monitor.value[:timer].stop unless monitor.value[:timer].nil?
28
+ monitor.value[:callback].call(monitor)
29
+ end
30
+ EventLoop.timer_once
31
+ end
32
+
33
+ def timer_once
34
+ now_time = Time.now.to_f
35
+ TIMERS.delete_if do |timer|
36
+ if timer.start_time < now_time
37
+ timer.callback.call
38
+ true
39
+ end
17
40
  end
18
41
  end
19
42
 
@@ -0,0 +1,10 @@
1
+ class EventLoop::Timer
2
+ attr_reader :time, :start_time, :callback
3
+ attr_accessor :start_time
4
+
5
+ def initialize(time, &callback)
6
+ @time = time
7
+ @callback = callback
8
+ @start_time = Float::INFINITY
9
+ end
10
+ end
@@ -8,7 +8,7 @@ class Midori::File
8
8
  data = ''
9
9
  EventLoop.register(@file, :r) do
10
10
  if @file.eof?
11
- EventLoop.unregister(@file)
11
+ EventLoop.deregister(@file)
12
12
  resolve.call(data)
13
13
  else
14
14
  data << @file.read_nonblock(16384)
@@ -23,7 +23,7 @@ class Midori::File
23
23
  EventLoop.register(@file, :w) do
24
24
  written += @file.write_nonblock(data[written..-1])
25
25
  if written == data.size
26
- EventLoop.unregister(@file)
26
+ EventLoop.deregister(@file)
27
27
  resolve.call(written)
28
28
  end
29
29
  end
@@ -17,7 +17,7 @@ module Hiredis
17
17
  EventLoop.register(@sock, :w) do
18
18
  written += @sock.write_nonblock(data[written..-1])
19
19
  if written == string_size(data)
20
- EventLoop.unregister(@sock)
20
+ EventLoop.deregister(@sock)
21
21
  resolve.call
22
22
  end
23
23
  end
@@ -31,7 +31,7 @@ module Hiredis
31
31
  @reader.feed @sock.read_nonblock(1024)
32
32
  reply = @reader.gets
33
33
  if reply
34
- EventLoop.unregister(@sock)
34
+ EventLoop.deregister(@sock)
35
35
  resolve.call(reply)
36
36
  end
37
37
  end
@@ -0,0 +1,36 @@
1
+ require 'net/protocol'
2
+
3
+ class Net::BufferedIO
4
+ def wait_io(interest)
5
+ await(Promise.new do |resolve|
6
+ EventLoop.register(@io.to_io, interest) do
7
+ EventLoop.deregister(@io.to_io)
8
+ resolve.call(self)
9
+ end
10
+ end)
11
+ end
12
+
13
+ def rbuf_fill
14
+ loop do
15
+ case rv = @io.read_nonblock(BUFSIZE, exception: false)
16
+ when String
17
+ return @rbuf << rv
18
+ when :wait_readable
19
+ wait_io(:r)
20
+ # @io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
21
+ # continue looping
22
+ when :wait_writable
23
+ # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
24
+ # http://www.openssl.org/support/faq.html#PROG10
25
+ # :nocov:
26
+ wait_io(:w)
27
+ # @io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
28
+ # continue looping
29
+ when nil
30
+ # callers do not care about backtrace, so avoid allocating for it
31
+ raise EOFError, 'end of file reached', []
32
+ # :nocov:
33
+ end
34
+ end
35
+ end
36
+ end
@@ -39,7 +39,7 @@ module Sequel
39
39
  socket = IO::open(conn.socket)
40
40
  await(Promise.new do |resolve|
41
41
  EventLoop.register(socket, :w) do
42
- EventLoop.unregister(socket)
42
+ EventLoop.deregister(socket)
43
43
  conn.query(sql,
44
44
  database_timezone: timezone,
45
45
  application_timezone: Sequel.application_timezone,
@@ -50,7 +50,7 @@ module Sequel
50
50
  end)
51
51
  await(Promise.new do |resolve|
52
52
  EventLoop.register(socket, :r) do
53
- EventLoop.unregister(socket)
53
+ EventLoop.deregister(socket)
54
54
  resolve.call(conn.async_result)
55
55
  end
56
56
  end)
@@ -14,7 +14,7 @@ class Sequel::Postgres::Adapter
14
14
  await(Promise.new do |resolve|
15
15
  EventLoop.register(socket_object, :w) do
16
16
  unless is_busy
17
- EventLoop.unregister(socket_object)
17
+ EventLoop.deregister(socket_object)
18
18
  send_query(sql)
19
19
  resolve.call
20
20
  end
@@ -24,7 +24,7 @@ class Sequel::Postgres::Adapter
24
24
  await(Promise.new do |resolve|
25
25
  consume_input
26
26
  EventLoop.register(socket_object, :r) do
27
- EventLoop.unregister(socket_object)
27
+ EventLoop.deregister(socket_object)
28
28
  resolve.call(get_result)
29
29
  end
30
30
  end)
data/lib/midori/runner.rb CHANGED
@@ -44,7 +44,7 @@ class Midori::Runner
44
44
  def stop
45
45
  if running?
46
46
  @logger.info 'Stopping Midori'.blue
47
- EventLoop.unregister @server
47
+ EventLoop.deregister @server
48
48
  @server.close
49
49
  @server = nil
50
50
  EventLoop.stop
@@ -1,5 +1,5 @@
1
1
  # Midori Module
2
2
  module Midori
3
3
  # Current Version Code
4
- VERSION = '0.1.11'.freeze
4
+ VERSION = '0.1.12'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-midori
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - HeckPsi Lab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r
@@ -74,10 +74,12 @@ files:
74
74
  - lib/midori/core_ext/promise.rb
75
75
  - lib/midori/core_ext/safe_require.rb
76
76
  - lib/midori/core_ext/string.rb
77
+ - lib/midori/core_ext/timer.rb
77
78
  - lib/midori/eventsource.rb
78
79
  - lib/midori/exception.rb
79
80
  - lib/midori/extension/file.rb
80
81
  - lib/midori/extension/hiredis.rb
82
+ - lib/midori/extension/net.rb
81
83
  - lib/midori/extension/sequel/mysql2.rb
82
84
  - lib/midori/extension/sequel/postgres.rb
83
85
  - lib/midori/middleware.rb