Class: Ticketing::Ticket

Inherits:
Object
  • Object
show all
Defined in:
lib/ticketing/broker.rb

Overview

An acquired lock guard.

The lock is released when #release is called, or in the background when the ticket is #closed. If the client dies before releasing, the server's lease eventually frees it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(broker, conn, key, token, logger) ⇒ Ticket

Returns a new instance of Ticket.



176
177
178
179
180
181
182
183
184
# File 'lib/ticketing/broker.rb', line 176

def initialize(broker, conn, key, token, logger)
  @broker = broker
  @conn = conn
  @key = key
  @token = token
  @logger = logger
  @released = false
  @mutex = Mutex.new
end

Instance Attribute Details

#keyObject (readonly)

The locked key.



169
170
171
# File 'lib/ticketing/broker.rb', line 169

def key
  @key
end

#tokenObject (readonly)

The fencing token for this grant — a value that strictly increases with every acquisition of any key. Pass it to the resource you protect and reject stale (lower) tokens.



174
175
176
# File 'lib/ticketing/broker.rb', line 174

def token
  @token
end

Instance Method Details

#closeObject

Releases the lock in the background if it was not already released.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ticketing/broker.rb', line 194

def close
  should_release =
    @mutex.synchronize do
      next false if @released

      @released = true
      true
    end
  return unless should_release

  Thread.new do
    begin
      @broker.release_lock(@conn, @key)
    rescue StandardError
      @logger.error("auto-release #{@key} failed — lease will expire it")
    end
  end
  nil
end

#releaseObject

Releases the lock. Returns true if the server still held it, false if it had already expired.



188
189
190
191
# File 'lib/ticketing/broker.rb', line 188

def release
  @mutex.synchronize { @released = true }
  @broker.release_lock(@conn, @key)
end