Skip to content

Task 2-1: Timeout Handling cho Apple/Google Verify (Circuit Breaker)

Phase: 2 - Resilience Priority: Medium Depends on: Không có Reference: docs/BountyHunter-Backend/details/feature-payment-marketplace/SPEC.md

Background

Apple App Store và Google Play verify endpoints có thể chậm hoặc timeout. Nếu không có circuit breaker, thread pool có thể bị cạn kiệt khi Apple/Google server có vấn đề.

Tasks

DI Note: - Xác nhận HTTP client hiện tại: RestTemplate hay WebClient — grep AppleIapServiceGooglePlayService để biết. - Nếu dùng RestTemplate: tạo bean riêng với timeout config trong @Configuration class, inject vào service qua constructor (không dùng shared restTemplate bean mặc định nếu có timeout khác nhau). - Nếu dùng Resilience4j (@CircuitBreaker): đảm bảo resilience4j-spring-boot2 dependency có trong pom.xml và config trong application.yaml.

  • [ ] Xác nhận HTTP client hiện tại cho Apple/Google API calls:

    grep -rn "RestTemplate\|WebClient\|HttpClient" application-core/service/payment/
    

  • [ ] Thêm timeout config (nếu dùng RestTemplate):

    // Trong @Configuration class (ví dụ: PaymentConfig.java):
    @Bean("appleRestTemplate")
    public RestTemplate appleRestTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(5000);   // 5s connect timeout
        factory.setReadTimeout(10000);     // 10s read timeout
        return new RestTemplate(factory);
    }
    // Inject: @Qualifier("appleRestTemplate") RestTemplate appleRestTemplate
    

  • [ ] Thêm circuit breaker nếu project dùng Resilience4j:

    @CircuitBreaker(name = "apple-iap", fallbackMethod = "appleVerifyFallback")
    public boolean verifyAppleReceipt(String receiptData) { ... }
    
    public boolean appleVerifyFallback(String receiptData, Exception e) {
        LOGGER.error("[APPLE_IAP] Circuit open - verify failed", e);
        throw new ServiceUnavailableException("APPLE_VERIFY_UNAVAILABLE");
    }
    

  • [ ] Nếu không dùng Resilience4j → implement simple timeout + retry với RetryTemplate (Spring Retry)

Verification / Acceptance Criteria

  • [ ] Apple/Google HTTP client có connect timeout ≤ 5s và read timeout ≤ 10s
  • [ ] Khi Apple/Google server timeout → ServiceUnavailableException (hoặc tương đương) được throw, không hang thread
  • [ ] Circuit breaker (nếu có): mở sau N failures, fallback method được gọi
  • [ ] Timeout config được externalize qua @Value hoặc application.yaml (không hardcode)
  • [ ] Normal flow không bị ảnh hưởng sau khi thêm timeout

Files to Modify

  • Config class cho Apple/Google HTTP client (tạo hoặc edit PaymentConfig.java)
  • application-core/service/payment/AppleIapService.java
  • application-core/service/payment/GooglePlayService.java