# 将 Discourse 升级到 Rails 6

**URL:** <https://meta.discourse.org/t/upgrading-discourse-to-rails-6/128004>\
**Category:** Development\
**Tags:** dev-news\
**Created:** [2019 年9 月 9 日 07:48 UTC](https://meta.discourse.org/t/upgrading-discourse-to-rails-6/128004 "2019-09-09T07:48:58Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

作者： ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
发布日期： [2019 年9 月 12 日 07:36 UTC](https://meta.discourse.org/t/upgrading-discourse-to-rails-6/128004/12 "2019-09-12T07:36:59Z")

</div>

这应根据以下内容进行修复：

> <https://github.com/discourse/discourse/commit/015051ecafe24975268a4022c40f17978f54890e>
>
> This is a temporary workaround for the issue in https://github.com/rails/rails/p…ull/36949
> 
> Discussing a proper fix in Rails with the Rails team.
> 
> Prior to this fix we were spinning up a thread every time we closed a connection
> to the db.

这是 Rails 6 中新代码的结果，该代码保护对线程绑定变量的访问，以决定是否可以使用预编译语句。

在 Discourse 中，我们完全不使用预编译语句，因此此补丁并非我们所需要。

更多信息请参见：

> <https://github.com/rails/rails/pull/36949#issuecomment-530698779>
>
> @rafaelfranca / @matthewd 
> 
> There is a slight unintended consequence to this …change I will monkey patch out of Discourse.
> 
> The implementation of RubyThreadLocalVar wacks a finalizer on the object, the finalizer spins a thread.
> 
> https://github.com//blob/bbeacbcebf72668ed04df0738df7e3a654f7c177/lib/concurrent/atomic/ruby\_thread\_local\_var.rb#L101-L111
> 
> We use no prepared statements, but are now paying the penalty of spinning up a thread every time a connection is closed.
> 
> This has this impact on my graphs for the big multisites:
> 
> https://meta.discourse.org/t/upgrading-discourse-to-rails-6/128004/11?u=sam
> 
> Since once false this thing can never be turned true I will just implement an Immutable ThreadLocalVar class here for the cases where prepared statements are false. (bind anything but current value and it raises.
> 
> \`\`\`diff --git a/activerecord/lib/active\_record/connection\_adapters/abstract\_adapter.rb b/activerecord/lib/active\_record/connection\_adapters/abstract\_adapter.rb
> index 91d5d08121..970a1f9e8f 100644
> \--- a/activerecord/lib/active\_record/connection\_adapters/abstract\_adapter.rb
> +++ b/activerecord/lib/active\_record/connection\_adapters/abstract\_adapter.rb
> @@ -114,6 +114,18 @@ def self.quoted\_table\_names # :nodoc:
> @quoted\_table\_names ||= {}
> end
>  
> + class StaticThreadLocalVar
> + attr\_reader :value
> +
> + def initialize(value)
> + @value = value
> + end
> +
> + def bind(value)
> + raise "attempting to change immutable local var" if value != @value
> + yield if block\_given?
> + end
> + end
> +
> def initialize(connection, logger = nil, config = {}) # :nodoc:
> super()
>  
> @@ -132,7 +144,7 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
> @prepared\_statement\_status = Concurrent::ThreadLocalVar.new(true)
> @visitor.extend(DetermineIfPreparableVisitor)
> else
> \- @prepared\_statement\_status = Concurrent::ThreadLocalVar.new(false)
> + @prepared\_statement\_status = StaticThreadLocalVar.new(false)
> end
>  
> @advisory\_locks\_enabled = self.class.type\_cast\_config\_to\_boolean(
> \`\`\`
> 
> Thoughts? Should we patch this in Rails?

---

_[View the full topic](https://meta.discourse.org/t/upgrading-discourse-to-rails-6/128004)._
