Skip to content

The Unified and Autonomous Development Platform

fix: Able to configure mail sending concurrency in mail service

Robin Shenb6e265628236
diff --git a/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java b/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java
index e9f7ba544a..f376906f49 100644
--- a/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java
+++ b/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java
@@ -9310,4 +9310,20 @@ public class DataMigrator {
}
}
+ private void migrate239(File dataDir, Stack<Integer> versions) {
+ for (File file : dataDir.listFiles()) {
+ if (file.getName().startsWith("Settings.xml")) {
+ var dom = VersionedXmlDoc.fromFile(file);
+ for (Element element : dom.getRootElement().elements()) {
+ if (element.elementTextTrim("key").equals("MAIL")) {
+ Element valueElement = element.element("value");
+ if (valueElement != null)
+ valueElement.addElement("concurrency").setText("2");
+ }
+ }
+ dom.writeToFile(file, false);
+ }
+ }
+ }
+
}
diff --git a/server-core/src/main/java/io/onedev/server/mail/DefaultMailService.java b/server-core/src/main/java/io/onedev/server/mail/DefaultMailService.java
index 233d80c495..50308f34ac 100644
--- a/server-core/src/main/java/io/onedev/server/mail/DefaultMailService.java
+++ b/server-core/src/main/java/io/onedev/server/mail/DefaultMailService.java
@@ -21,9 +21,12 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -174,6 +177,10 @@ public class DefaultMailService implements MailService, Serializable {
private volatile Thread thread;
+ private volatile ThreadPoolExecutor sendExecutor;
+
+ private final ThreadLocal<Boolean> inSendExecutor = ThreadLocal.withInitial(() -> false);
+
private boolean isProdTest() {
if (prodTest == null) {
prodTest = settingService.getSystemSetting().getServerUrl().equals("https://code.onedev.io")
@@ -186,6 +193,33 @@ public class DefaultMailService implements MailService, Serializable {
return "[" + settingService.getBrandingSetting().getName() + "]";
}
+ private synchronized ThreadPoolExecutor getSendExecutor(int concurrency) {
+ if (sendExecutor == null) {
+ sendExecutor = new ThreadPoolExecutor(concurrency, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
+ new LinkedBlockingQueue<>());
+ sendExecutor.allowCoreThreadTimeOut(true);
+ } else if (sendExecutor.getCorePoolSize() != concurrency) {
+ sendExecutor.setCorePoolSize(concurrency);
+ }
+ return sendExecutor;
+ }
+
+ private Future<?> submitToSendExecutor(int concurrency, Runnable runnable) {
+ if (Boolean.TRUE.equals(inSendExecutor.get())) {
+ runnable.run();
+ return CompletableFuture.completedFuture(null);
+ } else {
+ return getSendExecutor(concurrency).submit(() -> {
+ inSendExecutor.set(true);
+ try {
+ runnable.run();
+ } finally {
+ inSendExecutor.remove();
+ }
+ });
+ }
+ }
+
public Object writeReplace() throws ObjectStreamException {
return new ManagedSerializedForm(MailService.class);
}
@@ -195,15 +229,21 @@ public class DefaultMailService implements MailService, Serializable {
public void sendMailAsync(Collection<String> toList, Collection<String> ccList, Collection<String> bccList,
String subject, String htmlBody, String textBody, @Nullable String replyAddress,
@Nullable String senderName, @Nullable String references) {
- transactionService.runAfterCommit(() -> executorService.execute(() -> {
- try {
- SecurityUtils.bindAsSystem();
- sendMail(toList, ccList, bccList, subject, htmlBody, textBody, replyAddress,
- senderName, references);
- } catch (Exception e) {
- logger.error("Error sending email (to: " + toList + ", subject: " + subject + ")", e);
- }
- }));
+ transactionService.runAfterCommit(() -> {
+ var mailConnector = settingService.getMailConnector();
+ submitToSendExecutor(mailConnector.getConcurrency(), () -> {
+ try {
+ SecurityUtils.bindAsSystem();
+ sendMail(toList, ccList, bccList, subject, htmlBody, textBody, replyAddress,
+ senderName, references);
+ } catch (Exception e) {
+ var message = String.format(
+ "Error sending email (to: %s, cc: %s, bcc: %s, subject: %s)",
+ toList, ccList, bccList, subject);
+ logger.error(message, e);
+ }
+ });
+ });
}
private String getThreadIndex(String references) {
@@ -341,8 +381,18 @@ public class DefaultMailService implements MailService, Serializable {
message.setContent(bodyPart);
logger.debug("Sending email (subject: {}, to: {}, cc: {}, bcc: {})... ", subject, toList, ccList, bccList);
-
- Transport.send(message);
+
+ try {
+ submitToSendExecutor(smtpSetting.getConcurrency(), () -> {
+ try {
+ Transport.send(message);
+ } catch (MessagingException e) {
+ throw new RuntimeException(e);
+ }
+ }).get();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
} catch (MessagingException e) {
throw new RuntimeException(e);
}
@@ -813,12 +863,21 @@ public class DefaultMailService implements MailService, Serializable {
@Listen
public void on(SystemStopping event) {
- Thread copy = thread;
+ Thread threadCopy = thread;
thread = null;
- if (copy != null) {
- copy.interrupt();
+ if (threadCopy != null) {
+ threadCopy.interrupt();
+ try {
+ threadCopy.join();
+ } catch (InterruptedException ignored) {
+ }
+ }
+ var sendExecutorCopy = sendExecutor;
+ sendExecutor = null;
+ if (sendExecutorCopy != null) {
+ sendExecutorCopy.shutdown();
try {
- copy.join();
+ sendExecutorCopy.awaitTermination(60, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
}
}
diff --git a/server-core/src/main/java/io/onedev/server/mail/SmtpSetting.java b/server-core/src/main/java/io/onedev/server/mail/SmtpSetting.java
index 2f1db8b9f7..2042619bdb 100644
--- a/server-core/src/main/java/io/onedev/server/mail/SmtpSetting.java
+++ b/server-core/src/main/java/io/onedev/server/mail/SmtpSetting.java
@@ -15,15 +15,18 @@ public class SmtpSetting implements Serializable {
private final String smtpUser;
private final MailCredential smtpCredential;
+
+ private final int concurrency;
private final int timeout;
public SmtpSetting(String smtpHost, SmtpSslSetting sslSetting, String smtpUser,
- MailCredential smtpCredential, int timeout) {
+ MailCredential smtpCredential, int concurrency, int timeout) {
this.smtpHost = smtpHost;
this.sslSetting = sslSetting;
this.smtpUser = smtpUser;
this.smtpCredential = smtpCredential;
+ this.concurrency = concurrency;
this.timeout = timeout;
}
@@ -43,8 +46,12 @@ public class SmtpSetting implements Serializable {
return smtpCredential;
}
+ public int getConcurrency() {
+ return concurrency;
+ }
+
public int getTimeout() {
return timeout;
}
-
+
}
diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailservice/MailConnector.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailservice/MailConnector.java
index f87a8676ff..c268e6c839 100644
--- a/server-core/src/main/java/io/onedev/server/model/support/administration/mailservice/MailConnector.java
+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailservice/MailConnector.java
@@ -12,6 +12,8 @@ import io.onedev.server.mail.InboxMonitor;
public interface MailConnector extends Serializable {
String getSystemAddress();
+
+ int getConcurrency();
void sendMail(Collection<String> toList, Collection<String> ccList, Collection<String> bccList,
String subject, String htmlBody, String textBody, @Nullable String replyAddress,
diff --git a/server-ee b/server-ee
index 48c3350b31..7cb5e2514f 160000
--- a/server-ee
+++ b/server-ee
@@ -1 +1 @@
-Subproject commit 48c3350b311ebff9df104ee4b1410b754e820018
+Subproject commit 7cb5e2514f7651aaa785c84c326f30d41bbbc6e4
diff --git a/server-plugin/server-plugin-mail-gmail/src/main/java/io/onedev/server/plugin/mail/gmail/GmailConnector.java b/server-plugin/server-plugin-mail-gmail/src/main/java/io/onedev/server/plugin/mail/gmail/GmailConnector.java
index 4fece21d0b..17a29d5402 100644
--- a/server-plugin/server-plugin-mail-gmail/src/main/java/io/onedev/server/plugin/mail/gmail/GmailConnector.java
+++ b/server-plugin/server-plugin-mail-gmail/src/main/java/io/onedev/server/plugin/mail/gmail/GmailConnector.java
@@ -46,8 +46,10 @@ public class GmailConnector implements MailConnector {
private InboxPollSetting inboxPollSetting;
- private int timeout = 60;
+ private int concurrency = 2;
+ private int timeout = 60;
+
private transient MailPosition mailPosition;
@Editable(order=100, description="Client ID of this OneDev instance registered in Google cloud")
@@ -187,10 +189,20 @@ public class GmailConnector implements MailConnector {
});
return new SmtpSetting("smtp.gmail.com", new SmtpExplicitSsl(), accountName, smtpCredential,
- getTimeout());
+ getConcurrency(), getTimeout());
+ }
+
+ @Editable(order=10000, description="Specify max number of mail sending sessions that can run concurrently")
+ @Min(value=1, message="This value should not be less than 1")
+ public int getConcurrency() {
+ return concurrency;
+ }
+
+ public void setConcurrency(int concurrency) {
+ this.concurrency = concurrency;
}
- @Editable(order=10000, description="Specify timeout in seconds when communicating with mail server")
+ @Editable(order=10100, description="Specify timeout in seconds when communicating with mail server")
@Min(value=5, message="This value should not be less than 5")
public int getTimeout() {
return timeout;
diff --git a/server-plugin/server-plugin-mail-office365/src/main/java/io/onedev/server/plugin/mail/office365/Office365Connector.java b/server-plugin/server-plugin-mail-office365/src/main/java/io/onedev/server/plugin/mail/office365/Office365Connector.java
index 8ac1fdf893..932e6e2af9 100644
--- a/server-plugin/server-plugin-mail-office365/src/main/java/io/onedev/server/plugin/mail/office365/Office365Connector.java
+++ b/server-plugin/server-plugin-mail-office365/src/main/java/io/onedev/server/plugin/mail/office365/Office365Connector.java
@@ -45,6 +45,8 @@ public class Office365Connector implements MailConnector {
private InboxPollSetting inboxPollSetting;
private int timeout = 60;
+
+ private int concurrency = 2;
private transient MailPosition mailPosition;
@@ -128,8 +130,18 @@ public class Office365Connector implements MailConnector {
public void setInboxPollSetting(InboxPollSetting inboxPollSetting) {
this.inboxPollSetting = inboxPollSetting;
}
+
+ @Editable(order=10000, description="Specify max number of mail sending sessions that can run concurrently")
+ @Min(value=1, message="This value should not be less than 1")
+ public int getConcurrency() {
+ return concurrency;
+ }
+
+ public void setConcurrency(int concurrency) {
+ this.concurrency = concurrency;
+ }
- @Editable(order=10000, description="Specify timeout in seconds when communicating with mail server")
+ @Editable(order=10100, description="Specify timeout in seconds when communicating with mail server")
@Min(value=5, message="This value should not be less than 5")
public int getTimeout() {
return timeout;
@@ -225,7 +237,7 @@ public class Office365Connector implements MailConnector {
}
});
return new SmtpSetting("smtp.office365.com", new SmtpExplicitSsl(), userPrincipalName,
- smtpCredential, getTimeout());
+ smtpCredential, getConcurrency(), getTimeout());
}
@Override
diff --git a/server-plugin/server-plugin-mail-smtpimap/src/main/java/io/onedev/server/plugin/mail/smtpimap/SmtpImapConnector.java b/server-plugin/server-plugin-mail-smtpimap/src/main/java/io/onedev/server/plugin/mail/smtpimap/SmtpImapConnector.java
index c031ab72bb..1be354950a 100644
--- a/server-plugin/server-plugin-mail-smtpimap/src/main/java/io/onedev/server/plugin/mail/smtpimap/SmtpImapConnector.java
+++ b/server-plugin/server-plugin-mail-smtpimap/src/main/java/io/onedev/server/plugin/mail/smtpimap/SmtpImapConnector.java
@@ -35,8 +35,10 @@ public class SmtpImapConnector implements MailConnector {
private InboxPollSetting inboxPollSetting;
- private int timeout = 60;
+ private int concurrency = 2;
+ private int timeout = 60;
+
private transient MailPosition mailPosition;
@Editable(order=100, name="SMTP Host")
@@ -103,7 +105,17 @@ public class SmtpImapConnector implements MailConnector {
this.inboxPollSetting = inboxPollSetting;
}
- @Editable(order=10000, description="Specify timeout in seconds when communicating with mail server")
+ @Editable(order=10000, description="Specify max number of mail sending sessions that can run concurrently")
+ @Min(value=1, message="This value should not be less than 1")
+ public int getConcurrency() {
+ return concurrency;
+ }
+
+ public void setConcurrency(int concurrency) {
+ this.concurrency = concurrency;
+ }
+
+ @Editable(order=10100, description="Specify timeout in seconds when communicating with mail server")
@Min(value=5, message="This value should not be less than 5")
public int getTimeout() {
return timeout;
@@ -119,7 +131,7 @@ public class SmtpImapConnector implements MailConnector {
smtpCredential = new BasicAuthPassword(smtpPassword);
else
smtpCredential = null;
- return new SmtpSetting(smtpHost, sslSetting, smtpUser, smtpCredential, getTimeout());
+ return new SmtpSetting(smtpHost, sslSetting, smtpUser, smtpCredential, getConcurrency(), getTimeout());
}
@Override