Skip to content

Commit

Permalink
JENKINS-73740: Allow admins to disable throttling
Browse files Browse the repository at this point in the history
Add global configuration to disable throttling.
  • Loading branch information
slide committed Nov 1, 2024
1 parent bb7402a commit 6dd03bf
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public class ExtendedEmailPublisher extends Notifier implements MatrixAggregatab
*/
public boolean disabled = false;

/* If true, will check for throttling limits before sending email */
public boolean throttlingEnabled = false;

/**
* How to theTrigger the email if the project is a matrix project.
*/
Expand Down Expand Up @@ -533,7 +536,8 @@ boolean sendMail(ExtendedEmailPublisherContext context) {
}
MimeMessage msg = createMail(context, fromAddress, session);
debug(context.getListener().getLogger(), "Successfully created MimeMessage");
if (EmailThrottler.getInstance().isThrottlingLimitExceeded()
if (getDescriptor().isThrottlingEnabled()
&& EmailThrottler.getInstance().isThrottlingLimitExceeded()
&& !context.getTrigger().shouldBypassThrottling()) {
context.getListener().getLogger().println("Could not send email. Throttling limit exceeded.");
return false;
Expand Down Expand Up @@ -569,7 +573,9 @@ boolean sendMail(ExtendedEmailPublisherContext context) {
try {
transport.connect();
transport.sendMessage(msg, allRecipients);
EmailThrottler.getInstance().incrementEmailCount();
if (getDescriptor().isThrottlingEnabled()) {
EmailThrottler.getInstance().incrementEmailCount();
}
break;
} catch (SendFailedException e) {
if (e.getNextException() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public final class ExtendedEmailPublisherDescriptor extends BuildStepDescriptor<
*/
private boolean enableAllowUnregistered;

/**
* Enables the email throttling feature
*/
private boolean throttlingEnabled = false;

private transient String smtpHost;
private transient String smtpPort;
private transient String smtpAuthUsername;
Expand Down Expand Up @@ -300,6 +305,15 @@ public void setDefaultSuffix(String defaultSuffix) {
this.defaultSuffix = Util.fixEmptyAndTrim(defaultSuffix);
}

public boolean isThrottlingEnabled() {
return throttlingEnabled;
}

@DataBoundSetter
public void setThrottlingEnabled(boolean throttlingEnabled) {
this.throttlingEnabled = throttlingEnabled;
}

@Restricted(NoExternalUse.class)
Session createSession(MailAccount acc, ExtendedEmailPublisherContext context) {
final String SMTP_PORT_PROPERTY = "mail.smtp.port";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ f.section(title: _("Extended E-mail Notification")) {
}
}

f.entry(field: "throttlingEnabled", title: _("Enable Throttling"), help: "/plugin/email-ext/help/globalConfig/throttlingEnabled.html") {
f.checkbox()
}
f.entry(field: "debugMode", title: _("Enable Debug Mode"), help: "/plugin/email-ext/help/globalConfig/debugMode.html") {
f.checkbox()
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/help/globalConfig/throttlingEnabled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Check this to enable email throttling which for less important triggers,
will check the number of messages sent and throttle new messages if too
many have been sent.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Launcher;
Expand Down Expand Up @@ -123,6 +119,7 @@ public void setUp() throws Exception {
publisher.getDescriptor().setDefaultSuffix(null);
publisher.getDescriptor().setEmergencyReroute(null);
publisher.getDescriptor().setAllowedDomains(null);
publisher.getDescriptor().setThrottlingEnabled(true);
oldAuthorizationStrategy = j.jenkins.getAuthorizationStrategy();
oldSecurityRealm = j.jenkins.getSecurityRealm();
oldAdminAddress = JenkinsLocationConfiguration.get().getAdminAddress();
Expand Down Expand Up @@ -1702,8 +1699,8 @@ public void testProjectFromWithDefaultSuffix() throws Exception {
}

@Test
public void testLowerThanLimit() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("test");
public void testThrottlingLowerThanLimit() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("throttle-test");
prj.getPublishersList().add(publisher);

publisher.recipientList = "mickey@disney.com";
Expand All @@ -1730,8 +1727,8 @@ public void testLowerThanLimit() throws Exception {
}

@Test
public void testEqualsLimit() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("test2");
public void testThrottlingEqualsLimit() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("throttle-test2");
prj.getPublishersList().add(publisher);

publisher.recipientList = "mickey@disney.com";
Expand Down Expand Up @@ -1760,8 +1757,8 @@ public void testEqualsLimit() throws Exception {
}

@Test
public void testOverLimit() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("test3");
public void testThrottlingOverLimit() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("throttle-test3");
prj.getPublishersList().add(publisher);

publisher.recipientList = "mickey@disney.com";
Expand Down Expand Up @@ -1789,6 +1786,36 @@ public void testOverLimit() throws Exception {
Mailbox.get("mickey@disney.com").size());
}

@Test
public void testThrottlingDisabled() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("throttle-test4");
prj.getPublishersList().add(publisher);
publisher.getDescriptor().setThrottlingEnabled(false);

publisher.recipientList = "mickey@disney.com";
for (int i = 0; i < 130; i++) {
publisher.configuredTriggers.add(new SuccessTrigger(
recProviders,
"$DEFAULT_RECIPIENTS",
"$DEFAULT_REPLYTO",
"$DEFAULT_SUBJECT",
"$DEFAULT_CONTENT",
"",
0,
"project"));
}

for (EmailTrigger trigger : publisher.configuredTriggers) {
trigger.getEmail().addRecipientProvider(new ListRecipientProvider());
}

FreeStyleBuild build = prj.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);

assertNotEquals(EmailThrottler.THROTTLING_LIMIT, 130);
assertEquals(130, Mailbox.get("mickey@disney.com").size());
}

/**
* Similar to {@link SleepBuilder} but only on the first build. (Removing
* the builder between builds is tricky since you would have to wait for the
Expand Down

0 comments on commit 6dd03bf

Please sign in to comment.