当我使用 ActiveRecord::Base.assessment_connection 时,设计丢失会话
环境
- Ruby 2.6
- Rails 6.0
- Devise 4.7.2
- 数据库 Postgres
当前行为
您好, 我正在使用自定义中间件连接到基于子域名的正确数据库,
module Vtenancy
class WebTenancyMiddleware
def initialize(app)
@app = app
end
def call(env)
Rails.logger.info("env #{env['SERVER_NAME']}")
if (Vtenancy::Tenant.current = Vtenancy::Tenant.tenant_from_env(env))
Vtenancy::Tenant.current.establish_connection
@app.call(env)
else
[404, {}, []]
end
end
end
end
module Vtenancy
class Tenant < Vtenancy::ApplicationRecord
def self.current
if Rails.env.test?
@current
else
RequestStore.store[:tenant]
end
end
def self.current=(tenant)
if Rails.env.test?
@current = tenant
else
RequestStore.store[:tenant] = tenant
end
end
def establish_connection
Vtenancy::Tenant.current = self
ActiveRecord::Base.establish_connection(name.to_sym)
ActiveRecord::Base.connection.active?
ActiveRecord::Base.connection.enable_query_cache!
end
end
end
我在 Warden::Manager 之前添加它
config.middleware.insert_before Warden::Manager, Vtenancy::WebTenancyMiddleware
但是,当我访问包含大量请求的页面时,有些会失败并返回 401。完全随机,
我在上使用 cookie 会话导轨。
编辑1
我对代码做了一些更改,因为我阅读了rails问题(https://github.com/rails/rails/issues/37158" rel="nofollow noreferrer">https://github.com/rails/rails/issues/37158" com/rails/rails/issues/37158)连接到数据库的将被删除。
但无论如何,使用建立连接也有同样的问题。
编辑 2
我将 puma 线程设置为 1,现在它可以工作了。 这不是我需要的解决方案,因为它使我的应用笔记成为多线程
Environment
- Ruby 2.6
- Rails 6.0
- Devise 4.7.2
- Database Postgres
Current behavior
Hi,
I'm using a custom Middleware to connect to correct database based on subdmain
module Vtenancy
class WebTenancyMiddleware
def initialize(app)
@app = app
end
def call(env)
Rails.logger.info("env #{env['SERVER_NAME']}")
if (Vtenancy::Tenant.current = Vtenancy::Tenant.tenant_from_env(env))
Vtenancy::Tenant.current.establish_connection
@app.call(env)
else
[404, {}, []]
end
end
end
end
module Vtenancy
class Tenant < Vtenancy::ApplicationRecord
def self.current
if Rails.env.test?
@current
else
RequestStore.store[:tenant]
end
end
def self.current=(tenant)
if Rails.env.test?
@current = tenant
else
RequestStore.store[:tenant] = tenant
end
end
def establish_connection
Vtenancy::Tenant.current = self
ActiveRecord::Base.establish_connection(name.to_sym)
ActiveRecord::Base.connection.active?
ActiveRecord::Base.connection.enable_query_cache!
end
end
end
I add it before Warden::Manager
config.middleware.insert_before Warden::Manager, Vtenancy::WebTenancyMiddleware
But, when I access a page with a lot of requests, some fails with 401. Totally random
I'm using cookie session on Rails.
Edit 1
I changed the code a little bit because I read from rails issues (https://github.com/rails/rails/issues/37158) that connect to with database will be removed.
But anyway, using establish_connection have the same problem.
Edit 2
I set puma threads to one and now it's working.
It's not the solution I need because It make my application note mult thread
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论