我创建了小型API库,一切正常,直到我意识到,我需要多个配置。
看起来像这样:
module Store
class Api
class << self
attr_accessor :configuration
def configure
self.configuration ||= Configuration.new
yield configuration
end
def get options = {}
url = "#{configuration.domain}/#{options[:resource]}"
# ResClient url ...
end
end
end
class Configuration
attr_accessor :domain
def initialize options = {}
@domain = options[:domain]
end
end
class Product
def self.get
sleep 5
Api.get resource: 'products'
end
end
end
当我同时运行它时,它会覆盖模块配置。
Thread.new do
10.times do
Store::Api.configure do |c|
c.domain = "apple2.com"
end
p Store::Product.get
end
end
10.times do
Store::Api.configure do |c|
c.domain = "apple.com"
end
p Store::Product.get
end
我无法弄清楚,如何使这个模块更好。谢谢你的建议
答案 0 :(得分:1)
好吧,如果你不想让多个线程竞争一个资源,你就不应该让它成为一个单例。尝试将对象配置从类移动到其实例,然后单独实例化和配置它们。
这里还有更多重构,但这解决了你的问题:
module Store
class API
attr_reader :domain
def initialize(options = {})
@domain = options[:domain]
end
def products
sleep 5
get resource: 'products'
end
private
def get(options = {})
url = "#{configuration.domain}/#{options[:resource]}"
# ResClient url ...
end
end
end
Thread.new do
10.times do
api = Store::API.new(domain: 'apple2.com')
p api.products
end
end
10.times do
api = Store::API.new(domain: 'apple.com')
p api.products
end