eventmachine-maglev- 0.12.10 → 1.0.0.beta.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.yardopts +7 -0
- data/Gemfile +3 -0
- data/README.md +109 -0
- data/Rakefile +14 -368
- data/docs/DocumentationGuidesIndex.md +27 -0
- data/docs/GettingStarted.md +521 -0
- data/docs/old/DEFERRABLES +246 -0
- data/docs/{KEYBOARD → old/KEYBOARD} +15 -11
- data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
- data/docs/old/SMTP +4 -0
- data/docs/old/SPAWNED_PROCESSES +148 -0
- data/eventmachine.gemspec +20 -26
- data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
- data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
- data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
- data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
- data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
- data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
- data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
- data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
- data/examples/{ex_channel.rb → old/ex_channel.rb} +3 -3
- data/examples/old/ex_tick_loop_array.rb +15 -0
- data/examples/old/ex_tick_loop_counter.rb +32 -0
- data/ext/binder.cpp +0 -1
- data/ext/cmain.cpp +40 -29
- data/ext/ed.cpp +189 -134
- data/ext/ed.h +34 -40
- data/ext/em.cpp +388 -340
- data/ext/em.h +29 -32
- data/ext/eventmachine.h +7 -6
- data/ext/extconf.rb +57 -48
- data/ext/fastfilereader/extconf.rb +5 -3
- data/ext/fastfilereader/mapper.cpp +1 -1
- data/ext/fastfilereader/rubymain.cpp +0 -1
- data/ext/kb.cpp +1 -3
- data/ext/pipe.cpp +9 -11
- data/ext/project.h +12 -8
- data/ext/rubymain.cpp +158 -112
- data/java/src/com/rubyeventmachine/EmReactor.java +3 -2
- data/lib/em/buftok.rb +35 -63
- data/lib/em/callback.rb +43 -11
- data/lib/em/channel.rb +22 -15
- data/lib/em/completion.rb +303 -0
- data/lib/em/connection.rb +341 -208
- data/lib/em/deferrable/pool.rb +2 -0
- data/lib/em/deferrable.rb +20 -2
- data/lib/em/file_watch.rb +37 -18
- data/lib/em/iterator.rb +270 -0
- data/lib/em/pool.rb +146 -0
- data/lib/em/process_watch.rb +5 -4
- data/lib/em/processes.rb +8 -4
- data/lib/em/protocols/httpclient.rb +27 -11
- data/lib/em/protocols/httpclient2.rb +15 -5
- data/lib/em/protocols/line_protocol.rb +29 -0
- data/lib/em/protocols/memcache.rb +17 -9
- data/lib/em/protocols/object_protocol.rb +2 -1
- data/lib/em/protocols/postgres3.rb +2 -1
- data/lib/em/protocols/smtpclient.rb +19 -11
- data/lib/em/protocols/smtpserver.rb +101 -8
- data/lib/em/protocols/stomp.rb +9 -7
- data/lib/em/protocols/tcptest.rb +3 -2
- data/lib/em/protocols.rb +1 -1
- data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +188 -205
- data/lib/em/queue.rb +23 -13
- data/lib/em/resolver.rb +192 -0
- data/lib/em/spawnable.rb +9 -10
- data/lib/em/streamer.rb +34 -46
- data/lib/em/threaded_resource.rb +90 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +8 -3
- data/lib/em/version.rb +1 -1
- data/lib/eventmachine.rb +582 -686
- data/lib/jeventmachine.rb +25 -3
- data/tasks/package.rake +98 -0
- data/tasks/test.rake +8 -0
- data/tests/em_test_helper.rb +64 -0
- data/tests/test_attach.rb +56 -56
- data/tests/test_basic.rb +111 -168
- data/tests/test_channel.rb +5 -6
- data/tests/test_completion.rb +177 -0
- data/tests/test_connection_count.rb +1 -3
- data/tests/test_defer.rb +3 -32
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +27 -57
- data/tests/test_error_handler.rb +10 -7
- data/tests/test_exc.rb +6 -33
- data/tests/test_file_watch.rb +51 -35
- data/tests/test_futures.rb +10 -38
- data/tests/test_get_sock_opt.rb +27 -20
- data/tests/test_handler_check.rb +1 -3
- data/tests/test_hc.rb +49 -112
- data/tests/test_httpclient.rb +34 -62
- data/tests/test_httpclient2.rb +14 -39
- data/tests/test_inactivity_timeout.rb +44 -40
- data/tests/test_kb.rb +26 -52
- data/tests/test_ltp.rb +27 -71
- data/tests/test_ltp2.rb +1 -30
- data/tests/test_next_tick.rb +2 -31
- data/tests/test_object_protocol.rb +8 -9
- data/tests/test_pause.rb +45 -37
- data/tests/test_pending_connect_timeout.rb +42 -38
- data/tests/test_pool.rb +128 -0
- data/tests/test_process_watch.rb +37 -37
- data/tests/test_processes.rb +92 -110
- data/tests/test_proxy_connection.rb +137 -61
- data/tests/test_pure.rb +30 -67
- data/tests/test_queue.rb +10 -4
- data/tests/test_resolver.rb +55 -0
- data/tests/test_running.rb +1 -29
- data/tests/test_sasl.rb +8 -33
- data/tests/test_send_file.rb +163 -188
- data/tests/test_servers.rb +12 -55
- data/tests/test_shutdown_hooks.rb +23 -0
- data/tests/test_smtpclient.rb +1 -29
- data/tests/test_smtpserver.rb +1 -29
- data/tests/test_spawn.rb +2 -31
- data/tests/test_ssl_args.rb +9 -10
- data/tests/test_ssl_methods.rb +1 -3
- data/tests/test_ssl_verify.rb +63 -63
- data/tests/test_threaded_resource.rb +53 -0
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +52 -91
- data/tests/test_ud.rb +1 -29
- data/tests/test_unbind_reason.rb +31 -0
- metadata +113 -70
- data/README +0 -82
- data/docs/DEFERRABLES +0 -133
- data/docs/LIGHTWEIGHT_CONCURRENCY +0 -70
- data/docs/SMTP +0 -2
- data/docs/SPAWNED_PROCESSES +0 -89
- data/ext/cplusplus.cpp +0 -202
- data/ext/emwin.cpp +0 -300
- data/ext/emwin.h +0 -94
- data/ext/epoll.cpp +0 -26
- data/ext/epoll.h +0 -25
- data/ext/eventmachine_cpp.h +0 -96
- data/ext/files.cpp +0 -94
- data/ext/files.h +0 -65
- data/ext/sigs.cpp +0 -89
- data/ext/sigs.h +0 -32
- data/java/src/com/rubyeventmachine/application/Application.java +0 -194
- data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
- data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
- data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
- data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
- data/lib/evma/callback.rb +0 -32
- data/lib/evma/container.rb +0 -75
- data/lib/evma/factory.rb +0 -77
- data/lib/evma/protocol.rb +0 -87
- data/lib/evma/reactor.rb +0 -48
- data/lib/evma.rb +0 -32
- data/setup.rb +0 -1585
- data/tests/test_errors.rb +0 -82
- data/tests/testem.rb +0 -31
- data/web/whatis +0 -7
- /data/{docs/GNU → GNU} +0 -0
- /data/{docs/COPYING → LICENSE} +0 -0
- /data/docs/{ChangeLog → old/ChangeLog} +0 -0
- /data/docs/{EPOLL → old/EPOLL} +0 -0
- /data/docs/{INSTALL → old/INSTALL} +0 -0
- /data/docs/{LEGAL → old/LEGAL} +0 -0
- /data/docs/{PURE_RUBY → old/PURE_RUBY} +0 -0
- /data/docs/{RELEASE_NOTES → old/RELEASE_NOTES} +0 -0
- /data/docs/{TODO → old/TODO} +0 -0
- /data/examples/{ex_queue.rb → old/ex_queue.rb} +0 -0
- /data/examples/{helper.rb → old/helper.rb} +0 -0
data/lib/em/queue.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
1
|
module EventMachine
|
2
2
|
# A cross thread, reactor scheduled, linear queue.
|
3
3
|
#
|
4
|
-
# This class provides a simple
|
4
|
+
# This class provides a simple queue abstraction on top of the reactor
|
5
5
|
# scheduler. It services two primary purposes:
|
6
|
+
#
|
6
7
|
# * API sugar for stateful protocols
|
7
|
-
# * Pushing processing onto the
|
8
|
+
# * Pushing processing onto the reactor thread
|
8
9
|
#
|
9
|
-
#
|
10
|
+
# @example
|
10
11
|
#
|
11
12
|
# q = EM::Queue.new
|
12
13
|
# q.push('one', 'two', 'three')
|
13
14
|
# 3.times do
|
14
|
-
# q.pop{ |msg| puts(msg) }
|
15
|
+
# q.pop { |msg| puts(msg) }
|
15
16
|
# end
|
16
17
|
#
|
17
18
|
class Queue
|
18
|
-
# Create a new queue
|
19
19
|
def initialize
|
20
20
|
@items = []
|
21
21
|
@popq = []
|
22
22
|
end
|
23
23
|
|
24
24
|
# Pop items off the queue, running the block on the reactor thread. The pop
|
25
|
-
# will not happen immediately, but at some point in the future, either in
|
25
|
+
# will not happen immediately, but at some point in the future, either in
|
26
26
|
# the next tick, if the queue has data, or when the queue is populated.
|
27
|
+
#
|
28
|
+
# @return [NilClass] nil
|
27
29
|
def pop(*a, &b)
|
28
30
|
cb = EM::Callback(*a, &b)
|
29
31
|
EM.schedule do
|
@@ -37,7 +39,7 @@ module EventMachine
|
|
37
39
|
end
|
38
40
|
|
39
41
|
# Push items onto the queue in the reactor thread. The items will not appear
|
40
|
-
# in the queue immediately, but will be scheduled for addition during the
|
42
|
+
# in the queue immediately, but will be scheduled for addition during the
|
41
43
|
# next reactor tick.
|
42
44
|
def push(*items)
|
43
45
|
EM.schedule do
|
@@ -45,17 +47,25 @@ module EventMachine
|
|
45
47
|
@popq.shift.call @items.shift until @items.empty? || @popq.empty?
|
46
48
|
end
|
47
49
|
end
|
50
|
+
alias :<< :push
|
48
51
|
|
49
|
-
#
|
50
|
-
# accuracy.
|
52
|
+
# @return [Boolean]
|
53
|
+
# @note This is a peek, it's not thread safe, and may only tend toward accuracy.
|
51
54
|
def empty?
|
52
55
|
@items.empty?
|
53
56
|
end
|
54
57
|
|
55
|
-
#
|
56
|
-
# accuracy.
|
58
|
+
# @return [Integer] Queue size
|
59
|
+
# @note This is a peek, it's not thread safe, and may only tend toward accuracy.
|
57
60
|
def size
|
58
61
|
@items.size
|
59
62
|
end
|
60
|
-
|
61
|
-
|
63
|
+
|
64
|
+
# @return [Integer] Waiting size
|
65
|
+
# @note This is a peek at the number of jobs that are currently waiting on the Queue
|
66
|
+
def num_waiting
|
67
|
+
@popq.size
|
68
|
+
end
|
69
|
+
|
70
|
+
end # Queue
|
71
|
+
end # EventMachine
|
data/lib/em/resolver.rb
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
module EventMachine
|
2
|
+
module DNS
|
3
|
+
class Resolver
|
4
|
+
|
5
|
+
def self.resolve(hostname)
|
6
|
+
Request.new(socket, hostname)
|
7
|
+
end
|
8
|
+
|
9
|
+
@socket = @nameservers = nil
|
10
|
+
|
11
|
+
def self.socket
|
12
|
+
if !@socket || (@socket && @socket.error?)
|
13
|
+
@socket = Socket.open
|
14
|
+
|
15
|
+
@hosts = {}
|
16
|
+
IO.readlines('/etc/hosts').each do |line|
|
17
|
+
next if line =~ /^#/
|
18
|
+
addr, host = line.split(/\s+/)
|
19
|
+
|
20
|
+
if @hosts[host]
|
21
|
+
@hosts[host] << addr
|
22
|
+
else
|
23
|
+
@hosts[host] = [addr]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
@socket
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.nameservers=(ns)
|
32
|
+
@nameservers = ns
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.nameservers
|
36
|
+
if !@nameservers
|
37
|
+
@nameservers = []
|
38
|
+
IO.readlines('/etc/resolv.conf').each do |line|
|
39
|
+
if line =~ /^nameserver (.+)$/
|
40
|
+
@nameservers << $1.split(/\s+/).first
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
@nameservers
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.nameserver
|
48
|
+
nameservers.shuffle.first
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.hosts
|
52
|
+
@hosts
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class RequestIdAlreadyUsed < RuntimeError; end
|
57
|
+
|
58
|
+
class Socket < EventMachine::Connection
|
59
|
+
def self.open
|
60
|
+
EventMachine::open_datagram_socket('0.0.0.0', 0, self)
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize
|
64
|
+
@nameserver = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def post_init
|
68
|
+
@requests = {}
|
69
|
+
EM.add_periodic_timer(0.1, &method(:tick))
|
70
|
+
end
|
71
|
+
|
72
|
+
def unbind
|
73
|
+
end
|
74
|
+
|
75
|
+
def tick
|
76
|
+
@requests.each do |id,req|
|
77
|
+
req.tick
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def register_request(id, req)
|
82
|
+
if @requests.has_key?(id)
|
83
|
+
raise RequestIdAlreadyUsed
|
84
|
+
else
|
85
|
+
@requests[id] = req
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def send_packet(pkt)
|
90
|
+
send_datagram(pkt, nameserver, 53)
|
91
|
+
end
|
92
|
+
|
93
|
+
def nameserver=(ns)
|
94
|
+
@nameserver = ns
|
95
|
+
end
|
96
|
+
|
97
|
+
def nameserver
|
98
|
+
@nameserver || Resolver.nameserver
|
99
|
+
end
|
100
|
+
|
101
|
+
# Decodes the packet, looks for the request and passes the
|
102
|
+
# response over to the requester
|
103
|
+
def receive_data(data)
|
104
|
+
msg = nil
|
105
|
+
begin
|
106
|
+
msg = Resolv::DNS::Message.decode data
|
107
|
+
rescue
|
108
|
+
else
|
109
|
+
req = @requests[msg.id]
|
110
|
+
if req
|
111
|
+
@requests.delete(msg.id)
|
112
|
+
req.receive_answer(msg)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Request
|
119
|
+
include Deferrable
|
120
|
+
attr_accessor :retry_interval, :max_tries
|
121
|
+
|
122
|
+
def initialize(socket, hostname)
|
123
|
+
@socket = socket
|
124
|
+
@hostname = hostname
|
125
|
+
@tries = 0
|
126
|
+
@last_send = Time.at(0)
|
127
|
+
@retry_interval = 3
|
128
|
+
@max_tries = 5
|
129
|
+
|
130
|
+
if addrs = Resolver.hosts[hostname]
|
131
|
+
succeed addrs
|
132
|
+
else
|
133
|
+
EM.next_tick { tick }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def tick
|
138
|
+
# Break early if nothing to do
|
139
|
+
return if @last_send + @retry_interval > Time.now
|
140
|
+
if @tries < @max_tries
|
141
|
+
send
|
142
|
+
else
|
143
|
+
fail 'retries exceeded'
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def receive_answer(msg)
|
148
|
+
addrs = []
|
149
|
+
msg.each_answer do |name,ttl,data|
|
150
|
+
if data.kind_of?(Resolv::DNS::Resource::IN::A) ||
|
151
|
+
data.kind_of?(Resolv::DNS::Resource::IN::AAAA)
|
152
|
+
addrs << data.address.to_s
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
if addrs.empty?
|
157
|
+
fail "rcode=#{msg.rcode}"
|
158
|
+
else
|
159
|
+
succeed addrs
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def send
|
166
|
+
@tries += 1
|
167
|
+
@last_send = Time.now
|
168
|
+
@socket.send_packet(packet.encode)
|
169
|
+
end
|
170
|
+
|
171
|
+
def id
|
172
|
+
begin
|
173
|
+
@id = rand(65535)
|
174
|
+
@socket.register_request(@id, self)
|
175
|
+
rescue RequestIdAlreadyUsed
|
176
|
+
retry
|
177
|
+
end unless defined?(@id)
|
178
|
+
|
179
|
+
@id
|
180
|
+
end
|
181
|
+
|
182
|
+
def packet
|
183
|
+
msg = Resolv::DNS::Message.new
|
184
|
+
msg.id = id
|
185
|
+
msg.rd = 1
|
186
|
+
msg.add_question @hostname, Resolv::DNS::Resource::IN::A
|
187
|
+
msg
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
data/lib/em/spawnable.rb
CHANGED
@@ -45,21 +45,18 @@ module EventMachine
|
|
45
45
|
end
|
46
46
|
alias_method :resume, :notify
|
47
47
|
alias_method :run, :notify # for formulations like (EM.spawn {xxx}).run
|
48
|
-
#attr_accessor :receiver
|
49
48
|
|
50
|
-
#--
|
51
|
-
# I know I'm missing something stupid, but the inside of class << s
|
52
|
-
# can't see locally-bound values. It can see globals, though.
|
53
49
|
def set_receiver blk
|
54
|
-
|
55
|
-
|
56
|
-
define_method :call,
|
50
|
+
(class << self ; self ; end).class_eval do
|
51
|
+
remove_method :call if method_defined? :call
|
52
|
+
define_method :call, blk
|
57
53
|
end
|
58
54
|
end
|
59
55
|
|
60
56
|
end
|
61
57
|
|
62
|
-
|
58
|
+
# @private
|
59
|
+
class YieldBlockFromSpawnedProcess
|
63
60
|
def initialize block, notify
|
64
61
|
@block = [block,notify]
|
65
62
|
end
|
@@ -75,11 +72,13 @@ module EventMachine
|
|
75
72
|
s
|
76
73
|
end
|
77
74
|
|
78
|
-
|
75
|
+
# @private
|
76
|
+
def self.yield &block
|
79
77
|
return YieldBlockFromSpawnedProcess.new( block, false )
|
80
78
|
end
|
81
79
|
|
82
|
-
|
80
|
+
# @private
|
81
|
+
def self.yield_and_notify &block
|
83
82
|
return YieldBlockFromSpawnedProcess.new( block, true )
|
84
83
|
end
|
85
84
|
end
|
data/lib/em/streamer.rb
CHANGED
@@ -1,30 +1,24 @@
|
|
1
|
-
#--
|
2
|
-
#
|
3
|
-
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
-
# Homepage:: http://rubyeventmachine.com
|
5
|
-
# Date:: 16 Jul 2006
|
6
|
-
#
|
7
|
-
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
-
# usage examples.
|
9
|
-
#
|
10
|
-
#----------------------------------------------------------------------------
|
11
|
-
#
|
12
|
-
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
-
# Gmail: blackhedd
|
14
|
-
#
|
15
|
-
# This program is free software; you can redistribute it and/or modify
|
16
|
-
# it under the terms of either: 1) the GNU General Public License
|
17
|
-
# as published by the Free Software Foundation; either version 2 of the
|
18
|
-
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
-
#
|
20
|
-
# See the file COPYING for complete licensing information.
|
21
|
-
#
|
22
|
-
#---------------------------------------------------------------------------
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
|
27
1
|
module EventMachine
|
2
|
+
# Streams a file over a given connection. Streaming begins once the object is
|
3
|
+
# instantiated. Typically FileStreamer instances are not reused.
|
4
|
+
#
|
5
|
+
# Streaming uses buffering for files larger than 16K and uses so-called fast file reader (a C++ extension)
|
6
|
+
# if available (it is part of eventmachine gem itself).
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# module FileSender
|
11
|
+
# def post_init
|
12
|
+
# streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
|
13
|
+
# streamer.callback{
|
14
|
+
# # file was sent successfully
|
15
|
+
# close_connection_after_writing
|
16
|
+
# }
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# @author Francis Cianfrocca
|
28
22
|
class FileStreamer
|
29
23
|
include Deferrable
|
30
24
|
|
@@ -35,25 +29,16 @@ module EventMachine
|
|
35
29
|
# Send 16k chunks at a time
|
36
30
|
ChunkSize = 16384
|
37
31
|
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
# module FileSender
|
42
|
-
# def post_init
|
43
|
-
# streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
|
44
|
-
# streamer.callback{
|
45
|
-
# # file was sent successfully
|
46
|
-
# close_connection_after_writing
|
47
|
-
# }
|
48
|
-
# end
|
49
|
-
# end
|
32
|
+
# @param [EventMachine::Connection] connection
|
33
|
+
# @param [String] filename File path
|
50
34
|
#
|
35
|
+
# @option args [Boolean] :http_chunks (false) Use HTTP 1.1 style chunked-encoding semantics.
|
51
36
|
def initialize connection, filename, args = {}
|
52
37
|
@connection = connection
|
53
38
|
@http_chunks = args[:http_chunks]
|
54
39
|
|
55
40
|
if File.exist?(filename)
|
56
|
-
@size = File.size
|
41
|
+
@size = File.size(filename)
|
57
42
|
if @size <= MappingThreshold
|
58
43
|
stream_without_mapping filename
|
59
44
|
else
|
@@ -64,7 +49,8 @@ module EventMachine
|
|
64
49
|
end
|
65
50
|
end
|
66
51
|
|
67
|
-
|
52
|
+
# @private
|
53
|
+
def stream_without_mapping filename
|
68
54
|
if @http_chunks
|
69
55
|
@connection.send_data "#{@size.to_s(16)}\r\n"
|
70
56
|
@connection.send_file_data filename
|
@@ -76,7 +62,8 @@ module EventMachine
|
|
76
62
|
end
|
77
63
|
private :stream_without_mapping
|
78
64
|
|
79
|
-
|
65
|
+
# @private
|
66
|
+
def stream_with_mapping filename
|
80
67
|
ensure_mapping_extension_is_present
|
81
68
|
|
82
69
|
@position = 0
|
@@ -86,6 +73,7 @@ module EventMachine
|
|
86
73
|
private :stream_with_mapping
|
87
74
|
|
88
75
|
# Used internally to stream one chunk at a time over multiple reactor ticks
|
76
|
+
# @private
|
89
77
|
def stream_one_chunk
|
90
78
|
loop {
|
91
79
|
if @position < @size
|
@@ -111,7 +99,7 @@ module EventMachine
|
|
111
99
|
}
|
112
100
|
end
|
113
101
|
|
114
|
-
|
102
|
+
#
|
115
103
|
# We use an outboard extension class to get memory-mapped files.
|
116
104
|
# It's outboard to avoid polluting the core distro, but that means
|
117
105
|
# there's a "hidden" dependency on it. The first time we get here in
|
@@ -120,11 +108,11 @@ module EventMachine
|
|
120
108
|
# mapped files will work fine without it. This is a somewhat difficult
|
121
109
|
# compromise between usability and proper modularization.
|
122
110
|
#
|
123
|
-
|
111
|
+
# @private
|
112
|
+
def ensure_mapping_extension_is_present
|
124
113
|
@@fastfilereader ||= (require 'fastfilereaderext')
|
125
114
|
end
|
126
115
|
private :ensure_mapping_extension_is_present
|
127
116
|
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
117
|
+
end # FileStreamer
|
118
|
+
end # EventMachine
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module EventMachine
|
2
|
+
# = EventMachine::ThreadedResource
|
3
|
+
#
|
4
|
+
# A threaded resource is a "quick and dirty" wrapper around the concept of
|
5
|
+
# wiring up synchronous code into a standard EM::Pool. This is useful to keep
|
6
|
+
# interfaces coherent and provide a simple approach at "making an interface
|
7
|
+
# async-ish".
|
8
|
+
#
|
9
|
+
# General usage is to wrap libraries that do not support EventMachine, or to
|
10
|
+
# have a specific number of dedicated high-cpu worker resources.
|
11
|
+
#
|
12
|
+
# == Basic Usage example
|
13
|
+
#
|
14
|
+
# This example requires the cassandra gem. The cassandra gem contains an
|
15
|
+
# EventMachine interface, but it's sadly Fiber based and thus only works on
|
16
|
+
# 1.9. It also requires (potentially) complex stack switching logic to reach
|
17
|
+
# completion of nested operations. By contrast this approach provides a block
|
18
|
+
# in which normal synchronous code can occur, but makes no attempt to wire the
|
19
|
+
# IO into EventMachines C++ IO implementations, instead relying on the reactor
|
20
|
+
# pattern in rb_thread_select.
|
21
|
+
#
|
22
|
+
# cassandra_dispatcher = ThreadedResource.new do
|
23
|
+
# Cassandra.new('allthethings', '127.0.0.1:9160')
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# pool = EM::Pool.new
|
27
|
+
#
|
28
|
+
# pool.add cassandra_dispatcher
|
29
|
+
#
|
30
|
+
# # If we don't care about the result:
|
31
|
+
# pool.perform do |dispatcher|
|
32
|
+
# # The following blcok executes inside a dedicated thread, and should not
|
33
|
+
# # access EventMachine things:
|
34
|
+
# dispatcher.dispatch do |cassandra|
|
35
|
+
# cassandra.insert(:Things, '10', 'stuff' => 'things')
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # Example where we care about the result:
|
40
|
+
# pool.perform do |dispatcher|
|
41
|
+
# # The dispatch block is executed in the resources thread.
|
42
|
+
# completion = dispatcher.dispatch do |cassandra|
|
43
|
+
# cassandra.get(:Things, '10', 'stuff')
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # This block will be yielded on the EM thread:
|
47
|
+
# completion.callback do |result|
|
48
|
+
# EM.do_something_with(result)
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# completion
|
52
|
+
# end
|
53
|
+
class ThreadedResource
|
54
|
+
|
55
|
+
# The block should return the resource that will be yielded in a dispatch.
|
56
|
+
def initialize
|
57
|
+
@resource = yield
|
58
|
+
|
59
|
+
@running = true
|
60
|
+
@queue = ::Queue.new
|
61
|
+
@thread = Thread.new do
|
62
|
+
@queue.pop.call while @running
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Called on the EM thread, generally in a perform block to return a
|
67
|
+
# completion for the work.
|
68
|
+
def dispatch
|
69
|
+
completion = EM::Completion.new
|
70
|
+
@queue << lambda do
|
71
|
+
begin
|
72
|
+
result = yield @resource
|
73
|
+
completion.succeed result
|
74
|
+
rescue Exception => e
|
75
|
+
completion.fail e
|
76
|
+
end
|
77
|
+
end
|
78
|
+
completion
|
79
|
+
end
|
80
|
+
|
81
|
+
# Kill the internal thread. should only be used to cleanup - generally
|
82
|
+
# only required for tests.
|
83
|
+
def shutdown
|
84
|
+
@running = false
|
85
|
+
@queue << lambda {}
|
86
|
+
@thread.join
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
data/lib/em/tick_loop.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module EventMachine
|
2
|
+
# Creates and immediately starts an EventMachine::TickLoop
|
3
|
+
def self.tick_loop(*a, &b)
|
4
|
+
TickLoop.new(*a, &b).start
|
5
|
+
end
|
6
|
+
|
7
|
+
# A TickLoop is useful when one needs to distribute amounts of work
|
8
|
+
# throughout ticks in order to maintain response times. It is also useful for
|
9
|
+
# simple repeated checks and metrics.
|
10
|
+
#
|
11
|
+
# # Here we run through an array one item per tick until it is empty,
|
12
|
+
# # printing each element.
|
13
|
+
# # When the array is empty, we return :stop from the callback, and the
|
14
|
+
# # loop will terminate.
|
15
|
+
# # When the loop terminates, the on_stop callbacks will be called.
|
16
|
+
# EM.run do
|
17
|
+
# array = (1..100).to_a
|
18
|
+
#
|
19
|
+
# tickloop = EM.tick_loop do
|
20
|
+
# if array.empty?
|
21
|
+
# :stop
|
22
|
+
# else
|
23
|
+
# puts array.shift
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# tickloop.on_stop { EM.stop }
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
class TickLoop
|
31
|
+
|
32
|
+
# Arguments: A callback (EM::Callback) to call each tick. If the call
|
33
|
+
# returns +:stop+ then the loop will be stopped. Any other value is
|
34
|
+
# ignored.
|
35
|
+
def initialize(*a, &b)
|
36
|
+
@work = EM::Callback(*a, &b)
|
37
|
+
@stops = []
|
38
|
+
@stopped = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# Arguments: A callback (EM::Callback) to call once on the next stop (or
|
42
|
+
# immediately if already stopped).
|
43
|
+
def on_stop(*a, &b)
|
44
|
+
if @stopped
|
45
|
+
EM::Callback(*a, &b).call
|
46
|
+
else
|
47
|
+
@stops << EM::Callback(*a, &b)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Stop the tick loop immediately, and call it's on_stop callbacks.
|
52
|
+
def stop
|
53
|
+
@stopped = true
|
54
|
+
until @stops.empty?
|
55
|
+
@stops.shift.call
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Query if the loop is stopped.
|
60
|
+
def stopped?
|
61
|
+
@stopped
|
62
|
+
end
|
63
|
+
|
64
|
+
# Start the tick loop, will raise argument error if the loop is already
|
65
|
+
# running.
|
66
|
+
def start
|
67
|
+
raise ArgumentError, "double start" unless @stopped
|
68
|
+
@stopped = false
|
69
|
+
schedule
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
def schedule
|
74
|
+
EM.next_tick do
|
75
|
+
next if @stopped
|
76
|
+
if @work.call == :stop
|
77
|
+
stop
|
78
|
+
else
|
79
|
+
schedule
|
80
|
+
end
|
81
|
+
end
|
82
|
+
self
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/em/timers.rb
CHANGED
@@ -20,6 +20,7 @@ module EventMachine
|
|
20
20
|
|
21
21
|
# Creates a periodic timer
|
22
22
|
#
|
23
|
+
# @example
|
23
24
|
# n = 0
|
24
25
|
# timer = EventMachine::PeriodicTimer.new(5) do
|
25
26
|
# puts "the time is #{Time.now}"
|
@@ -32,6 +33,7 @@ module EventMachine
|
|
32
33
|
@interval = interval
|
33
34
|
@code = callback || block
|
34
35
|
@cancelled = false
|
36
|
+
@work = method(:fire)
|
35
37
|
schedule
|
36
38
|
end
|
37
39
|
|
@@ -43,10 +45,13 @@ module EventMachine
|
|
43
45
|
# Fire the timer every interval seconds
|
44
46
|
attr_accessor :interval
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
+
# @private
|
49
|
+
def schedule
|
50
|
+
EventMachine::add_timer @interval, @work
|
48
51
|
end
|
49
|
-
|
52
|
+
|
53
|
+
# @private
|
54
|
+
def fire
|
50
55
|
unless @cancelled
|
51
56
|
@code.call
|
52
57
|
schedule
|
data/lib/em/version.rb
CHANGED