espace-neverblock 0.1.3 → 0.1.4
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'mysqlplus'
|
|
2
2
|
|
|
3
3
|
module NeverBlock
|
|
4
4
|
|
|
@@ -18,27 +18,48 @@ module NeverBlock
|
|
|
18
18
|
# Creates a new mysql connection, sets it
|
|
19
19
|
# to nonblocking and wraps the descriptor in an IO
|
|
20
20
|
# object.
|
|
21
|
-
def real_connect(*args)
|
|
22
|
-
super(*args)
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
def self.real_connect(*args)
|
|
22
|
+
me = super(*args)
|
|
23
|
+
me.init_descriptor
|
|
24
|
+
me
|
|
25
25
|
end
|
|
26
26
|
#alias :real_connect :initialize
|
|
27
27
|
#alias :connect :initialize
|
|
28
|
+
|
|
29
|
+
def init_descriptor
|
|
30
|
+
@fd = socket
|
|
31
|
+
@io = IO.new(socket)
|
|
32
|
+
end
|
|
28
33
|
|
|
29
34
|
# Assuming the use of NeverBlock fiber extensions and that the exec is run in
|
|
30
35
|
# the context of a fiber. One that have the value :neverblock set to true.
|
|
31
36
|
# All neverblock IO classes check this value, setting it to false will force
|
|
32
37
|
# the execution in a blocking way.
|
|
33
38
|
def query(sql)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
begin
|
|
40
|
+
if Fiber.respond_to? :current and Fiber.current[:neverblock]
|
|
41
|
+
send_query sql
|
|
42
|
+
@fiber = Fiber.current
|
|
43
|
+
Fiber.yield
|
|
44
|
+
get_result
|
|
45
|
+
else
|
|
46
|
+
super(sql)
|
|
47
|
+
end
|
|
48
|
+
rescue Exception => e
|
|
49
|
+
reconnect if e.msg.include? "not connected"
|
|
50
|
+
raise e
|
|
40
51
|
end
|
|
41
52
|
end
|
|
53
|
+
|
|
54
|
+
# reset the connection
|
|
55
|
+
# and reattach to the
|
|
56
|
+
# event loop
|
|
57
|
+
def reconnect
|
|
58
|
+
unregister_from_event_loop
|
|
59
|
+
super
|
|
60
|
+
init_descriptor
|
|
61
|
+
register_with_event_loop(@loop)
|
|
62
|
+
end
|
|
42
63
|
|
|
43
64
|
# Attaches the connection socket to an event loop.
|
|
44
65
|
# Currently only supports EM, but Rev support will be
|
|
@@ -73,8 +94,8 @@ module NeverBlock
|
|
|
73
94
|
|
|
74
95
|
# The callback, this is called whenever
|
|
75
96
|
# there is data available at the socket
|
|
76
|
-
def
|
|
77
|
-
@fiber.resume
|
|
97
|
+
def resume_command
|
|
98
|
+
@fiber.resume
|
|
78
99
|
end
|
|
79
100
|
|
|
80
101
|
end #FiberedPostgresConnection
|
|
@@ -86,7 +107,7 @@ module NeverBlock
|
|
|
86
107
|
@connection = connection
|
|
87
108
|
end
|
|
88
109
|
def notify_readable
|
|
89
|
-
@connection.
|
|
110
|
+
@connection.resume_command
|
|
90
111
|
end
|
|
91
112
|
end
|
|
92
113
|
|
|
@@ -20,24 +20,52 @@ module NeverBlock
|
|
|
20
20
|
# object.
|
|
21
21
|
def initialize(*args)
|
|
22
22
|
super(*args)
|
|
23
|
-
|
|
24
|
-
@io = IO.new(socket)
|
|
23
|
+
init_descriptor
|
|
25
24
|
#setnonblocking(true)
|
|
26
25
|
end
|
|
27
|
-
|
|
26
|
+
|
|
27
|
+
def init_descriptor
|
|
28
|
+
@fd = socket
|
|
29
|
+
@io = IO.new(socket)
|
|
30
|
+
end
|
|
28
31
|
# Assuming the use of NeverBlock fiber extensions and that the exec is run in
|
|
29
32
|
# the context of a fiber. One that have the value :neverblock set to true.
|
|
30
33
|
# All neverblock IO classes check this value, setting it to false will force
|
|
31
34
|
# the execution in a blocking way.
|
|
32
35
|
def exec(sql)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
begin
|
|
37
|
+
if Fiber.respond_to? :current and Fiber.current[:neverblock]
|
|
38
|
+
send_query sql
|
|
39
|
+
@fiber = Fiber.current
|
|
40
|
+
Fiber.yield
|
|
41
|
+
while is_busy
|
|
42
|
+
consume_input
|
|
43
|
+
Fiber.yield if is_busy
|
|
44
|
+
end
|
|
45
|
+
res, data = 0, []
|
|
46
|
+
while res != nil
|
|
47
|
+
res = self.get_result
|
|
48
|
+
data << res unless res.nil?
|
|
49
|
+
end
|
|
50
|
+
data.last
|
|
51
|
+
else
|
|
52
|
+
super(sql)
|
|
53
|
+
end
|
|
54
|
+
rescue Exception => e
|
|
55
|
+
reset if e.msg.include? "not connected"
|
|
56
|
+
raise e
|
|
39
57
|
end
|
|
40
58
|
end
|
|
59
|
+
|
|
60
|
+
# reset the connection
|
|
61
|
+
# and reattach to the
|
|
62
|
+
# event loop
|
|
63
|
+
def reset
|
|
64
|
+
unregister_from_event_loop
|
|
65
|
+
super
|
|
66
|
+
init_descriptor
|
|
67
|
+
register_with_event_loop(@loop)
|
|
68
|
+
end
|
|
41
69
|
|
|
42
70
|
# Attaches the connection socket to an event loop.
|
|
43
71
|
# Currently only supports EM, but Rev support will be
|
|
@@ -73,20 +101,9 @@ module NeverBlock
|
|
|
73
101
|
|
|
74
102
|
# The callback, this is called whenever
|
|
75
103
|
# there is data available at the socket
|
|
76
|
-
def
|
|
77
|
-
#
|
|
78
|
-
|
|
79
|
-
#return unless self.flush
|
|
80
|
-
self.consume_input
|
|
81
|
-
unless is_busy
|
|
82
|
-
res, data = 0, []
|
|
83
|
-
while res != nil
|
|
84
|
-
res = self.get_result
|
|
85
|
-
data << res unless res.nil?
|
|
86
|
-
end
|
|
87
|
-
#let the fiber continue its work
|
|
88
|
-
@fiber.resume(data.last)
|
|
89
|
-
end
|
|
104
|
+
def resume_command
|
|
105
|
+
#let the fiber continue its work
|
|
106
|
+
@fiber.resume
|
|
90
107
|
end
|
|
91
108
|
|
|
92
109
|
end #FiberedPostgresConnection
|
|
@@ -98,7 +115,7 @@ module NeverBlock
|
|
|
98
115
|
@connection = connection
|
|
99
116
|
end
|
|
100
117
|
def notify_readable
|
|
101
|
-
@connection.
|
|
118
|
+
@connection.resume_command
|
|
102
119
|
end
|
|
103
120
|
end
|
|
104
121
|
|
data/neverblock.gemspec
CHANGED