forked from spring-cloud/spring-cloud-circuitbreaker
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new circuitbreaker-failsafe with naive implementation
- Started working on spring-cloud-incubator/spring-cloud-circuitbreaker/spring-cloud#30
- Loading branch information
1 parent
a32929a
commit 78b2dff
Showing
10 changed files
with
616 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>spring-cloud-circuitbreaker</artifactId> | ||
<groupId>org.springframework.cloud</groupId> | ||
<version>0.0.1.BUILD-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-cloud-circuitbreaker-failsafe</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-circuitbreaker-commons</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.jodah</groupId> | ||
<artifactId>failsafe</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
63 changes: 63 additions & 0 deletions
63
...ain/java/org/springframework/cloud/circuitbreaker/failsafe/FailsafeAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.circuitbreaker.failsafe; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import net.jodah.failsafe.Failsafe; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.cloud.circuitbreaker.commons.CircuitBreakerFactory; | ||
import org.springframework.cloud.circuitbreaker.commons.Customizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Jakub Marchwicki | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(Failsafe.class) | ||
public class FailsafeAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(CircuitBreakerFactory.class) | ||
public CircuitBreakerFactory failsafeCircuitBreakerFactory() { | ||
return new FailsafeCircuitBreakerFactory(); | ||
} | ||
|
||
@Configuration | ||
public static class FailsafeCustomizerConfiguration { | ||
|
||
@Autowired(required = false) | ||
private List<Customizer<FailsafeCircuitBreakerFactory>> customizers = new ArrayList<>(); | ||
|
||
@Autowired(required = false) | ||
private FailsafeCircuitBreakerFactory factory; | ||
|
||
@PostConstruct | ||
public void init() { | ||
customizers.forEach(customizer -> customizer.customize(factory)); | ||
} | ||
|
||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...c/main/java/org/springframework/cloud/circuitbreaker/failsafe/FailsafeCircuitBreaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.circuitbreaker.failsafe; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import net.jodah.failsafe.Failsafe; | ||
import net.jodah.failsafe.Fallback; | ||
import net.jodah.failsafe.event.ExecutionAttemptedEvent; | ||
import net.jodah.failsafe.function.CheckedFunction; | ||
|
||
import org.springframework.cloud.circuitbreaker.commons.CircuitBreaker; | ||
|
||
/** | ||
* @author Jakub Marchwicki | ||
*/ | ||
public class FailsafeCircuitBreaker implements CircuitBreaker { | ||
|
||
private String id; | ||
|
||
private FailsafeConfigBuilder.FailsafeConfig config; | ||
|
||
// private Optional<Customizer<RetryTemplate>> retryTemplateCustomizer; | ||
// | ||
// private RetryTemplate retryTemplate; | ||
|
||
public FailsafeCircuitBreaker(String id, | ||
FailsafeConfigBuilder.FailsafeConfig config) { | ||
// , | ||
// Optional<Customizer<RetryTemplate>> retryTemplateCustomizer) { | ||
this.id = id; | ||
this.config = config; | ||
// this.retryTemplateCustomizer = retryTemplateCustomizer; | ||
// this.retryTemplate = new RetryTemplate(); | ||
} | ||
|
||
@Override | ||
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) { | ||
|
||
// retryTemplate.setBackOffPolicy(config.getBackOffPolicy()); | ||
// retryTemplate.setRetryPolicy(config.getRetryPolicy()); | ||
// | ||
// retryTemplateCustomizer | ||
// .ifPresent(customizer -> customizer.customize(retryTemplate)); | ||
|
||
Fallback<T> f = Fallback.of(extract(fallback)); | ||
return Failsafe.with(f).get(ex -> toRun.get()); | ||
// return retryTemplate.execute(context -> toRun.get(), | ||
// context -> fallback.apply(context.getLastThrowable()), | ||
// new DefaultRetryState(id, config.isForceRefreshState(), | ||
// config.getStateClassifier())); | ||
} | ||
|
||
private <T> CheckedFunction<ExecutionAttemptedEvent<? extends T>, ? extends T> extract( | ||
Function<Throwable, T> fallback) { | ||
return execution -> fallback.apply(execution.getLastFailure()); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...java/org/springframework/cloud/circuitbreaker/failsafe/FailsafeCircuitBreakerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.circuitbreaker.failsafe; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import net.jodah.failsafe.Policy; | ||
|
||
import org.springframework.cloud.circuitbreaker.commons.CircuitBreaker; | ||
import org.springframework.cloud.circuitbreaker.commons.CircuitBreakerFactory; | ||
import org.springframework.cloud.circuitbreaker.commons.Customizer; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* @author Jakub Marchwicki | ||
*/ | ||
public class FailsafeCircuitBreakerFactory extends | ||
CircuitBreakerFactory<FailsafeConfigBuilder.FailsafeConfig, FailsafeConfigBuilder> { | ||
|
||
private Function<String, FailsafeConfigBuilder.FailsafeConfig> defaultConfig = id -> new FailsafeConfigBuilder( | ||
id).build(); | ||
|
||
private Map<String, Customizer<Policy>> failsafeCustomizers = new HashMap<>(); | ||
|
||
@Override | ||
protected FailsafeConfigBuilder configBuilder(String id) { | ||
return new FailsafeConfigBuilder(id); | ||
} | ||
|
||
@Override | ||
public void configureDefault( | ||
Function<String, FailsafeConfigBuilder.FailsafeConfig> defaultConfiguration) { | ||
this.defaultConfig = defaultConfiguration; | ||
} | ||
|
||
@Override | ||
public CircuitBreaker create(String id) { | ||
Assert.hasText(id, "A circuit breaker must have an id"); | ||
FailsafeConfigBuilder.FailsafeConfig config = getConfigurations() | ||
.computeIfAbsent(id, defaultConfig); | ||
return new FailsafeCircuitBreaker(id, config); | ||
} | ||
|
||
public void addRetryTemplateCustomizers(Customizer<Policy> customizer, | ||
String... ids) { | ||
for (String id : ids) { | ||
this.failsafeCustomizers.put(id, customizer); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.