Skip to content

Commit

Permalink
BE: Make gh version check timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerdv committed Sep 20, 2024
1 parent 025c41a commit f5551f8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
16 changes: 11 additions & 5 deletions api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -74,4 +76,8 @@ public void updateGithubReleaseInfo() {
githubReleaseInfo.refresh().subscribe();
}

@VisibleForTesting
GithubReleaseInfo githubReleaseInfo() {
return githubReleaseInfo;
}
}
16 changes: 10 additions & 6 deletions api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -24,17 +24,21 @@ static GithubReleaseDto empty() {

private final Mono<Void> 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)
Expand Down
4 changes: 4 additions & 0 deletions api/src/test/java/io/kafbat/ui/AbstractIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f5551f8

Please sign in to comment.