Implement acquire heartbeat functionality
Acquire
signature changes to Acquire(ctx context.Context, key string)
.
Previously, Acquire
didn't take a context, we would lock the instance and then immediately return because the calculation to determine whether the instance was acquirable or not was instant.
Now, as part of Acquire
, a heartbeat connection is attempted. This can take some time, as it's driven by the usual connection timeouts.
We avoid locking for the whole duration, and instead increase a pending
count. The singleflight
library is used so that multiple heartbeats for the same instance are rolled into one, and if the pending
count exceeds the capacity of the instance, we immediately return saying that there's no capacity so that the caller will try against a different instance.
If the WithHeartbeatFunc
isn't provided, everything works exactly as it did before.