How to identify that a document is locked when using GetAndLock

Hi,

I am using the go sdk, version 2.3.1

When I execute a GetAndLock function on a document that is already locked by a previous GetAndLock call I get a timeout error. I see in the error that there were retries and the retry reason is KV_LOCKED.

Is there a way know if a document is locked according to the error?
Is there any other way to check if a document is locked?

I upload a screenshot of the error I am getting.

Thank you,
Eli.

Hi @Eli_gotesman this depends on why you want to access the KV_LOCKED reason.

If you just want to know the timeout was caused by the document being locked then you could use errors.As as documented in our docs here and then check what the retry reason is.

If you don’t actually want to retry until timeout and want to fail as soon as you see that the doc is locked then you could implement a custom retry strategy which fails fast on the locked retry reason and otherwise falls back to the best effort strategy otherwise. Something like…

type myRetryStrategy struct {
	base *gocb.BestEffortRetryStrategy
}

func (rs *myRetryStrategy) RetryAfter(req gocb.RetryRequest, reason gocb.RetryReason) gocb.RetryAction {
	if reason == gocb.KVLockedRetryReason {
		// Bail out.
		return &gocb.NoRetryRetryAction{}
	}

	return rs.base.RetryAfter(req, reason)
}
...
...
retryStat := &myRetryStrategy{
	base: gocb.NewBestEffortRetryStrategy(nil),
}
_, err = col.GetAndLock("mydoc", 5*time.Second, &gocb.GetAndLockOptions{
	RetryStrategy: retryStat,
})
if err != nil {
	if errors.Is(err, gocb.ErrDocumentLocked) {
		log.Println("IM LOCKED")
	}

	// do something else
}

Hi @chvck,

The custom retry strategy it’s what I needed, I tried it and it works great!
Thank you very much.