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 Jul 31, 2024
1 parent 8c70126 commit 61a74b9
Show file tree
Hide file tree
Showing 4 changed files with 48 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.kafbat.ui.service;

import static io.kafbat.ui.util.GithubReleaseInfo.GITHUB_RELEASE_INFO_TIMEOUT;
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.kafbat.ui.AbstractIntegrationTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class ApplicationInfoServiceTest extends AbstractIntegrationTest {
private static final int timeout = 100;

@Autowired
private ApplicationInfoService service;

@BeforeAll
public static void setup() {
System.setProperty(GITHUB_RELEASE_INFO_TIMEOUT, String.valueOf(timeout));
}

@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 61a74b9

Please sign in to comment.