go-zookeeper / zk

Native ZooKeeper client for Go
BSD 3-Clause "New" or "Revised" License
504 stars 130 forks source link

TryLock API for lock #116

Open k-jingyang opened 11 months ago

k-jingyang commented 11 months ago

Hello,

Thanks for this open source library. I was looking into Lock() and realised that it blocks until a lock is acquired.

We have a use case where we have multiple instances trying to acquire the lock and we want to return an error immediately for instances that did not acquire the lock instead of waiting to acquire the lock. Is this achievable?

Calling Exists() before Lock() is not an ideal solution because reads are not linearizable in ZooKeeper.

Thanks!

k-jingyang commented 11 months ago

https://github.com/samuel/go-zookeeper/pull/168/files

I realised that an unmerged PR in the original repo tries to do something similar, by implementing a timeout. However, the timeout may also not be ideal because

  1. Network speed could fluctuate. timing out doesn't mean that there's already an existing lock
  2. Setting an arbitrary duration is not efficient. Other clients (those that did not acquire the lock) will just spend their time busy waiting and not doing work.