Class: TransactionEventStoreMongoid::Locker
- Inherits:
-
Object
- Object
- TransactionEventStoreMongoid::Locker
- Defined in:
- lib/transaction_event_store_mongoid/locker.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#retry_interval ⇒ Object
readonly
Returns the value of attribute retry_interval.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#initialize(timeout: 10, retry_interval: 0.1, adapter: ::TransactionEventStoreMongoid::Lock.new) ⇒ Locker
constructor
A new instance of Locker.
- #with_lock(stream, &block) ⇒ Object
Constructor Details
#initialize(timeout: 10, retry_interval: 0.1, adapter: ::TransactionEventStoreMongoid::Lock.new) ⇒ Locker
Returns a new instance of Locker.
7 8 9 10 11 |
# File 'lib/transaction_event_store_mongoid/locker.rb', line 7 def initialize(timeout: 10, retry_interval: 0.1, adapter: ::TransactionEventStoreMongoid::Lock.new) @timeout = timeout @retry_interval = retry_interval @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
5 6 7 |
# File 'lib/transaction_event_store_mongoid/locker.rb', line 5 def adapter @adapter end |
#retry_interval ⇒ Object (readonly)
Returns the value of attribute retry_interval.
5 6 7 |
# File 'lib/transaction_event_store_mongoid/locker.rb', line 5 def retry_interval @retry_interval end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/transaction_event_store_mongoid/locker.rb', line 5 def timeout @timeout end |
Instance Method Details
#with_lock(stream, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/transaction_event_store_mongoid/locker.rb', line 13 def with_lock(stream, &block) begin start = Time.now adapter.with_lock(stream, &block) rescue TransactionEventStore::CannotObtainLock if (Time.now - start) < timeout sleep(retry_interval) retry else raise end end end |