Class: Suo::Client::Base

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/suo/client/base.rb

Direct Known Subclasses

Memcached, Redis

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

Instance Method Summary collapse

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, options = {})
  fail "Client required" unless options[:client]

  @options = DEFAULT_OPTIONS.merge(options)
  @retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
  @client = @options[:client]
  @resources = @options[:resources].to_i
  @key = key

  super() # initialize Monitor mixin for thread safety
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/suo/client/base.rb', line 14

def client
  @client
end

#keyObject

Returns the value of attribute key.



14
15
16
# File 'lib/suo/client/base.rb', line 14

def key
  @key
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/suo/client/base.rb', line 14

def options
  @options
end

#resourcesObject

Returns the value of attribute resources.



14
15
16
# File 'lib/suo/client/base.rb', line 14

def resources
  @resources
end

Instance Method Details

#clearObject



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

Returns:

  • (Boolean)


44
45
46
# File 'lib/suo/client/base.rb', line 44

def locked?
  locks.size >= resources
end

#locksObject



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