-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
8 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
82 changes: 82 additions & 0 deletions
82
graal/src/main/java/io/sixwaaaay/sharingcomment/client/UserClientWrapper.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,82 @@ | ||
/* | ||
* Copyright (c) 2023-2024 sixwaaaay. | ||
* 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 | ||
* http://www.apache.org/licenses/LICENSE-2. | ||
* 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 io.sixwaaaay.sharingcomment.client; | ||
|
||
import io.github.resilience4j.circuitbreaker.CircuitBreaker; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; | ||
import io.github.resilience4j.decorators.Decorators; | ||
import io.github.resilience4j.retry.Retry; | ||
import io.github.resilience4j.retry.RetryConfig; | ||
import io.sixwaaaay.sharingcomment.transmission.GetMultipleUserReply; | ||
import io.sixwaaaay.sharingcomment.transmission.GetUserReply; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
|
||
public class UserClientWrapper implements UserClient { | ||
private final UserClient userClient; | ||
|
||
private final CircuitBreaker circuitBreaker; | ||
|
||
private final Retry retry; | ||
|
||
public UserClientWrapper(UserClient userClient) { | ||
|
||
this.userClient = userClient; | ||
var name = "userClient"; | ||
var retryConfig = RetryConfig.custom() | ||
.maxAttempts(3) | ||
.waitDuration(Duration.ofMillis(1000)) | ||
.ignoreExceptions( | ||
HttpClientErrorException.NotFound.class, | ||
HttpClientErrorException.Forbidden.class, | ||
HttpClientErrorException.MethodNotAllowed.class, | ||
HttpClientErrorException.Unauthorized.class | ||
).build(); | ||
retry = Retry.of(name, retryConfig); | ||
var circuitBreakerConfig = CircuitBreakerConfig.custom() | ||
.failureRateThreshold(50) | ||
.ignoreExceptions( | ||
HttpClientErrorException.NotFound.class, | ||
HttpClientErrorException.Forbidden.class, | ||
HttpClientErrorException.MethodNotAllowed.class, | ||
HttpClientErrorException.Unauthorized.class | ||
) | ||
.waitDurationInOpenState(Duration.ofMillis(1000)) | ||
.permittedNumberOfCallsInHalfOpenState(2) | ||
.slidingWindowSize(2) | ||
.build(); | ||
circuitBreaker = CircuitBreaker.of(name, circuitBreakerConfig); | ||
} | ||
|
||
@Override | ||
public GetUserReply getUser(long id, String token) { | ||
var getUserReplySupplier = Decorators.ofSupplier(() -> userClient.getUser(id, token)) | ||
.withCircuitBreaker(circuitBreaker) | ||
.withRetry(retry) | ||
.decorate(); | ||
return getUserReplySupplier.get(); | ||
} | ||
|
||
|
||
@Override | ||
public GetMultipleUserReply getManyUser(Collection<Long> ids, String token) { | ||
var getManyUserReplySupplier = Decorators.ofSupplier(() -> userClient.getManyUser(ids, token)) | ||
.withCircuitBreaker(circuitBreaker) | ||
.withRetry(retry) | ||
.decorate(); | ||
return getManyUserReplySupplier.get(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
graal/src/main/java/io/sixwaaaay/sharingcomment/client/VoteClientWrapper.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,85 @@ | ||
/* | ||
* Copyright (c) 2023-2024 sixwaaaay. | ||
* 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 | ||
* http://www.apache.org/licenses/LICENSE-2. | ||
* 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 io.sixwaaaay.sharingcomment.client; | ||
|
||
import io.github.resilience4j.circuitbreaker.CircuitBreaker; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; | ||
import io.github.resilience4j.decorators.Decorators; | ||
import io.github.resilience4j.retry.Retry; | ||
import io.github.resilience4j.retry.RetryConfig; | ||
import io.sixwaaaay.sharingcomment.transmission.*; | ||
|
||
import java.time.Duration; | ||
import java.util.Set; | ||
|
||
public class VoteClientWrapper implements VoteClient { | ||
|
||
|
||
private final VoteClient voteClient; | ||
|
||
public VoteClientWrapper(VoteClient voteClient) { | ||
this.voteClient = voteClient; | ||
} | ||
|
||
private final String name = "voteClient"; | ||
|
||
private final CircuitBreaker circuitBreaker = CircuitBreaker.of(name, CircuitBreakerConfig.custom() | ||
.failureRateThreshold(50) | ||
.waitDurationInOpenState(Duration.ofMillis(2000)) | ||
.permittedNumberOfCallsInHalfOpenState(2) | ||
.slidingWindowSize(2) | ||
.build() | ||
); | ||
|
||
private final Retry retry = Retry.of(name, RetryConfig.custom() | ||
.maxAttempts(3) | ||
.waitDuration(Duration.ofMillis(1000)) | ||
.build() | ||
); | ||
|
||
@Override | ||
public VoteReply itemAdd(VoteReq req) { | ||
return voteClient.itemAdd(req); | ||
} | ||
|
||
@Override | ||
public VoteReply itemDelete(VoteReq req) { | ||
return voteClient.itemDelete(req); | ||
} | ||
|
||
/** | ||
* wrap the exists method with resilience4j circuit breaker and retry | ||
* fallback with an empty set if the circuit breaker is open | ||
*/ | ||
@Override | ||
public VoteExistsReply exists(VoteExistsReq req) { | ||
var existsReplySupplier = Decorators.ofSupplier(() -> voteClient.exists(req)) | ||
.withCircuitBreaker(circuitBreaker) | ||
.withRetry(retry) | ||
.withFallback(this::fallback) | ||
.decorate(); | ||
return existsReplySupplier.get(); | ||
} | ||
|
||
@Override | ||
public ScanVotedReply scan(ScanVotedReq req) { | ||
return voteClient.scan(req); | ||
} | ||
|
||
private VoteExistsReply fallback(Throwable e) { | ||
var reply = new VoteExistsReply(); | ||
reply.setExists(Set.of()); | ||
return reply; | ||
} | ||
} |
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