Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
185c93eb
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
185c93eb
编写于
2月 25, 2016
作者:
M
Matthew Draper
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use AS::Executor / AS::Reloader to support reloading in ActionCable
上级
d3c9d808
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
96 addition
and
30 deletion
+96
-30
actioncable/CHANGELOG.md
actioncable/CHANGELOG.md
+5
-0
actioncable/lib/action_cable/connection/base.rb
actioncable/lib/action_cable/connection/base.rb
+3
-2
actioncable/lib/action_cable/engine.rb
actioncable/lib/action_cable/engine.rb
+25
-0
actioncable/lib/action_cable/server/base.rb
actioncable/lib/action_cable/server/base.rb
+10
-0
actioncable/lib/action_cable/server/worker.rb
actioncable/lib/action_cable/server/worker.rb
+29
-19
actioncable/lib/action_cable/server/worker/active_record_connection_management.rb
...able/server/worker/active_record_connection_management.rb
+0
-3
actioncable/test/client_test.rb
actioncable/test/client_test.rb
+20
-0
actioncable/test/stubs/test_server.rb
actioncable/test/stubs/test_server.rb
+4
-0
railties/lib/rails/generators/rails/app/templates/config.ru
railties/lib/rails/generators/rails/app/templates/config.ru
+0
-6
未找到文件。
actioncable/CHANGELOG.md
浏览文件 @
185c93eb
*
Safely support autoloading and class unloading, by preventing concurrent
loads, and disconnecting all cables during reload.
*Matthew Draper*
*
Ensure ActionCable behaves correctly for non-string queue names.
*Jay Hayes*
...
...
actioncable/lib/action_cable/connection/base.rb
浏览文件 @
185c93eb
...
...
@@ -48,12 +48,13 @@ class Base
include
InternalChannel
include
Authorization
attr_reader
:server
,
:env
,
:subscriptions
,
:logger
delegate
:stream_event_loop
,
:
worker_pool
,
:
pubsub
,
to: :server
attr_reader
:server
,
:env
,
:subscriptions
,
:logger
,
:worker_pool
delegate
:stream_event_loop
,
:pubsub
,
to: :server
def
initialize
(
server
,
env
)
@server
,
@env
=
server
,
env
@worker_pool
=
server
.
worker_pool
@logger
=
new_tagged_logger
@websocket
=
ActionCable
::
Connection
::
WebSocket
.
new
(
env
,
self
,
stream_event_loop
)
...
...
actioncable/lib/action_cable/engine.rb
浏览文件 @
185c93eb
...
...
@@ -51,5 +51,30 @@ class Railtie < Rails::Engine # :nodoc:
end
end
end
initializer
"action_cable.set_work_hooks"
do
|
app
|
ActiveSupport
.
on_load
(
:action_cable
)
do
ActionCable
::
Server
::
Worker
.
set_callback
:work
,
:around
,
prepend:
true
do
|
_
,
inner
|
app
.
executor
.
wrap
do
# If we took a while to get the lock, we may have been halted
# in the meantime. As we haven't started doing any real work
# yet, we should pretend that we never made it off the queue.
unless
stopping?
inner
.
call
end
end
end
wrap
=
lambda
do
|
_
,
inner
|
app
.
executor
.
wrap
(
&
inner
)
end
ActionCable
::
Channel
::
Base
.
set_callback
:subscribe
,
:around
,
prepend:
true
,
&
wrap
ActionCable
::
Channel
::
Base
.
set_callback
:unsubscribe
,
:around
,
prepend:
true
,
&
wrap
app
.
reloader
.
before_class_unload
do
ActionCable
.
server
.
restart
end
end
end
end
end
actioncable/lib/action_cable/server/base.rb
浏览文件 @
185c93eb
...
...
@@ -33,6 +33,16 @@ def disconnect(identifiers)
remote_connections
.
where
(
identifiers
).
disconnect
end
def
restart
connections
.
each
(
&
:close
)
@mutex
.
synchronize
do
worker_pool
.
halt
if
@worker_pool
@worker_pool
=
nil
end
end
# Gateway to RemoteConnections. See that class for details.
def
remote_connections
@remote_connections
||
@mutex
.
synchronize
{
@remote_connections
||=
RemoteConnections
.
new
(
self
)
}
...
...
actioncable/lib/action_cable/server/worker.rb
浏览文件 @
185c93eb
...
...
@@ -20,6 +20,26 @@ def initialize(max_size: 5)
)
end
# Stop processing work: any work that has not already started
# running will be discarded from the queue
def
halt
@pool
.
kill
end
def
stopping?
@pool
.
shuttingdown?
end
def
work
(
connection
)
self
.
connection
=
connection
run_callbacks
:work
do
yield
end
ensure
self
.
connection
=
nil
end
def
async_invoke
(
receiver
,
method
,
*
args
)
@pool
.
post
do
invoke
(
receiver
,
method
,
*
args
)
...
...
@@ -27,19 +47,15 @@ def async_invoke(receiver, method, *args)
end
def
invoke
(
receiver
,
method
,
*
args
)
begin
self
.
connection
=
receiver
run_callbacks
:work
do
work
(
receiver
)
do
begin
receiver
.
send
method
,
*
args
end
rescue
Exception
=>
e
logger
.
error
"There was an exception -
#{
e
.
class
}
(
#{
e
.
message
}
)"
logger
.
error
e
.
backtrace
.
join
(
"
\n
"
)
rescue
Exception
=>
e
logger
.
error
"There was an exception -
#{
e
.
class
}
(
#{
e
.
message
}
)"
logger
.
error
e
.
backtrace
.
join
(
"
\n
"
)
receiver
.
handle_exception
if
receiver
.
respond_to?
(
:handle_exception
)
ensure
self
.
connection
=
nil
receiver
.
handle_exception
if
receiver
.
respond_to?
(
:handle_exception
)
end
end
end
...
...
@@ -50,14 +66,8 @@ def async_run_periodic_timer(channel, callback)
end
def
run_periodic_timer
(
channel
,
callback
)
begin
self
.
connection
=
channel
.
connection
run_callbacks
:work
do
callback
.
respond_to?
(
:call
)
?
channel
.
instance_exec
(
&
callback
)
:
channel
.
send
(
callback
)
end
ensure
self
.
connection
=
nil
work
(
channel
.
connection
)
do
callback
.
respond_to?
(
:call
)
?
channel
.
instance_exec
(
&
callback
)
:
channel
.
send
(
callback
)
end
end
...
...
actioncable/lib/action_cable/server/worker/active_record_connection_management.rb
浏览文件 @
185c93eb
module
ActionCable
module
Server
class
Worker
# Clear active connections between units of work so that way long-running channels or connection processes do not hoard connections.
module
ActiveRecordConnectionManagement
extend
ActiveSupport
::
Concern
...
...
@@ -13,8 +12,6 @@ module ActiveRecordConnectionManagement
def
with_database_connections
connection
.
logger
.
tag
(
ActiveRecord
::
Base
.
logger
)
{
yield
}
ensure
ActiveRecord
::
Base
.
clear_active_connections!
end
end
end
...
...
actioncable/test/client_test.rb
浏览文件 @
185c93eb
...
...
@@ -127,8 +127,16 @@ def close
end
@ws
.
close
wait_for_close
end
def
wait_for_close
@closed
.
wait
(
WAIT_WHEN_EXPECTING_EVENT
)
end
def
closed?
@closed
.
set?
end
end
def
faye_client
(
port
)
...
...
@@ -220,4 +228,16 @@ def test_unsubscribe_client
assert_equal
(
0
,
app
.
connections
.
count
)
end
end
def
test_server_restart
with_puma_server
do
|
port
|
c
=
faye_client
(
port
)
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
"{
\"
channel
\"
:
\"
EchoChannel
\"
}"
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
ActionCable
.
server
.
restart
c
.
wait_for_close
assert
c
.
closed?
end
end
end
actioncable/test/stubs/test_server.rb
浏览文件 @
185c93eb
...
...
@@ -17,4 +17,8 @@ def pubsub
def
stream_event_loop
@stream_event_loop
||=
ActionCable
::
Connection
::
StreamEventLoop
.
new
end
def
worker_pool
@worker_pool
||=
ActionCable
::
Server
::
Worker
.
new
(
max_size:
5
)
end
end
railties/lib/rails/generators/rails/app/templates/config.ru
.tt
→
railties/lib/rails/generators/rails/app/templates/config.ru
浏览文件 @
185c93eb
# This file is used by Rack-based servers to start the application.
require
::
File
.
expand_path
(
'../config/environment'
,
__FILE__
)
<%- unless options[:skip_action_cable] -%>
# Action Cable requires that all classes are loaded in advance
Rails.application.eager_load!
<%- end -%>
run
Rails
.
application
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录