Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAMES-3876 Load-balancing flag for Remote Delivery Gateways #1403

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ port is not explicitly defined in the *<gateway/>* parameter. Default is the def
AUTH command. Default is not to issue the AUTH command.
* *gatewayPassword* (required if *gatewayUsername*) is set - a String representing the password to be used
to authenticate the user using the AUTH command.
* *loadBalancing* (optional) - a Boolean (true/false) indicating whether load should be balanced randomly over all defined gateway server. Default is true, false leads to failover only.
* *heloName* (optional) - a String containing the name used in the SMTP HELO and EHLO commands. Default is the default domain,
which is typically *localhost*.
* *mail.** (optional) - Any property beginning with *mail.* described in the Javadoc for package
Expand Down
5 changes: 5 additions & 0 deletions server/apps/spring-app/src/main/resources/mailetcontainer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Regards, Postmaster XXX.YYY
<gateway>otherserver.mydomain.com</gateway>
<gatewayPort>25</gatewayPort>
-->
<!-- in case of multiple gateway load balancing can be activated -->
<!-- with the loadBalancing flag, default is true -->
<!--
<loadBalancing>true</loadBalancing>
AdBuch marked this conversation as resolved.
Show resolved Hide resolved
-->
<!-- If the gateway requires smtp authentication the following directives -->
<!-- (gatewayUsername/gatewayPassword) can be used. -->
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package org.apache.james.transport.mailets.remote.delivery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.dnsservice.api.TemporaryResolutionException;
Expand All @@ -41,6 +44,10 @@ public DnsHelper(DNSService dnsServer, RemoteDeliveryConfiguration configuration
public Iterator<HostAddress> retrieveHostAddressIterator(String host, boolean smtps) throws TemporaryResolutionException {
if (configuration.getGatewayServer().isEmpty()) {
return new MXHostAddressIterator(dnsServer.findMXRecords(host).iterator(), dnsServer, USE_SEVERAL_IP, smtps);
} else if (configuration.isLoadBalancing()) {
List<String> gatewayList = new ArrayList<>(configuration.getGatewayServer());
Collections.shuffle(gatewayList);
return new MXHostAddressIterator(gatewayList.iterator(), dnsServer, USE_SEVERAL_IP, smtps);
} else {
return new MXHostAddressIterator(configuration.getGatewayServer().iterator(), dnsServer, USE_SEVERAL_IP, smtps);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class RemoteDeliveryConfiguration {
public static final String DELAY_TIME = "delayTime";
public static final String DEBUG = "debug";
public static final String ON_SUCCESS = "onSuccess";
public static final String LOAD_BALANCING = "loadBalancing";
public static final int DEFAULT_SMTP_TIMEOUT = 180000;
public static final MailQueueName DEFAULT_OUTGOING_QUEUE_NAME = MailQueueName.of("outgoing");
public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
Expand All @@ -79,6 +80,7 @@ public class RemoteDeliveryConfiguration {
private final boolean verifyServerIdentity;
private final boolean isBindUsed;
private final boolean sendPartial;
private final boolean loadBalancing;
private final int maxRetries;
private final long smtpTimeout;
private final int dnsProblemRetry;
Expand All @@ -101,6 +103,7 @@ public RemoteDeliveryConfiguration(MailetConfig mailetConfig, DomainList domainL
verifyServerIdentity = MailetUtil.getInitParameter(mailetConfig, VERIFY_SERVER_IDENTITY).orElse(true);
usePriority = MailetUtil.getInitParameter(mailetConfig, USE_PRIORITY).orElse(false);
sendPartial = MailetUtil.getInitParameter(mailetConfig, SENDPARTIAL).orElse(false);
loadBalancing = MailetUtil.getInitParameter(mailetConfig, LOAD_BALANCING).orElse(true);
outGoingQueueName = Optional.ofNullable(mailetConfig.getInitParameter(OUTGOING))
.map(MailQueueName::of)
.orElse(DEFAULT_OUTGOING_QUEUE_NAME);
Expand Down Expand Up @@ -340,4 +343,9 @@ public String getBindAddress() {
public Optional<ProcessingState> getOnSuccess() {
return onSuccess;
}

public boolean isLoadBalancing() {
return loadBalancing;
}

}