I have a Ruby on Rails application which need to send HTTP request to a remote server frequently. And I want to keep those (there’re more than one clients) em-http-client globally instead of creating one client every time. But I’m not very clear about WHERE should I store that client array in?
For example, store in the session? Or, ROR has prepared something like a out-of-box singleton pattern?
I have a Ruby on Rails application which need to send HTTP request to
Share
First of all,
rubyitself hassingletonpattern ability. Check out the doc.So you need in-process storage which are persistent between request.
In
RubyOnRailsyou can use following options:metaclassThread.current[:whatever])And good idea is initialize it in
config/initializers/your_initializer.rb.I prefer third option.
Here is naive implementation what you’re looking for.