Skip to content

Commit

Permalink
[SCB-2163] tiny code improve (#2138)
Browse files Browse the repository at this point in the history
  • Loading branch information
wujimin committed Dec 16, 2020
1 parent 58e61d8 commit 4d613ee
Showing 1 changed file with 28 additions and 22 deletions.
Expand Up @@ -36,6 +36,7 @@
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.dns.AddressResolverOptions;

/**
* load and manages a set of HttpClient at boot up.
Expand Down Expand Up @@ -65,7 +66,7 @@ public static void mockClientPoolManager(String name, ClientPoolManager<HttpClie
}

/* used for configurations module: these module must be boot before other HttpClients is initialized. so can
* not load by SPI, must add manually */
* not load by SPI, must add manually */
public static void addNewClientPoolManager(HttpClientOptionsSPI option) {
httpClients.put(option.clientName(), createClientPoolManager(option));
}
Expand All @@ -80,20 +81,7 @@ public static void destroy() {
}

private static ClientPoolManager<HttpClientWithContext> createClientPoolManager(HttpClientOptionsSPI option) {
Vertx vertx;

if (option.useSharedVertx()) {
vertx = SharedVertxFactory.getSharedVertx();
} else {
VertxOptions vertxOptions = new VertxOptions()
.setAddressResolverOptions(AddressResolverConfig
.getAddressResover(option.getConfigTag(), option.getConfigReader()))
.setEventLoopPoolSize(option.getEventLoopPoolSize());

// Maybe we can deploy only one vert.x for the application. However this has did it like this.
vertx = VertxUtils.getOrCreateVertxByName(option.clientName(), vertxOptions);
}

Vertx vertx = getOrCreateVertx(option);
ClientPoolManager<HttpClientWithContext> clientPoolManager = new ClientPoolManager<>(vertx,
new HttpClientPoolFactory(HttpClientOptionsSPI.createHttpClientOptions(option)));

Expand All @@ -104,10 +92,25 @@ private static ClientPoolManager<HttpClientWithContext> createClientPoolManager(
.setWorkerPoolSize(option.getWorkerPoolSize());
try {
VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions);
return clientPoolManager;
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return clientPoolManager;
}

private static Vertx getOrCreateVertx(HttpClientOptionsSPI option) {
if (option.useSharedVertx()) {
return SharedVertxFactory.getSharedVertx();
}

AddressResolverOptions resolverOptions = AddressResolverConfig
.getAddressResover(option.getConfigTag(), option.getConfigReader());
VertxOptions vertxOptions = new VertxOptions()
.setAddressResolverOptions(resolverOptions)
.setEventLoopPoolSize(option.getEventLoopPoolSize());

// Maybe we can deploy only one vert.x for the application. However this has did it like this.
return VertxUtils.getOrCreateVertxByName(option.clientName(), vertxOptions);
}

/**
Expand All @@ -116,11 +119,12 @@ private static ClientPoolManager<HttpClientWithContext> createClientPoolManager(
* @return the deployed instance name
*/
public static HttpClientWithContext getClient(String clientName) {
if (httpClients.get(clientName) == null) {
ClientPoolManager<HttpClientWithContext> poolManager = httpClients.get(clientName);
if (poolManager == null) {
LOGGER.error("client name [{}] not exists, should only happen in tests.", clientName);
return null;
}
return httpClients.get(clientName).findThreadBindClientPool();
return poolManager.findThreadBindClientPool();
}

/**
Expand All @@ -130,11 +134,12 @@ public static HttpClientWithContext getClient(String clientName) {
* @return the deployed instance name
*/
public static HttpClientWithContext getClient(String clientName, boolean sync) {
if (httpClients.get(clientName) == null) {
ClientPoolManager<HttpClientWithContext> poolManager = httpClients.get(clientName);
if (poolManager == null) {
LOGGER.error("client name [{}] not exists, should only happen in tests.", clientName);
return null;
}
return httpClients.get(clientName).findClientPool(sync);
return poolManager.findClientPool(sync);
}

/**
Expand All @@ -145,10 +150,11 @@ public static HttpClientWithContext getClient(String clientName, boolean sync) {
* @return the deployed instance name
*/
public static HttpClientWithContext getClient(String clientName, boolean sync, Context targetContext) {
if (httpClients.get(clientName) == null) {
ClientPoolManager<HttpClientWithContext> poolManager = httpClients.get(clientName);
if (poolManager == null) {
LOGGER.error("client name [{}] not exists, should only happen in tests.", clientName);
return null;
}
return httpClients.get(clientName).findClientPool(sync, targetContext);
return poolManager.findClientPool(sync, targetContext);
}
}

0 comments on commit 4d613ee

Please sign in to comment.