citusdata / activerecord-multi-tenant

Rails/ActiveRecord support for distributed multi-tenant databases like Postgres+Citus
MIT License
717 stars 97 forks source link

Do not dynamically load tenant model specified in autoload. #228

Open alpaca-tc opened 8 months ago

alpaca-tc commented 8 months ago

The basic policy of Rails is that application code in the development environment is lazy loaded. For this reason, class_name is passed as a string rather than a class in the reflection definition, and the string is not constantized until it is evaluated. This is done to reduce performance issues and the complex locking load of constant-loading logic.

Now activerecord-multitenant is loading the class specified in the argument to multi_tenant() without lazy loading. This conditional statement was added at https://github.com/citusdata/activerecord-multi-tenant/pull/6, but I have been fixed. Note that for projects where the tenant model does not exist, the existing behavior is supported by specifying skip_reflection: true.

alpaca-tc commented 7 months ago

Ruby can potentially cause deadlocks when loading code with multi-threading. And in our project, this deadlock was occurring. https://github.com/rails/rails/issues/50802

Part of the reason for this was that the gem was making constant load to Tenant in any multi-tenantized model. Hopefully this PR will be merged.