Skip to content

Commit

Permalink
apply http manager to handle rquest
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Sep 6, 2020
1 parent 97efc6c commit 598d6f3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
15 changes: 0 additions & 15 deletions core/src/main/java/com/flowci/core/common/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import com.flowci.core.common.helper.ThreadHelper;
import com.flowci.util.FileHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.cache.annotation.EnableCaching;
Expand Down Expand Up @@ -94,18 +91,6 @@ public ObjectMapper objectMapper() {
return JacksonHelper.create();
}

@Bean("httpClient")
public HttpClient httpClient() {
int timeout = 10 * 1000;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();

return HttpClientBuilder.create().setDefaultRequestConfig(config).build();
}

@Bean("appTaskExecutor")
public TaskExecutor getAppTaskExecutor() {
return ThreadHelper.createTaskExecutor(100, 100, 50, "app-task-");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.flowci.core.common.manager;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

@Component("httpManager")
public class HttpRequestManager {

private final static int DefaultTimeout = 30 * 1000;

private final static String DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36";

private HttpClient client;

@PostConstruct
public void init() {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(DefaultTimeout)
.setConnectionRequestTimeout(DefaultTimeout)
.setSocketTimeout(DefaultTimeout)
.build();
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
}

public String get(String url) throws IOException {
HttpGet request = new HttpGet(url);
request.setHeader("User-Agent", DefaultUserAgent);

HttpResponse execute = client.execute(request);
return EntityUtils.toString(execute.getEntity());
}
}
12 changes: 7 additions & 5 deletions core/src/main/java/com/flowci/core/flow/config/FlowConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flowci.core.common.config.AppProperties;
import com.flowci.core.common.helper.CacheHelper;
import com.flowci.core.common.manager.HttpRequestManager;
import com.flowci.core.flow.domain.Template;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.log4j.Log4j2;
Expand All @@ -28,7 +29,6 @@
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
Expand All @@ -50,12 +50,14 @@ public Cache<String, List<String>> gitBranchCache() {
}

@Bean("templates")
public List<Template> getTemplates() throws IOException {
public List<Template> getTemplates(HttpRequestManager httpManager) throws IOException {
String body = httpManager.get(flowProperties.getTemplatesUrl());

TypeReference<List<Template>> typeRef = new TypeReference<List<Template>>() {
};
String url = flowProperties.getTemplatesUrl();
List<Template> list = objectMapper.readValue(new URL(url), typeRef);
log.info("Templates is loaded from {}", url);

List<Template> list = objectMapper.readValue(body, typeRef);
log.info("Templates is loaded from {}", flowProperties.getTemplatesUrl());
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flowci.core.common.config.AppProperties;
import com.flowci.core.common.git.GitClient;
import com.flowci.core.common.manager.HttpRequestManager;
import com.flowci.core.common.manager.VarManager;
import com.flowci.core.plugin.dao.PluginDao;
import com.flowci.core.plugin.domain.Plugin;
Expand Down Expand Up @@ -90,6 +91,9 @@ public class PluginServiceImpl implements PluginService {
@Autowired
private VarManager varManager;

@Autowired
private HttpRequestManager httpManager;

private final Object reloadLock = new Object();

@EventListener
Expand Down Expand Up @@ -191,7 +195,8 @@ public Path getDir(Plugin plugin) {
public List<PluginRepoInfo> load(String repoUrl) {
try {
repoUrl = repoUrl + "?t=" + Instant.now().toEpochMilli();
return objectMapper.readValue(new URL(repoUrl), RepoListType);
String body = httpManager.get(repoUrl);
return objectMapper.readValue(body, RepoListType);
} catch (Throwable e) {
log.warn("Unable to load plugin repo '{}' : {}", repoUrl, e.getMessage());
return Collections.emptyList();
Expand Down

0 comments on commit 598d6f3

Please sign in to comment.