james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [james-project] branch master updated: JAMES-2740 Cache mailbox listing in RandomStoring mailet
Date Tue, 30 Apr 2019 11:24:56 GMT
This is an automated email from the ASF dual-hosted git repository.

rouazana 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 59e4b07  JAMES-2740 Cache mailbox listing in RandomStoring mailet
59e4b07 is described below

commit 59e4b07b00cfd1e0df8925ed079f2bf98645adc1
Author: Gautier DI FOLCO <gdifolco@linagora.com>
AuthorDate: Fri Apr 19 17:48:54 2019 +0200

    JAMES-2740 Cache mailbox listing in RandomStoring mailet
---
 .../james/transport/mailets/RandomStoring.java     | 46 ++++++++++++----------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
index 74d33e8..8423b37 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
@@ -19,6 +19,7 @@
 
 package org.apache.james.transport.mailets;
 
+import java.time.Duration;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
@@ -44,6 +45,7 @@ import org.apache.mailet.base.GenericMailet;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.Streams;
+import reactor.core.publisher.Mono;
 
 /**
  * Process messages and randomly assign them to 4 to 8 mailboxes.
@@ -52,6 +54,9 @@ public class RandomStoring extends GenericMailet {
 
     private static final int MIN_NUMBER_OF_RECIPIENTS = 4;
     private static final int MAX_NUMBER_OF_RECIPIENTS = 8;
+    private static final Duration CACHE_DURATION = Duration.ofMinutes(15);
+
+    private final Mono<List<ReroutingInfos>> reroutingInfos;
     private final UsersRepository usersRepository;
     private final MailboxManager mailboxManager;
     private final Iterator<Integer> randomRecipientsNumbers;
@@ -61,23 +66,20 @@ public class RandomStoring extends GenericMailet {
         this.usersRepository = usersRepository;
         this.mailboxManager = mailboxManager;
         this.randomRecipientsNumbers = new Random().ints(MIN_NUMBER_OF_RECIPIENTS, MAX_NUMBER_OF_RECIPIENTS
+ 1).boxed().iterator();
+        this.reroutingInfos = Mono.fromCallable(this::retrieveReroutingInfos).cache(CACHE_DURATION);
     }
 
     @Override
     public void service(Mail mail) throws MessagingException {
-        try {
-            Collection<ReroutingInfos> reroutingInfos = generateRandomMailboxes();
-            Collection<MailAddress> mailAddresses = reroutingInfos
-                .stream()
-                .map(ReroutingInfos::getMailAddress)
-                .collect(Guavate.toImmutableList());
-
-            mail.setRecipients(mailAddresses);
-            reroutingInfos.forEach(reroutingInfo ->
-                mail.setAttribute(Attribute.convertToAttribute(MailStore.DELIVERY_PATH_PREFIX
+ reroutingInfo.getUser(), reroutingInfo.getMailbox())));
-        } catch (UsersRepositoryException e) {
-            throw new MessagingException("Unable to compute a random user list", e);
-        }
+        Collection<ReroutingInfos> reroutingInfos = generateRandomMailboxes();
+        Collection<MailAddress> mailAddresses = reroutingInfos
+            .stream()
+            .map(ReroutingInfos::getMailAddress)
+            .collect(Guavate.toImmutableList());
+
+        mail.setRecipients(mailAddresses);
+        reroutingInfos.forEach(reroutingInfo ->
+            mail.setAttribute(Attribute.convertToAttribute(MailStore.DELIVERY_PATH_PREFIX
+ reroutingInfo.getUser(), reroutingInfo.getMailbox())));
     }
 
     @Override
@@ -89,20 +91,24 @@ public class RandomStoring extends GenericMailet {
     public void init() throws MessagingException {
     }
 
-    private Collection<ReroutingInfos> generateRandomMailboxes() throws UsersRepositoryException
{
-        List<ReroutingInfos> reroutingInfos = Streams.stream(usersRepository.list())
-            .map(User::fromUsername)
-            .flatMap(this::buildReRoutingInfos)
-            .distinct()
-            .collect(Collectors.toList());
-
+    private Collection<ReroutingInfos> generateRandomMailboxes() {
+        List<ReroutingInfos> reroutingInfos = this.reroutingInfos.block();
         Collections.shuffle(reroutingInfos);
+
         return reroutingInfos
             .stream()
             .limit(randomRecipientsNumbers.next())
             .collect(Guavate.toImmutableSet());
     }
 
+    private List<ReroutingInfos> retrieveReroutingInfos() throws UsersRepositoryException
{
+        return Streams.stream(usersRepository.list())
+            .map(User::fromUsername)
+            .flatMap(this::buildReRoutingInfos)
+            .distinct()
+            .collect(Collectors.toList());
+    }
+
     private Stream<ReroutingInfos> buildReRoutingInfos(User user) {
         try {
             MailAddress mailAddress = usersRepository.getMailAddressFor(user);


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


Mime
View raw message