menuTicketing
close





Ticketing Ruby लाइब्रेरी

GitHub

रिपोजिटरी

उदाहरण

सरल उदाहरण

सर्वर से जुड़कर एक लॉक प्राप्त करने के बाद तुरंत छोड़ने का सबसे सरल प्रवाह है।

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

समसामयिकता उदाहरण (वितरित लॉक)

कई थ्रेड एक साथ खाता-वार लॉक से सुरक्षित रहते हुए जमा/निकासी दोहराने पर भी, सभी खातों की शेष राशि अंततः शुरुआती मान पर वापस आती है या नहीं, यह जाँचने का उदाहरण है।

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

पूरा उदाहरण test_example.rb, test_bank_example.rb, test_bench.rb में देखें।