You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2023/01/25 14:49:28 UTC

[james-project] branch master updated: JAMES-3876 Load-balancing flag for Remote Delivery Gateways (#1403)

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new ecc2583dc9 JAMES-3876 Load-balancing flag for Remote Delivery Gateways (#1403)
ecc2583dc9 is described below

commit ecc2583dc9f582d4de36656f606b8b3e26b94b2c
Author: AdBuch <41...@users.noreply.github.com>
AuthorDate: Wed Jan 25 15:49:22 2023 +0100

    JAMES-3876 Load-balancing flag for Remote Delivery Gateways (#1403)
---
 .../docs/modules/ROOT/partials/RemoteDelivery.adoc                | 1 +
 server/apps/spring-app/src/main/resources/mailetcontainer.xml     | 5 +++++
 .../apache/james/transport/mailets/remote/delivery/DnsHelper.java | 7 +++++++
 .../mailets/remote/delivery/RemoteDeliveryConfiguration.java      | 8 ++++++++
 4 files changed, 21 insertions(+)

diff --git a/server/apps/distributed-app/docs/modules/ROOT/partials/RemoteDelivery.adoc b/server/apps/distributed-app/docs/modules/ROOT/partials/RemoteDelivery.adoc
index f450898607..159e30dce1 100644
--- a/server/apps/distributed-app/docs/modules/ROOT/partials/RemoteDelivery.adoc
+++ b/server/apps/distributed-app/docs/modules/ROOT/partials/RemoteDelivery.adoc
@@ -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
diff --git a/server/apps/spring-app/src/main/resources/mailetcontainer.xml b/server/apps/spring-app/src/main/resources/mailetcontainer.xml
index 91dd3868ee..ce4c038c56 100644
--- a/server/apps/spring-app/src/main/resources/mailetcontainer.xml
+++ b/server/apps/spring-app/src/main/resources/mailetcontainer.xml
@@ -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>
+            -->
           <!-- If the gateway requires smtp authentication the following directives -->
           <!-- (gatewayUsername/gatewayPassword) can be used. -->
           <!--
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DnsHelper.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DnsHelper.java
index bc7976e7fe..c1554c6afd 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DnsHelper.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DnsHelper.java
@@ -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;
@@ -41,6 +44,10 @@ public class DnsHelper {
     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);
         }
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
index 262eec7dec..7c6f91f4e5 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
@@ -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;
@@ -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;
@@ -101,6 +103,7 @@ public class RemoteDeliveryConfiguration {
         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);
@@ -340,4 +343,9 @@ public class RemoteDeliveryConfiguration {
     public Optional<ProcessingState> getOnSuccess() {
         return onSuccess;
     }
+
+    public boolean isLoadBalancing() {
+        return loadBalancing;
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org