Skip to content

Add Reserve/Unreserve methods for creating a reservation for capacity

Arran Walker requested to merge ajwalker/reserve-capacity into main

Why

There's situations where consumers may want to only call Acquire() once they've confirmed there is enough capacity.

Whilst this can be done by first checking with:

available, _ := ts.Capacity()

// only acquire when we know there's capacity
if available > 0 {
    acq, err := ts.Acquire()
    ...
}

... it provides little guarantee when multiple goroutines are doing an identical check. A race exists between the call to Capacity() and Acquire().

To address this, this MR introduces Reserve and Unreserve.

Reserve(key) returns true if capacity exists, and providing all other callers also use Reserve(), the race is eliminated. Reserve() subtracts reserved capacity from a call to Capacity(). When Acquire() is called with the same key, the reservation is automatically removed.

Unreserve(key) cancels a reservation.

Related issues

gitlab-org/gitlab-runner#29431 (closed)

Merge request reports