简体   繁体   中英

Ruby why are class instance variables threadsafe

According to this answer they are, but then the poster states that things work differently in JRuby so I am confused?

I am implementing a multi tenancy solution using class instance variables, so it doesn't matter what Ruby implementation or web server I am using, I need to make sure that data cannot be leaked.

Here is my code:

class Tenant < ActiveRecord::Base

  def self.current_tenant=(tenant)
    @tenant = tenant
  end

  def self.current_tenant
    @tenant
  end
end

What do I need to do to make sure that no matter what happens (changing Ruby implementation, changing web server, new Ruby threading capabilities etc) that my code is thread safe?

Since the tenancy attribute's scope is a request, I would suggest you keep it in the scope of the current thread . Since a request is handled on a single thread, and a thread handles a single request at a time - as long as you always set the tenancy at the beginning of the request you will be fine (for extra security, you might want to un-assign the tenant at the end of the request).

To do this you can use thread local attributes:

class Tenant < ActiveRecord::Base

  def self.current_tenant=(tenant)
    Thread.current[:current_tenant] = tenant
  end

  def self.current_tenant
    Thread.current[:current_tenant]
  end

  def self.clear_current_tenant
    Thread.current[:current_tenant] = nil
  end
end

Since this is using a thread store, you are totally thread safe - each thread is responsible for its own data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM