em-midori 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/midori/extension/redic.rb +88 -0
- data/lib/midori/request.rb +2 -1
- data/lib/midori/runner.rb +2 -1
- data/lib/midori/server.rb +3 -3
- data/lib/midori/version.rb +1 -1
- metadata +3 -3
- data/lib/midori/extension/hiredis.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3925b4b9ef7e976cc1e1af24b2979a0e3a276f06
|
4
|
+
data.tar.gz: 6b8a7e0d0300709096247aed4755a285d128cf94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 449941b9d262ef8a1e8727524976e3af90effde6efc56d043258b37db1eb3a932dc16c6ff701ccd18911dbd23d9c0c821441d687ccabc9e811a5c91b197cf419
|
7
|
+
data.tar.gz: 512051dcf9e5d764ba1c46b4cc1f817189b91d65875b08472a2337cdbe3da6d9aac72124e49da4b71b6a43ca3e607382d862ed9b699f6ae2aa3cc30cb7bc72dd
|
@@ -0,0 +1,88 @@
|
|
1
|
+
##
|
2
|
+
# Meta-programming hiredis for redis async extension
|
3
|
+
module Hiredis
|
4
|
+
require 'hiredis/connection'
|
5
|
+
# Overwrite with ruby implementation to hook IO
|
6
|
+
require 'hiredis/ruby/connection'
|
7
|
+
require 'hiredis/ruby/reader'
|
8
|
+
# Redis Connection
|
9
|
+
Connection = Ruby::Connection
|
10
|
+
# Redis Result Reader
|
11
|
+
Reader = Ruby::Reader
|
12
|
+
|
13
|
+
##
|
14
|
+
# Meta-programming hiredis for redis async extension
|
15
|
+
module Ruby
|
16
|
+
##
|
17
|
+
# Meta-programming hiredis for redis async extension
|
18
|
+
class Connection
|
19
|
+
# Do redis query
|
20
|
+
# @param [String] data generated string data
|
21
|
+
def query(args)
|
22
|
+
await(Promise.new do |resolve|
|
23
|
+
read_flag = false
|
24
|
+
data = pre_write(args)
|
25
|
+
written = 0
|
26
|
+
EventLoop.register(@sock, :rw) do |monitor|
|
27
|
+
if read_flag && monitor.readable?
|
28
|
+
# Reading
|
29
|
+
_read(resolve, @sock)
|
30
|
+
end
|
31
|
+
if !read_flag && monitor.writable?
|
32
|
+
# Writing
|
33
|
+
written += @sock.write_nonblock(data[written..-1])
|
34
|
+
read_flag = true if written == string_size(data)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def pre_write(args)
|
42
|
+
command = []
|
43
|
+
command << "*#{args.size}"
|
44
|
+
args.each do |arg|
|
45
|
+
arg = arg.to_s
|
46
|
+
command << "$#{string_size arg}"
|
47
|
+
command << arg
|
48
|
+
end
|
49
|
+
data = command.join(COMMAND_DELIMITER) + COMMAND_DELIMITER
|
50
|
+
data.force_encoding('binary') if data.respond_to?(:force_encoding)
|
51
|
+
data
|
52
|
+
end
|
53
|
+
|
54
|
+
def _read(resolve, sock)
|
55
|
+
@reader.feed @sock.read_nonblock(1024)
|
56
|
+
reply = @reader.gets
|
57
|
+
if reply
|
58
|
+
EventLoop.deregister(sock)
|
59
|
+
resolve.call(reply)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
require 'redic'
|
67
|
+
|
68
|
+
class Redic
|
69
|
+
class Client
|
70
|
+
def connect
|
71
|
+
establish_connection unless connected?
|
72
|
+
if block_given?
|
73
|
+
@semaphore.synchronize do
|
74
|
+
yield
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def call(*args)
|
80
|
+
@connection.query(*args)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def call(*args)
|
85
|
+
@client.connect
|
86
|
+
@client.call(args)
|
87
|
+
end
|
88
|
+
end
|
data/lib/midori/request.rb
CHANGED
@@ -8,7 +8,8 @@
|
|
8
8
|
# @attr [String] query_string request query string
|
9
9
|
# @attr [Hash] header request header
|
10
10
|
# @attr [String] body request body
|
11
|
-
# @attr [Boolean] parsed whether the request parsed
|
11
|
+
# @attr [Boolean] parsed whether the request header parsed
|
12
|
+
# @attr [Boolean] body_parsed whether the request body parsed
|
12
13
|
# @attr [Hash] params params in the url
|
13
14
|
class Midori::Request
|
14
15
|
attr_accessor :ip, :port,
|
data/lib/midori/runner.rb
CHANGED
@@ -2,8 +2,9 @@
|
|
2
2
|
# Abstract runner class to control instance of Midori Server
|
3
3
|
# @attr [String] bind the address to bind
|
4
4
|
# @attr [Integer] port the port to bind
|
5
|
+
# @attr [Logger] logger midori logger
|
5
6
|
class Midori::Runner
|
6
|
-
attr_reader :bind, :port
|
7
|
+
attr_reader :bind, :port, :logger
|
7
8
|
|
8
9
|
# Define status of a runner
|
9
10
|
# @param [Class] api inherited from [Midori::API]
|
data/lib/midori/server.rb
CHANGED
@@ -28,7 +28,6 @@ module Midori::Server
|
|
28
28
|
lambda do
|
29
29
|
async_fiber(Fiber.new do
|
30
30
|
begin
|
31
|
-
start_time = Time.now
|
32
31
|
_sock_domain, remote_port, _remote_hostname, remote_ip = monitor.io.peeraddr
|
33
32
|
port, ip = remote_port, remote_ip
|
34
33
|
@request.ip = ip
|
@@ -40,8 +39,6 @@ module Midori::Server
|
|
40
39
|
@request.parse(data)
|
41
40
|
receive_new_request if @request.parsed && @request.body_parsed?
|
42
41
|
end
|
43
|
-
now_time = Time.now
|
44
|
-
@logger.info "#{@request.ip} - - \"#{@request.method} #{@request.path} HTTP/#{@request.protocol.join('.')}\" #{@response.status} #{(now_time.to_f - start_time.to_f).round(6)}".green
|
45
42
|
rescue => e
|
46
43
|
close_connection
|
47
44
|
@logger.warn "#{@request.ip} - - #{e.backtrace.join("\n")}".yellow
|
@@ -53,7 +50,10 @@ module Midori::Server
|
|
53
50
|
# Logic of receiving new request
|
54
51
|
def receive_new_request
|
55
52
|
begin
|
53
|
+
start_time = Time.now
|
56
54
|
@response = @api.receive(request, self)
|
55
|
+
now_time = Time.now
|
56
|
+
@logger.info "#{@request.ip} - - \"#{@request.method} #{@request.path} HTTP/#{@request.protocol.join('.')}\" #{@response.status} #{sprintf("%.6f", now_time.to_f - start_time.to_f)}".green
|
57
57
|
call_event(:open) if @request.websocket?
|
58
58
|
rescue Midori::Exception::NotFound => e
|
59
59
|
@response = Midori::Sandbox.capture(e)
|
data/lib/midori/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.4
|
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-06-
|
11
|
+
date: 2017-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|
@@ -80,8 +80,8 @@ files:
|
|
80
80
|
- lib/midori/eventsource.rb
|
81
81
|
- lib/midori/exception.rb
|
82
82
|
- lib/midori/extension/file.rb
|
83
|
-
- lib/midori/extension/hiredis.rb
|
84
83
|
- lib/midori/extension/net.rb
|
84
|
+
- lib/midori/extension/redic.rb
|
85
85
|
- lib/midori/extension/sequel/mysql2.rb
|
86
86
|
- lib/midori/extension/sequel/postgres.rb
|
87
87
|
- lib/midori/middleware.rb
|
@@ -1,59 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# Meta-programming hiredis for redis async extension
|
3
|
-
module Hiredis
|
4
|
-
require 'hiredis/connection'
|
5
|
-
# Overwrite with ruby implementation to hook IO
|
6
|
-
require 'hiredis/ruby/connection'
|
7
|
-
require 'hiredis/ruby/reader'
|
8
|
-
# Redis Connection
|
9
|
-
Connection = Ruby::Connection
|
10
|
-
# Redis Result Reader
|
11
|
-
Reader = Ruby::Reader
|
12
|
-
|
13
|
-
##
|
14
|
-
# Meta-programming hiredis for redis async extension
|
15
|
-
module Ruby
|
16
|
-
##
|
17
|
-
# Meta-programming hiredis for redis async extension
|
18
|
-
class Connection
|
19
|
-
# write message directly
|
20
|
-
# @param [IO] _sock raw socket
|
21
|
-
# @param [String] data generated string data
|
22
|
-
# @param [Float] _timeout operation timeout
|
23
|
-
def _write(_sock, data, _timeout)
|
24
|
-
await(Promise.new do |resolve|
|
25
|
-
data.force_encoding('binary') if data.respond_to?(:force_encoding)
|
26
|
-
written = 0
|
27
|
-
EventLoop.register(@sock, :w) do
|
28
|
-
written += @sock.write_nonblock(data[written..-1])
|
29
|
-
if written == string_size(data)
|
30
|
-
EventLoop.deregister(@sock)
|
31
|
-
resolve.call
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end)
|
35
|
-
end
|
36
|
-
|
37
|
-
# read from redis socket
|
38
|
-
# @return [String] reply
|
39
|
-
def read
|
40
|
-
raise 'not connected' unless connected?
|
41
|
-
await(Promise.new do |resolve|
|
42
|
-
EventLoop.register(@sock, :r) do
|
43
|
-
@reader.feed @sock.read_nonblock(1024)
|
44
|
-
reply = @reader.gets
|
45
|
-
if reply
|
46
|
-
EventLoop.deregister(@sock)
|
47
|
-
resolve.call(reply)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end)
|
51
|
-
rescue ::EOFError
|
52
|
-
# :nocov:
|
53
|
-
# Error Handler Copied from HiRedis driver itself
|
54
|
-
raise Errno::ECONNRESET
|
55
|
-
# :nocov:
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|