Skip to content

Commit

Permalink
implement resilience bulkhead patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
aktaskaan committed Jun 3, 2024
1 parent 3a72731 commit 6108dc8
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 93 deletions.
4 changes: 0 additions & 4 deletions application/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,5 @@ testing-support-endpoints.enabled=${TESTING_SUPPORT_ENDPOINTS_ENABLED:false}
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB

ratelimiter.default.limitForPeriod=${RATE_LIMITER_DEFAULT_LIMIT_FOR_PERIOD:30}
ratelimiter.default.limitRefreshPeriod=${RATE_LIMITER_DEFAULT_LIMIT_REFRESH_PERIOD:1}
ratelimiter.default.timeoutDuration=${RATE_LIMITER_DEFAULT_TIME_OUT_DURATION:0}

bulkhead.default.maxConcurrentCalls=${BULKHEAD_DEFAULT_MAX_CONCURRENT_CALLS:6}
bulkhead.default.maxWaitDuration=${BULKHEAD_DEFAULT_MAX_WAIT_DURATION:500}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.BulkheadConfig;
import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -17,19 +14,6 @@
@Configuration
@Slf4j
public class Resilience4jConfig {

@Getter
@Value("${ratelimiter.default.limitForPeriod}")
private int ratelimiterDefaultLimitForPeriod;

@Getter
@Value("${ratelimiter.default.limitRefreshPeriod}")
private int ratelimiterDefaultLimitRefreshPeriod;

@Getter
@Value("${ratelimiter.default.timeoutDuration}")
private int ratelimiterDefaultTimeoutDuration;

@Getter
@Value("${bulkhead.default.maxConcurrentCalls}")
private int bulkheadDefaultMaxConcurrentCalls;
Expand All @@ -38,66 +22,16 @@ public class Resilience4jConfig {
@Value("${bulkhead.default.maxWaitDuration}")
private int bulkheadDefaultMaxWaitDuration;

public static final String RATE_LIMITER_CASE_TYPE = "case_type";
public static final String RATE_LIMITER_JURISDICTIONS = "jurisdictions";
public static final String RATE_LIMITER_EVENT_TRIGGERS = "event-triggers";

public static final String BULKHEAD_CASE_TYPE = "case_type";
public static final String BULKHEAD_JURISDICTIONS = "jurisdictions";
public static final String BULKHEAD_EVENT_TRIGGERS = "event-triggers";

private final RateLimiterRegistry rateLimiterRegistry;
private final BulkheadRegistry bulkheadRegistry;

public Resilience4jConfig(RateLimiterRegistry rateLimiterRegistry, BulkheadRegistry bulkheadRegistry) {
this.rateLimiterRegistry = rateLimiterRegistry;
public Resilience4jConfig(BulkheadRegistry bulkheadRegistry) {
this.bulkheadRegistry = bulkheadRegistry;
}

@Bean
public RateLimiter caseTypeRateLimiter() {
RateLimiter rateLimiter = createRateLimiter(RATE_LIMITER_CASE_TYPE,
getRatelimiterDefaultLimitForPeriod(),
getRatelimiterDefaultLimitRefreshPeriod(),
getRatelimiterDefaultTimeoutDuration());
initiateRateLimiterEvent(rateLimiter);

return rateLimiter;
}

@Bean
public RateLimiter eventTriggersRateLimiter() {
RateLimiter rateLimiter = createRateLimiter(RATE_LIMITER_EVENT_TRIGGERS,
getRatelimiterDefaultLimitForPeriod(),
getRatelimiterDefaultLimitRefreshPeriod(),
getRatelimiterDefaultTimeoutDuration());
initiateRateLimiterEvent(rateLimiter);

return rateLimiter;
}

@Bean
public RateLimiter jurisdictionRateLimiter() {
RateLimiter rateLimiter = createRateLimiter(RATE_LIMITER_JURISDICTIONS,
getRatelimiterDefaultLimitForPeriod(),
getRatelimiterDefaultLimitRefreshPeriod(),
getRatelimiterDefaultTimeoutDuration());
initiateRateLimiterEvent(rateLimiter);

return rateLimiter;
}

private RateLimiter createRateLimiter(final String name, int limitForPeriod, int limitRefreshPeriod,
int timeoutDuration) {
RateLimiterConfig config = RateLimiterConfig.custom()
.timeoutDuration(Duration.ofSeconds(timeoutDuration))
.limitRefreshPeriod(Duration.ofSeconds(limitRefreshPeriod))
.limitForPeriod(limitForPeriod)
.build();

return rateLimiterRegistry.rateLimiter(name, config);
}

@Bean
public Bulkhead caseTypeBulkhead() {
Bulkhead bulkhead = createBulkhead(BULKHEAD_CASE_TYPE,
Expand Down Expand Up @@ -138,19 +72,10 @@ private Bulkhead createBulkhead(final String name, int bulkheadDefaultMaxConcurr
return bulkheadRegistry.bulkhead(name, config);
}

private void initiateRateLimiterEvent(RateLimiter rateLimiter) {
RateLimiter.EventPublisher eventPublisher = rateLimiter
.getEventPublisher();

eventPublisher.onSuccess(event -> log.info(event.toString()));
eventPublisher.onFailure(event -> log.info(event.toString()));
}

private void initiateBulkheadEvent(Bulkhead bulkhead) {
Bulkhead.EventPublisher eventPublisher = bulkhead
.getEventPublisher();

// eventPublisher.onCallPermitted(event -> log.info(event.toString()));
eventPublisher.onCallRejected(event -> log.error(event.toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public CaseDefinitionController(CaseTypeService caseTypeService, JurisdictionSer
@ApiResponse(code = 200, message = "Unexpected error")
})
@Bulkhead(name = Resilience4jConfig.BULKHEAD_CASE_TYPE)
//@RateLimiter(name = Resilience4jConfig.RATE_LIMITER_CASE_TYPE)
public CaseType dataCaseTypeIdGet(
@ApiParam(value = "Case Type ID", required = true) @PathVariable("id") String id) {
return caseTypeService.findByCaseTypeId(id).orElseThrow(() -> new NotFoundException(id));
Expand Down Expand Up @@ -130,7 +129,6 @@ public List<CaseType> dataJurisdictionsJurisdictionIdCaseTypeGet(
@ApiResponse(code = 200, message = "List of jurisdictions")
})
@Bulkhead(name = Resilience4jConfig.BULKHEAD_JURISDICTIONS)
//@RateLimiter(name = Resilience4jConfig.RATE_LIMITER_JURISDICTIONS)
public List<Jurisdiction> findJurisdictions(
@ApiParam(value = "list of jurisdiction references") @RequestParam("ids") Optional<List<String>> idsOptional) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ImmutableMap;
import io.github.resilience4j.bulkhead.BulkheadFullException;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import org.hibernate.exception.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -123,13 +122,6 @@ Map<String, String> elasticSearchInitialisationException(ElasticSearchInitialisa
return getMessage(e, "ElasticSearch initialisation exception: %s");
}

@ExceptionHandler({RequestNotPermitted.class})
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public Map<String, String> handleRequestNotPermitted(RequestNotPermitted e) {
log(e);
return getMessage(e, "Request is not permitted: %s");
}

@ExceptionHandler({BulkheadFullException.class})
@ResponseStatus(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED)
public Map<String, String> handleRequestNotPermitted(BulkheadFullException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public CaseTabCollection displayTabStructureIdGet(
@ApiResponse(code = 200, message = "A Case Wizard Page Collection"),
@ApiResponse(code = 200, message = "Unexpected error")
})
@Bulkhead(name = Resilience4jConfig.BULKHEAD_CASE_TYPE)
//@RateLimiter(name = Resilience4jConfig.RATE_LIMITER_EVENT_TRIGGERS)
@Bulkhead(name = Resilience4jConfig.BULKHEAD_EVENT_TRIGGERS)
public WizardPageCollection displayWizardPageStructureIdGet(
@ApiParam(value = "Case Type ID", required = true) @PathVariable("ctid") String caseTypeId,
@ApiParam(value = "Event Reference", required = true) @PathVariable("etid") String eventReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setUp() {
when(bulkhead.getEventPublisher()).thenReturn(bulkheadEventPublisher);
when(bulkheadRegistry.bulkhead(anyString(), any(BulkheadConfig.class))).thenReturn(bulkhead);

resilience4jConfig = new Resilience4jConfig(rateLimiterRegistry, bulkheadRegistry);
resilience4jConfig = new Resilience4jConfig(bulkheadRegistry);
resilience4jConfig = spy(resilience4jConfig);

doReturn(10).when(resilience4jConfig).getRatelimiterDefaultLimitForPeriod();
Expand Down

0 comments on commit 6108dc8

Please sign in to comment.