-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf($Druid): dynamic connection pool size
BREAKING CHANGE: dynamic connection pool size, set by CPU core count
- Loading branch information
1 parent
5673dba
commit ea37259
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...a/com/jmsoftware/maf/springcloudstarter/database/DruidDataSourceCreatorPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |