The simplest flow: connect to the server, acquire a single lock, then release it right away.
broker = Broker.connect(addr)
begin
broker.wait_ready(5)
ticket = broker.acquire("key", wait: 5, lease: 30)
ticket.release
puts "pass"
ensure
broker.close
endAn 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.
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}")
endSee the full examples in test_example.rb, test_bank_example.rb, test_bench.rb.