[muc] Make providesMucService() use the KNOWN_MUC_SERVICES cache

This reduces the number of disco#info queries on MUC join in some
situations.
This commit is contained in:
damencho 2020-06-11 13:12:13 -05:00 committed by Florian Schmaus
parent f1319d1c8b
commit 8e337d7810
2 changed files with 16 additions and 12 deletions

View File

@ -88,7 +88,6 @@ import org.jxmpp.jid.EntityJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.util.cache.ExpirationCache;
/**
* A MultiUserChat room (XEP-45), created with {@link MultiUserChatManager#getMultiUserChat(EntityBareJid)}.
@ -112,9 +111,6 @@ import org.jxmpp.util.cache.ExpirationCache;
public class MultiUserChat {
private static final Logger LOGGER = Logger.getLogger(MultiUserChat.class.getName());
private static final ExpirationCache<DomainBareJid, Void> KNOWN_MUC_SERVICES = new ExpirationCache<>(
100, 1000 * 60 * 60 * 24);
private final XMPPConnection connection;
private final EntityBareJid room;
private final MultiUserChatManager multiUserChatManager;
@ -340,12 +336,8 @@ public class MultiUserChat {
private Presence enter(MucEnterConfiguration conf) throws NotConnectedException, NoResponseException,
XMPPErrorException, InterruptedException, NotAMucServiceException {
final DomainBareJid mucService = room.asDomainBareJid();
if (!KNOWN_MUC_SERVICES.containsKey(mucService)) {
if (multiUserChatManager.providesMucService(mucService)) {
KNOWN_MUC_SERVICES.put(mucService, null);
} else {
throw new NotAMucServiceException(this);
}
if (!multiUserChatManager.providesMucService(mucService)) {
throw new NotAMucServiceException(this);
}
// We enter a room by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"

View File

@ -64,6 +64,7 @@ import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.EntityJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.util.cache.ExpirationCache;
/**
* A manager for Multi-User Chat rooms.
@ -136,6 +137,9 @@ public final class MultiUserChatManager extends Manager {
private static final StanzaFilter INVITATION_FILTER = new AndFilter(StanzaTypeFilter.MESSAGE, new StanzaExtensionFilter(new MUCUser()),
new NotFilter(MessageTypeFilter.ERROR));
private static final ExpirationCache<DomainBareJid, Void> KNOWN_MUC_SERVICES = new ExpirationCache<>(
100, 1000 * 60 * 60 * 24);
private final Set<InvitationListener> invitationsListeners = new CopyOnWriteArraySet<InvitationListener>();
/**
@ -392,8 +396,16 @@ public final class MultiUserChatManager extends Manager {
*/
public boolean providesMucService(DomainBareJid domainBareJid) throws NoResponseException,
XMPPErrorException, NotConnectedException, InterruptedException {
return serviceDiscoveryManager.supportsFeature(domainBareJid,
MUCInitialPresence.NAMESPACE);
boolean contains = KNOWN_MUC_SERVICES.containsKey(domainBareJid);
if (!contains) {
if (serviceDiscoveryManager.supportsFeature(domainBareJid,
MUCInitialPresence.NAMESPACE)) {
KNOWN_MUC_SERVICES.put(domainBareJid, null);
return true;
}
}
return contains;
}
/**