firebase_id_token icon indicating copy to clipboard operation
firebase_id_token copied to clipboard

Dynamic Project Ids

Open zeitnot opened this issue 4 years ago • 0 comments

Thanks for this great gem. It really very helpful for known projects ids. But for dynamic project id(s) this gem is not thread safe. In our case we have so much firebase projects on our database associated spesific users. As a workaround we monkey-patched the gem like the following:

module FirebaseIdToken
  def self.configuration
    Thread.current[:__fid_configuration] ||= Configuration.new
  end

  # Resets Configuration to defaults.
  def self.reset
    Thread.current[:__fid_configuration] = Configuration.new
  end
end

With this setup the following code sample is thread safe:

  pool = ConnectionPool.new(size: 100) { Redis.new url: ENV['REDIS_URL'] }

  Array(0..100).each do |project_id|
    Thread.new(project_id) do |project_id|
      pool.with do |conn|
        FirebaseIdToken.configure do |config|
          config.project_ids = [project_id]
          config.redis = conn
        end

        FirebaseIdToken::Certificates.request
        FirebaseIdToken::Signature.verify(token)
      end
    end
  end

In our use case this patch worked. If there is alternative approach, it is welcome. Otherwise I can create pull request including this monkey patched code.

zeitnot avatar Aug 13 '20 01:08 zeitnot