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

Add support for retries #613

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spring-vault-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<optional>true</optional>
</dependency>

<!-- Testing -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.lang.Nullable;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.Assert;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.authentication.SessionManager;
Expand Down Expand Up @@ -58,6 +59,9 @@ public class VaultTemplate implements InitializingBean, VaultOperations, Disposa

private final RestTemplate sessionTemplate;

@Nullable
private RetryTemplate retryTemplate;

@Nullable
private SessionManager sessionManager;

Expand Down Expand Up @@ -267,6 +271,17 @@ public void setSessionManager(SessionManager sessionManager) {
this.sessionManager = sessionManager;
}

/**
* Set the {@link RetryTemplate}.
* @param retryTemplate must not be {@literal null}.
*/
public void setRetryTemplate(RetryTemplate retryTemplate) {

Assert.notNull(retryTemplate, "RetryTemplate must not be null");

this.retryTemplate = retryTemplate;
}

@Override
public void afterPropertiesSet() {
Assert.notNull(this.sessionManager, "SessionManager must not be null");
Expand Down Expand Up @@ -445,7 +460,9 @@ public <T> T doWithSession(RestOperationsCallback<T> sessionCallback) {
Assert.notNull(sessionCallback, "Session callback must not be null");

try {
return sessionCallback.doWithRestOperations(this.sessionTemplate);
return retryTemplate != null
? retryTemplate.execute(retryContext -> sessionCallback.doWithRestOperations(this.sessionTemplate))
: sessionCallback.doWithRestOperations(this.sessionTemplate);
}
catch (HttpStatusCodeException e) {
throw VaultResponses.buildException(e);
Expand Down
10 changes: 10 additions & 0 deletions spring-vault-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<google-api-services-iam.version>v1-rev20201112-1.31.0</google-api-services-iam.version>
<google-auth-library-oauth2-http.version>0.22.2</google-auth-library-oauth2-http.version>
<bcpkix-jdk15on.version>1.67</bcpkix-jdk15on.version>
<spring-retry.version>1.3.0</spring-retry.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -160,6 +161,15 @@
<optional>true</optional>
</dependency>

<!-- Spring Retry -->

<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${spring-retry.version}</version>
<optional>true</optional>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down