Class: Suo::Client::Base
- Inherits:
-
Object
- Object
- Suo::Client::Base
- Includes:
- MonitorMixin
- Defined in:
- lib/suo/client/base.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ acquisition_timeout: 0.1, acquisition_delay: 0.01, stale_lock_expiration: 3600, resources: 1, ttl: 60, }.freeze
- BLANK_STR =
"".freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#key ⇒ Object
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resources ⇒ Object
Returns the value of attribute resources.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(key, options = {}) ⇒ Base
constructor
A new instance of Base.
- #lock(custom_token = nil) ⇒ Object
- #locked? ⇒ Boolean
- #locks ⇒ Object
- #refresh(token) ⇒ Object
- #unlock(token) ⇒ Object
Constructor Details
#initialize(key, options = {}) ⇒ Base
Returns a new instance of Base.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/suo/client/base.rb', line 18 def initialize(key, = {}) fail "Client required" unless [:client] = DEFAULT_OPTIONS.merge() @retry_count = ([:acquisition_timeout] / [:acquisition_delay].to_f).ceil @client = [:client] @resources = [:resources].to_i @key = key super() # initialize Monitor mixin for thread safety end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
14 15 16 |
# File 'lib/suo/client/base.rb', line 14 def client @client end |
#key ⇒ Object
Returns the value of attribute key.
14 15 16 |
# File 'lib/suo/client/base.rb', line 14 def key @key end |
#options ⇒ Object
Returns the value of attribute options.
14 15 16 |
# File 'lib/suo/client/base.rb', line 14 def end |
#resources ⇒ Object
Returns the value of attribute resources.
14 15 16 |
# File 'lib/suo/client/base.rb', line 14 def resources @resources end |
Instance Method Details
#clear ⇒ Object
88 89 90 |
# File 'lib/suo/client/base.rb', line 88 def clear fail NotImplementedError end |
#lock(custom_token = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/suo/client/base.rb', line 30 def lock(custom_token = nil) token = acquire_lock(custom_token) if block_given? && token begin yield ensure unlock(token) end else token end end |
#locked? ⇒ Boolean
44 45 46 |
# File 'lib/suo/client/base.rb', line 44 def locked? locks.size >= resources end |
#locks ⇒ Object
48 49 50 51 52 53 |
# File 'lib/suo/client/base.rb', line 48 def locks val, _ = get cleared_locks = deserialize_and_clear_locks(val) cleared_locks end |
#refresh(token) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/suo/client/base.rb', line 55 def refresh(token) retry_with_timeout do val, cas = get cas = initial_set if val.nil? cleared_locks = deserialize_and_clear_locks(val) refresh_lock(cleared_locks, token) break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?) end end |
#unlock(token) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/suo/client/base.rb', line 69 def unlock(token) return unless token retry_with_timeout do val, cas = get break if val.nil? cleared_locks = deserialize_and_clear_locks(val) acquisition_lock = remove_lock(cleared_locks, token) break unless acquisition_lock break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?) end rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions # ignore - assume success due to optimistic locking end |