Skip to content

Commit

Permalink
perf($Druid): dynamic connection pool size
Browse files Browse the repository at this point in the history
BREAKING CHANGE: dynamic connection pool size, set by CPU core count
  • Loading branch information
johnnymillergh committed Aug 25, 2021
1 parent 5673dba commit ea37259
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
@Slf4j
@ConditionalOnClass({MybatisPlusAutoConfiguration.class})
public class DataSourceConfiguration {
@Bean
public DruidDataSourceCreatorPostProcessor druidDataSourceCreatorPostProcessor() {
log.warn("Initial bean: '{}'", DruidDataSourceCreatorPostProcessor.class.getSimpleName());
return new DruidDataSourceCreatorPostProcessor();
}

/**
* Primary data source. Had to configure DynamicRoutingDataSource as primary. Otherwise
* MasterSlaveAutoRoutingPlugin will not be able to be injected datasource correctly.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.jmsoftware.maf.springcloudstarter.database;

import com.baomidou.dynamic.datasource.creator.DruidDataSourceCreator;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

/**
* Description: DruidDataSourceCreatorPostProcessor, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 8/25/2021 11:27 AM
**/
@Slf4j
public class DruidDataSourceCreatorPostProcessor implements BeanPostProcessor {
@Nullable
@Override
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
if (bean instanceof DruidDataSourceCreator) {
this.postProcessDynamicDataSourceProperties((DruidDataSourceCreator) bean);
}
return bean;
}

private void postProcessDynamicDataSourceProperties(DruidDataSourceCreator bean) {
val cpuCoreCount = Runtime.getRuntime().availableProcessors();
val druidConfig = bean.getGConfig();
log.warn("Setting Druid connection pool by current cpuCoreCount: {}", cpuCoreCount);
druidConfig.setInitialSize(cpuCoreCount);
druidConfig.setMaxActive(cpuCoreCount * 2 + 1);
}
}

0 comments on commit ea37259

Please sign in to comment.