Mangopay / mangopay2-ruby-sdk

Ruby Gem for MANGOPAY
https://rubygems.org/gems/mangopay
MIT License
42 stars 38 forks source link

Thread local MangoPay.configuration variable #58

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi,

just to let you know, I had to monkeypatch the MangoPay module so that configurations are thread-local. I did this in order to achieve multi-tenancy of MangoPay accounts in a multithreaded server (puma). Each time I receive a new request I configure MangoPay accordingly. Since each configuration lives in its own thread, I'm guaranteed each request wont mistakingly use the configuration of another request, which is the case if MangoPay.configuration is a class variable (it is shared among all threads). Here is the patch in question

MangoPay.class_eval do
  def self.configuration=(value)
    Thread.current[:mangopay_configuration] = value
  end

  def self.configuration
    Thread.current[:mangopay_configuration]
  end

  def self.configure
    config = self.configuration || MangoPay::Configuration.new
    yield config
    self.configuration = config
  end
end

Cheers

hobailey commented 8 years ago

Hey @TristeFigure, thanks very much for this detailed report. Do you think you could do a pull request with the optim to be sure I've totally understood your suggestion?

hobailey commented 8 years ago

Closing in favour of #59