menuTicketing

Ticketing Ruby Library

GitHub

Repository

Example

Basic Example

The simplest flow: connect to the server, acquire a single lock, then release it right away.

ruby
broker = Broker.connect(addr)
begin
  broker.wait_ready(5)

  ticket = broker.acquire("key", wait: 5, lease: 30)
  ticket.release

  puts "pass"
ensure
  broker.close
end

Concurrency Example (Distributed Lock)

An example that verifies that all account balances eventually return to their starting value even when several threads simultaneously repeat deposits/withdrawals protected by a per-account lock.

ruby
def transaction(broker, balances, account, delta, log)
  key = "account-#{account}"
  ticket =
    begin
      broker.acquire(key, wait: WAIT, lease: LEASE)
    rescue StandardError => e
      log.error("account #{account} acquire failed: #{e}")
      return
    end

  before = balances[account]
  Thread.pass
  after = before + delta
  balances[account] = after
  ticket.release

  action, sign = delta < 0 ? ["withdraw", "-"] : ["deposit", "+"]
  log.info("account #{account} #{action} #{sign}$#{AMOUNT} -> $#{after}")
end

See the full examples in test_example.rb, test_bank_example.rb, test_bench.rb.