Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
85319e37
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,体验更适合开发者的 AI 搜索 >>
提交
85319e37
编写于
8月 06, 2019
作者:
E
Eileen M. Uchitelle
提交者:
eileencodes
8月 06, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #36868 from eileencodes/make-prevent-writes-threadsafe
Fix thread safety of prevent_writes
上级
fb716a65
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
49 addition
and
7 deletion
+49
-7
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
...ve_record/connection_adapters/abstract/connection_pool.rb
+10
-5
activerecord/test/cases/database_selector_test.rb
activerecord/test/cases/database_selector_test.rb
+39
-2
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
浏览文件 @
85319e37
...
...
@@ -1005,28 +1005,33 @@ def self.discard_unowned_pools(pid_map) # :nodoc:
end
end
attr_reader
:prevent_writes
def
initialize
# These caches are keyed by spec.name (ConnectionSpecification#name).
@owner_to_pool
=
ConnectionHandler
.
create_owner_to_pool
@prevent_writes
=
false
# Backup finalizer: if the forked child never needed a pool, the above
# early discard has not occurred
ObjectSpace
.
define_finalizer
self
,
ConnectionHandler
.
unowned_pool_finalizer
(
@owner_to_pool
)
end
def
prevent_writes
# :nodoc:
Thread
.
current
[
:prevent_writes
]
end
def
prevent_writes
=
(
prevent_writes
)
# :nodoc:
Thread
.
current
[
:prevent_writes
]
=
prevent_writes
end
# Prevent writing to the database regardless of role.
#
# In some cases you may want to prevent writes to the database
# even if you are on a database that can write. `while_preventing_writes`
# will prevent writes to the database for the duration of the block.
def
while_preventing_writes
(
enabled
=
true
)
original
,
@prevent_writes
=
@
prevent_writes
,
enabled
original
,
self
.
prevent_writes
=
self
.
prevent_writes
,
enabled
yield
ensure
@
prevent_writes
=
original
self
.
prevent_writes
=
original
end
def
connection_pool_list
...
...
activerecord/test/cases/database_selector_test.rb
浏览文件 @
85319e37
...
...
@@ -143,12 +143,12 @@ def test_preventing_writes_turns_off_for_primary_write
write
=
false
resolver
.
read
do
assert
ActiveRecord
::
Base
.
connected_to?
(
role: :writing
)
assert
ActiveRecord
::
Base
.
connection_handler
.
prevent_writes
assert
_predicate
ActiveRecord
::
Base
.
connection
,
:preventing_writes?
read
=
true
resolver
.
write
do
assert
ActiveRecord
::
Base
.
connected_to?
(
role: :writing
)
assert_not
ActiveRecord
::
Base
.
connection_handler
.
prevent_writes
assert_not
_predicate
ActiveRecord
::
Base
.
connection
,
:preventing_writes?
write
=
true
end
end
...
...
@@ -157,6 +157,43 @@ def test_preventing_writes_turns_off_for_primary_write
assert
read
end
def
test_preventing_writes_works_in_a_threaded_environment
resolver
=
ActiveRecord
::
Middleware
::
DatabaseSelector
::
Resolver
.
new
(
@session
,
delay:
5
.
seconds
)
inside_preventing
=
Concurrent
::
Event
.
new
finished_checking
=
Concurrent
::
Event
.
new
@session
.
update_last_write_timestamp
t1
=
Thread
.
new
do
resolver
.
read
do
inside_preventing
.
wait
assert
ActiveRecord
::
Base
.
connected_to?
(
role: :writing
)
assert_predicate
ActiveRecord
::
Base
.
connection
,
:preventing_writes?
finished_checking
.
set
end
end
t2
=
Thread
.
new
do
resolver
.
write
do
assert
ActiveRecord
::
Base
.
connected_to?
(
role: :writing
)
assert_not_predicate
ActiveRecord
::
Base
.
connection
,
:preventing_writes?
inside_preventing
.
set
finished_checking
.
wait
end
end
t3
=
Thread
.
new
do
resolver
.
read
do
assert
ActiveRecord
::
Base
.
connected_to?
(
role: :writing
)
assert_predicate
ActiveRecord
::
Base
.
connection
,
:preventing_writes?
end
end
t1
.
join
t2
.
join
t3
.
join
end
def
test_read_from_replica_with_no_delay
resolver
=
ActiveRecord
::
Middleware
::
DatabaseSelector
::
Resolver
.
new
(
@session
,
delay:
0
.
seconds
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录