Skip to content

Allow for timeout while acquiring lock in StrictConnPool.#163

Merged
ok2c merged 3 commits intoapache:masterfrom
cwildman:timeout_acquiring_lock
Dec 13, 2019
Merged

Allow for timeout while acquiring lock in StrictConnPool.#163
ok2c merged 3 commits intoapache:masterfrom
cwildman:timeout_acquiring_lock

Conversation

@cwildman
Copy link
Copy Markdown
Contributor

I mentioned in the mailing list that a connection request does not fail immediately when the time to acquire the StrictConnPool lock is longer than the requestTimeout. Instead it will only fail when the lock is finally acquired and the deadline is checked in processPendingRequest(). @ok2c agreed and suggested I propose a PR.

This PR changes StrictConnPool.lease to use the requestTimeout when attempting to grab the lock. If the lock can't be acquired within the timeout the request is failed with a DeadlineTimeoutException.

A few comments:

  1. tryLock(timeout) is interruptible whereas lock() is not. I'm unclear if this would break any existing behavior.
  2. I don't love that acquisition of the lock is captured in a boolean, it feels slightly dangerous if this block gets more complex down the road. However I chose this over nested try catches since we need to handle the InterruptedException.

if (request.isDone()) {
this.completedRequests.add(request);
acquiredLock = this.lock.tryLock(requestTimeout.getDuration(), requestTimeout.getTimeUnit());
} catch (final InterruptedException ignored) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cwildman InterruptedException handling logic does not look quite right to me. At the very least one must call Thread.currentThread().interrupt() when catching and not re-throwing InterruptedException. I also think we should either cancel request at that point or propagate the exception to the caller.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I updated to set interrupted again and immediately return a failed future with the InterruptedException.

acquiredLock = this.lock.tryLock(requestTimeout.getDuration(), requestTimeout.getTimeUnit());
} catch (final InterruptedException interruptedException) {
Thread.currentThread().interrupt();
future.failed(interruptedException);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cwildman One last minor thing. Would not future#cancel() be more appropriate here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that does seem right.

@ok2c ok2c merged commit 6d6f1fa into apache:master Dec 13, 2019
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

Successfully merging this pull request may close these issues.

2 participants