kenn / redis-mutex

Distributed mutex using Redis
https://rubygems.org/gems/redis-mutex
MIT License
281 stars 44 forks source link

Include args to auto_mutex key #11

Closed bolshakov closed 10 years ago

bolshakov commented 10 years ago

Now you can optionally pass with_args: true option to auto_mutex method to include method's arguments to mutex redis key.

kenn commented 10 years ago

Can you give me a use case?

bolshakov commented 10 years ago

I'm verifying iTunes receipt with Sidekiq worker:

class ItunesVerifier
  include Sidekiq::Worker
  include Redis::Mutex::Macro
  auto_mutex :perform, block: 0

  def perform(transaction_id)
    ...
  end
end

It's ok if different receipts would be verified in the same time. But I don't want same receipts to be verified twice.

kenn commented 10 years ago

I can see the value, but the implementation lacks the flexibility of what should be included / excluded as a key, which seems too limiting to me.

For instance, it can go wrong like this:

def perform(transaction_id, options = {})
  ...
end

perform(100, {a:1,b:2})
 => "ItunesVerifier#perform:100:{:a=>1, :b=>2}" # key

I prefer a clearer API like so:

auto_mutex :perform, on: [:transaction_id]
bolshakov commented 10 years ago

Sounds reasonable. Please, check out my latest commit.

kenn commented 10 years ago

Awesome, thanks!

kenn commented 10 years ago

Just released as 3.0.0