Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could we have retry and timeout added to simple locks for manual use #11

Closed
cookietx opened this issue Apr 16, 2020 · 3 comments
Closed

Comments

@cookietx
Copy link

cookietx commented Apr 16, 2020

I used the following code stripped form the LockMethodInterceptor to get the retry/timeout functionality with a simple redis lock. It sure would be nice if it was part of the library.

`
private String getLockToken(List keys, String storeId, int retry, long timeOut, long expiration) {

    String token = null;
    try {
        token = constructRetryTemplate(retry, timeOut).execute(context -> {
            final String attemptedToken = lock.acquire(keys, storeId, expiration);

            if (StringUtils.isEmpty(attemptedToken)) {
                throw new LockNotAvailableException(
                        String.format("Lock not available for keys: %s in store %s", keys, storeId));
            }

            return attemptedToken;
        });
    } catch (final Exception e) {
        throw new DistributedLockException(String.format("Unable to acquire lock for keys: %s in store %s", keys, storeId), e);
    }
    return token;
}

private RetryTemplate constructRetryTemplate(int retryPeriodMs, long timeOutMs) {
    // how long to sleep between retries
    final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(retryPeriodMs);

    // when to timeout the whole operation
    final TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy();
    timeoutRetryPolicy.setTimeout(timeOutMs);

    // what exceptions to retry; only LockNotAvailableException, all other exceptions are unexpected and locking should fail
    final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(Integer.MAX_VALUE, Collections.singletonMap(LockNotAvailableException.class, true));

    // combine policies
    final CompositeRetryPolicy compositeRetryPolicy = new CompositeRetryPolicy();
    compositeRetryPolicy.setPolicies(new RetryPolicy[]{timeoutRetryPolicy, simpleRetryPolicy});

    // construct the template
    final RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(compositeRetryPolicy);
    retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

    return retryTemplate;
  }

`

@alturkovic
Copy link
Owner

Released a new version which should have what you are looking for. Hopefully, com.github.alturkovic.lock.retry.RetriableLock is what you were looking for. If not, feel free to leave a comment. If it was, please close the issue.

@cookietx
Copy link
Author

A busy week. I will try to look at it again this weekend

@alturkovic
Copy link
Owner

Closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants