menuTicketing

Ticketing Go Library

GitHub

Repository

Example

Basic Example

A minimal example that connects to a single server, acquires a key, and releases it right away. Since Go has no async/await, it's offered as a blocking API combined with a background goroutine.

go
wait := 5 * time.Second
lease := 30 * time.Second

broker := Connect("127.0.0.1:5225")
defer broker.Close()
broker.WaitReady(5 * time.Second)

ticket, err := broker.Acquire("key", wait, lease)
if err != nil {
    log.Fatalf("acquire: %v", err)
}
if _, err := ticket.Release(); err != nil {
    log.Fatalf("release: %v", err)
}

fmt.Println("pass")

Concurrency Example (Distributed Lock)

An example that verifies that the balance always returns to its starting value even when several goroutines repeat deposits/withdrawals while mutually excluding each other via a per-account lock.

go
transaction := func(account int, delta int64) {
    key := fmt.Sprintf("account-%d", account)
    ticket, err := broker.Acquire(key, bankWait, bankLease)
    if err != nil {
        log.Printf("account %d acquire failed: %v", account, err)
        return
    }
    before := balances[account]
    runtime.Gosched()
    after := before + delta
    balances[account] = after
    ticket.Close() // released automatically in the background

    action, sign := "deposit", "+"
    if delta < 0 {
        action, sign = "withdraw", "-"
    }
    log.Printf("account %d %s %s$%d -> $%d", account, action, sign, bankAmount, after)
}

See the full examples in example_test.go, bank_example_test.go, bench_test.go.