diff --git a/api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java b/api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java index 0a04ce9d6..7d380036c 100644 --- a/api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java +++ b/api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java @@ -1,7 +1,9 @@ package io.kafbat.ui.service; import static io.kafbat.ui.model.ApplicationInfoDTO.EnabledFeaturesEnum; +import static io.kafbat.ui.util.GithubReleaseInfo.GITHUB_RELEASE_INFO_TIMEOUT; +import com.google.common.annotations.VisibleForTesting; import io.kafbat.ui.model.ApplicationInfoBuildDTO; import io.kafbat.ui.model.ApplicationInfoDTO; import io.kafbat.ui.model.ApplicationInfoLatestReleaseDTO; @@ -13,27 +15,27 @@ import java.util.Optional; import java.util.Properties; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.info.BuildProperties; import org.springframework.boot.info.GitProperties; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import reactor.core.publisher.Mono; @Service public class ApplicationInfoService { - - private final GithubReleaseInfo githubReleaseInfo = new GithubReleaseInfo(); - + private final GithubReleaseInfo githubReleaseInfo; private final DynamicConfigOperations dynamicConfigOperations; private final BuildProperties buildProperties; private final GitProperties gitProperties; public ApplicationInfoService(DynamicConfigOperations dynamicConfigOperations, @Autowired(required = false) BuildProperties buildProperties, - @Autowired(required = false) GitProperties gitProperties) { + @Autowired(required = false) GitProperties gitProperties, + @Value("${" + GITHUB_RELEASE_INFO_TIMEOUT + ":10}") int githubApiMaxWaitTime) { this.dynamicConfigOperations = dynamicConfigOperations; this.buildProperties = Optional.ofNullable(buildProperties).orElse(new BuildProperties(new Properties())); this.gitProperties = Optional.ofNullable(gitProperties).orElse(new GitProperties(new Properties())); + githubReleaseInfo = new GithubReleaseInfo(githubApiMaxWaitTime); } public ApplicationInfoDTO getApplicationInfo() { @@ -74,4 +76,8 @@ public void updateGithubReleaseInfo() { githubReleaseInfo.refresh().subscribe(); } + @VisibleForTesting + GithubReleaseInfo githubReleaseInfo() { + return githubReleaseInfo; + } } diff --git a/api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java b/api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java index e3f546ad8..ba767a65a 100644 --- a/api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java +++ b/api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java @@ -2,17 +2,17 @@ import com.google.common.annotations.VisibleForTesting; import java.time.Duration; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; @Slf4j public class GithubReleaseInfo { + public static final String GITHUB_RELEASE_INFO_TIMEOUT = "github.release.info.timeout"; private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL = "https://api.github.com/repos/kafbat/kafka-ui/releases/latest"; - private static final Duration GITHUB_API_MAX_WAIT_TIME = Duration.ofSeconds(10); - public record GithubReleaseDto(String html_url, String tag_name, String published_at) { static GithubReleaseDto empty() { @@ -24,17 +24,21 @@ static GithubReleaseDto empty() { private final Mono refreshMono; - public GithubReleaseInfo() { - this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL); + @Getter + private final int githubApiMaxWaitTime; + + public GithubReleaseInfo(int githubApiMaxWaitTime) { + this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL, githubApiMaxWaitTime); } @VisibleForTesting - GithubReleaseInfo(String url) { + GithubReleaseInfo(String url, int githubApiMaxWaitTime) { + this.githubApiMaxWaitTime = githubApiMaxWaitTime; this.refreshMono = new WebClientConfigurator().build() .get() .uri(url) .exchangeToMono(resp -> resp.bodyToMono(GithubReleaseDto.class)) - .timeout(GITHUB_API_MAX_WAIT_TIME) + .timeout(Duration.ofSeconds(this.githubApiMaxWaitTime)) .doOnError(th -> log.trace("Error getting latest github release info", th)) .onErrorResume(th -> true, th -> Mono.just(GithubReleaseDto.empty())) .doOnNext(release -> this.release = release) diff --git a/api/src/test/java/io/kafbat/ui/AbstractIntegrationTest.java b/api/src/test/java/io/kafbat/ui/AbstractIntegrationTest.java index 5f7e6040c..00f58f81b 100644 --- a/api/src/test/java/io/kafbat/ui/AbstractIntegrationTest.java +++ b/api/src/test/java/io/kafbat/ui/AbstractIntegrationTest.java @@ -1,5 +1,7 @@ package io.kafbat.ui; +import static io.kafbat.ui.util.GithubReleaseInfo.GITHUB_RELEASE_INFO_TIMEOUT; + import io.kafbat.ui.container.KafkaConnectContainer; import io.kafbat.ui.container.KsqlDbContainer; import io.kafbat.ui.container.SchemaRegistryContainer; @@ -32,6 +34,7 @@ public abstract class AbstractIntegrationTest { public static final String LOCAL = "local"; public static final String SECOND_LOCAL = "secondLocal"; + protected static final int timeout = 100; private static final boolean IS_ARM = System.getProperty("os.arch").contains("arm") || System.getProperty("os.arch").contains("aarch64"); @@ -98,6 +101,7 @@ public void initialize(@NotNull ConfigurableApplicationContext context) { System.setProperty("dynamic.config.enabled", "true"); System.setProperty("config.related.uploads.dir", tmpDir.toString()); + System.setProperty(GITHUB_RELEASE_INFO_TIMEOUT, String.valueOf(timeout)); } } diff --git a/api/src/test/java/io/kafbat/ui/service/ApplicationInfoServiceTest.java b/api/src/test/java/io/kafbat/ui/service/ApplicationInfoServiceTest.java new file mode 100644 index 000000000..c1fd9975e --- /dev/null +++ b/api/src/test/java/io/kafbat/ui/service/ApplicationInfoServiceTest.java @@ -0,0 +1,17 @@ +package io.kafbat.ui.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import io.kafbat.ui.AbstractIntegrationTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class ApplicationInfoServiceTest extends AbstractIntegrationTest { + @Autowired + private ApplicationInfoService service; + + @Test + public void testCustomGithubReleaseInfoTimeout() { + assertEquals(timeout, service.githubReleaseInfo().getGithubApiMaxWaitTime()); + } +} diff --git a/api/src/test/java/io/kafbat/ui/util/GithubReleaseInfoTest.java b/api/src/test/java/io/kafbat/ui/util/GithubReleaseInfoTest.java index 2f97d7a3a..8d78cedfc 100644 --- a/api/src/test/java/io/kafbat/ui/util/GithubReleaseInfoTest.java +++ b/api/src/test/java/io/kafbat/ui/util/GithubReleaseInfoTest.java @@ -37,7 +37,7 @@ void test() { """)); var url = mockWebServer.url("repos/kafbat/kafka-ui/releases/latest").toString(); - var infoHolder = new GithubReleaseInfo(url); + var infoHolder = new GithubReleaseInfo(url, 10); infoHolder.refresh().block(); var i = infoHolder.get();