1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Merge branch '4.3'

This commit is contained in:
Florian Schmaus 2019-04-16 09:21:14 +02:00
commit 9c30e4f3cc
18 changed files with 147 additions and 22 deletions

View file

@ -6,6 +6,11 @@
<module name="SuppressionFilter"> <module name="SuppressionFilter">
<property name="file" value="config/suppressions.xml"/> <property name="file" value="config/suppressions.xml"/>
</module> </module>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE\:OFF\:(\w+)"/>
<property name="onCommentFormat" value="CHECKSTYLE\:ON\:(\w+)"/>
<property name="checkFormat" value="$1"/>
</module>
<module name="Header"> <module name="Header">
<property name="headerFile" value="config/${checkstyleLicenseHeader}.txt"/> <property name="headerFile" value="config/${checkstyleLicenseHeader}.txt"/>
<property name="ignoreLines" value="3"/> <property name="ignoreLines" value="3"/>
@ -61,6 +66,14 @@
<property name="format" value="^\s*//[^\s]"/> <property name="format" value="^\s*//[^\s]"/>
<property name="message" value="Comment start ('//') followed by non-space character. You would not continue after a punctuation without a space, would you?"/> <property name="message" value="Comment start ('//') followed by non-space character. You would not continue after a punctuation without a space, would you?"/>
</module> </module>
<!-- Check for synchronized keyword on Manager's static
getInstanceFor() method. Note that if XMPPConnection is every
replaced with something else, then we need to change it here
too. -->
<module name="RegexpSingleline">
<property name="format" value="^\s*public(?!.*synchronized).*getInstanceFor\(XMPPConnection.*$"/>
<property name="message" value="getInstanceFor() should be synchronized"/>
</module>
<module name="JavadocPackage"/> <module name="JavadocPackage"/>
<module name="TreeWalker"> <module name="TreeWalker">
<module name="SuppressionCommentFilter"/> <module name="SuppressionCommentFilter"/>

View file

@ -41,6 +41,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
@ -343,6 +344,17 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
protected final AsyncButOrdered<StanzaListener> inOrderListeners = new AsyncButOrdered<>(); protected final AsyncButOrdered<StanzaListener> inOrderListeners = new AsyncButOrdered<>();
/**
* An executor which uses {@link #asyncGoLimited(Runnable)} to limit the number of asynchronously processed runnables
* per connection.
*/
private final Executor limitedExcutor = new Executor() {
@Override
public void execute(Runnable runnable) {
asyncGoLimited(runnable);
}
};
/** /**
* The used host to establish the connection to * The used host to establish the connection to
*/ */
@ -1336,7 +1348,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
executorService = ASYNC_BUT_ORDERED.asExecutorFor(this); executorService = ASYNC_BUT_ORDERED.asExecutorFor(this);
break; break;
case async: case async:
executorService = CACHED_EXECUTOR_SERVICE; executorService = limitedExcutor;
break; break;
} }
final IQRequestHandler finalIqRequestHandler = iqRequestHandler; final IQRequestHandler finalIqRequestHandler = iqRequestHandler;
@ -1379,7 +1391,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
final Collection<StanzaListener> listenersToNotify = new LinkedList<>(); final Collection<StanzaListener> listenersToNotify = new LinkedList<>();
extractMatchingListeners(packet, asyncRecvListeners, listenersToNotify); extractMatchingListeners(packet, asyncRecvListeners, listenersToNotify);
for (final StanzaListener listener : listenersToNotify) { for (final StanzaListener listener : listenersToNotify) {
asyncGo(new Runnable() { asyncGoLimited(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1875,6 +1887,75 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return getClass().getSimpleName() + '[' + localEndpointString + "] (" + getConnectionCounter() + ')'; return getClass().getSimpleName() + '[' + localEndpointString + "] (" + getConnectionCounter() + ')';
} }
/**
* A queue of deferred runnables that where not executed immediately because {@link #currentAsyncRunnables} reached
* {@link #maxAsyncRunnables}. Note that we use a {@code LinkedList} in order to avoid space blowups in case the
* list ever becomes very big and shrinks again.
*/
private final Queue<Runnable> deferredAsyncRunnables = new LinkedList<>();
private int deferredAsyncRunnablesCount;
private int deferredAsyncRunnablesCountPrevious;
private int maxAsyncRunnables = SmackConfiguration.getDefaultConcurrencyLevelLimit();
private int currentAsyncRunnables;
protected void asyncGoLimited(final Runnable runnable) {
Runnable wrappedRunnable = new Runnable() {
@Override
public void run() {
runnable.run();
synchronized (deferredAsyncRunnables) {
Runnable defferredRunnable = deferredAsyncRunnables.poll();
if (defferredRunnable == null) {
currentAsyncRunnables--;
} else {
deferredAsyncRunnablesCount--;
asyncGo(defferredRunnable);
}
}
}
};
synchronized (deferredAsyncRunnables) {
if (currentAsyncRunnables < maxAsyncRunnables) {
currentAsyncRunnables++;
asyncGo(wrappedRunnable);
} else {
deferredAsyncRunnablesCount++;
deferredAsyncRunnables.add(wrappedRunnable);
}
final int HIGH_WATERMARK = 100;
final int INFORM_WATERMARK = 20;
final int deferredAsyncRunnablesCount = this.deferredAsyncRunnablesCount;
if (deferredAsyncRunnablesCount >= HIGH_WATERMARK
&& deferredAsyncRunnablesCountPrevious < HIGH_WATERMARK) {
LOGGER.log(Level.WARNING, "High watermark of " + HIGH_WATERMARK + " simultaneous executing runnables reached");
} else if (deferredAsyncRunnablesCount >= INFORM_WATERMARK
&& deferredAsyncRunnablesCountPrevious < INFORM_WATERMARK) {
LOGGER.log(Level.INFO, INFORM_WATERMARK + " simultaneous executing runnables reached");
}
deferredAsyncRunnablesCountPrevious = deferredAsyncRunnablesCount;
}
}
public void setMaxAsyncOperations(int maxAsyncOperations) {
if (maxAsyncOperations < 1) {
throw new IllegalArgumentException("Max async operations must be greater than 0");
}
synchronized (deferredAsyncRunnables) {
maxAsyncRunnables = maxAsyncOperations;
}
}
protected static void asyncGo(Runnable runnable) { protected static void asyncGo(Runnable runnable) {
CACHED_EXECUTOR_SERVICE.execute(runnable); CACHED_EXECUTOR_SERVICE.execute(runnable);
} }

View file

@ -55,6 +55,16 @@ public class AsyncButOrdered<K> {
private final Map<K, Boolean> threadActiveMap = new WeakHashMap<>(); private final Map<K, Boolean> threadActiveMap = new WeakHashMap<>();
private final Executor executor;
public AsyncButOrdered() {
this(null);
}
public AsyncButOrdered(Executor executor) {
this.executor = executor;
}
/** /**
* Invoke the given {@link Runnable} asynchronous but ordered in respect to the given key. * Invoke the given {@link Runnable} asynchronous but ordered in respect to the given key.
* *
@ -86,7 +96,11 @@ public class AsyncButOrdered<K> {
if (newHandler) { if (newHandler) {
Handler handler = new Handler(keyQueue, key); Handler handler = new Handler(keyQueue, key);
threadActiveMap.put(key, true); threadActiveMap.put(key, true);
if (executor == null) {
AbstractXMPPConnection.asyncGo(handler); AbstractXMPPConnection.asyncGo(handler);
} else {
executor.execute(handler);
}
} }
} }

View file

@ -365,4 +365,19 @@ public final class SmackConfiguration {
public static void setUnknownIqRequestReplyMode(UnknownIqRequestReplyMode unknownIqRequestReplyMode) { public static void setUnknownIqRequestReplyMode(UnknownIqRequestReplyMode unknownIqRequestReplyMode) {
SmackConfiguration.unknownIqRequestReplyMode = Objects.requireNonNull(unknownIqRequestReplyMode, "Must set mode"); SmackConfiguration.unknownIqRequestReplyMode = Objects.requireNonNull(unknownIqRequestReplyMode, "Must set mode");
} }
private static final int defaultConcurrencyLevelLimit;
static {
int availableProcessors = Runtime.getRuntime().availableProcessors();
if (availableProcessors < 8) {
defaultConcurrencyLevelLimit = 8;
} else {
defaultConcurrencyLevelLimit = (int) (availableProcessors * 1.1);
}
}
public static int getDefaultConcurrencyLevelLimit() {
return defaultConcurrencyLevelLimit;
}
} }

View file

@ -32,7 +32,7 @@ public final class JingleFileTransferManager extends Manager {
super(connection); super(connection);
} }
public static JingleFileTransferManager getInstanceFor(XMPPConnection connection) { public static synchronized JingleFileTransferManager getInstanceFor(XMPPConnection connection) {
JingleFileTransferManager manager = INSTANCES.get(connection); JingleFileTransferManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new JingleFileTransferManager(connection); manager = new JingleFileTransferManager(connection);

View file

@ -178,7 +178,9 @@ public final class MamManager extends Manager {
* @param connection the XMPP connection to get the archive for. * @param connection the XMPP connection to get the archive for.
* @return the instance of MamManager. * @return the instance of MamManager.
*/ */
// CHECKSTYLE:OFF:RegexpSingleline
public static MamManager getInstanceFor(XMPPConnection connection) { public static MamManager getInstanceFor(XMPPConnection connection) {
// CHECKSTYLE:ON:RegexpSingleline
return getInstanceFor(connection, (Jid) null); return getInstanceFor(connection, (Jid) null);
} }

View file

@ -51,7 +51,7 @@ public final class ReferenceManager extends Manager {
* @param connection xmpp connection * @param connection xmpp connection
* @return reference manager instance * @return reference manager instance
*/ */
public static ReferenceManager getInstanceFor(XMPPConnection connection) { public static synchronized ReferenceManager getInstanceFor(XMPPConnection connection) {
ReferenceManager manager = INSTANCES.get(connection); ReferenceManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new ReferenceManager(connection); manager = new ReferenceManager(connection);

View file

@ -78,7 +78,7 @@ public final class StableUniqueStanzaIdManager extends Manager {
* @param connection xmpp-connection * @param connection xmpp-connection
* @return manager instance for the connection * @return manager instance for the connection
*/ */
public static StableUniqueStanzaIdManager getInstanceFor(XMPPConnection connection) { public static synchronized StableUniqueStanzaIdManager getInstanceFor(XMPPConnection connection) {
StableUniqueStanzaIdManager manager = INSTANCES.get(connection); StableUniqueStanzaIdManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new StableUniqueStanzaIdManager(connection); manager = new StableUniqueStanzaIdManager(connection);

View file

@ -61,7 +61,7 @@ public final class SpoilerManager extends Manager {
* @param connection xmpp connection * @param connection xmpp connection
* @return SpoilerManager * @return SpoilerManager
*/ */
public static SpoilerManager getInstanceFor(XMPPConnection connection) { public static synchronized SpoilerManager getInstanceFor(XMPPConnection connection) {
SpoilerManager manager = INSTANCES.get(connection); SpoilerManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new SpoilerManager(connection); manager = new SpoilerManager(connection);

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2003-2007 Jive Software, 2018 Florian Schmaus. * Copyright 2003-2007 Jive Software, 2018-2019 Florian Schmaus.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -227,7 +227,7 @@ public final class ServiceDiscoveryManager extends Manager {
/** /**
* Returns the type of client that will be returned when asked for the client identity in a * Returns the type of client that will be returned when asked for the client identity in a
* disco request. The valid types are defined by the category client. Follow this link to learn * disco request. The valid types are defined by the category client. Follow this link to learn
* the possible types: <a href="http://xmpp.org/registrar/disco-categories.html#client">Jabber::Registrar</a>. * the possible types: <a href="https://xmpp.org/registrar/disco-categories.html">XMPP Registry for Service Discovery Identities</a>
* *
* @return the type of client that will be returned when asked for the client identity in a * @return the type of client that will be returned when asked for the client identity in a
* disco request. * disco request.
@ -271,8 +271,8 @@ public final class ServiceDiscoveryManager extends Manager {
*/ */
public Set<DiscoverInfo.Identity> getIdentities() { public Set<DiscoverInfo.Identity> getIdentities() {
Set<Identity> res = new HashSet<>(identities); Set<Identity> res = new HashSet<>(identities);
// Add the default identity that must exist // Add the main identity that must exist
res.add(defaultIdentity); res.add(identity);
return Collections.unmodifiableSet(res); return Collections.unmodifiableSet(res);
} }

View file

@ -263,7 +263,7 @@ public class DiscoverInfo extends IQ implements TypedCloneable<DiscoverInfo> {
* Represents the identity of a given XMPP entity. An entity may have many identities but all * Represents the identity of a given XMPP entity. An entity may have many identities but all
* the identities SHOULD have the same name.<p> * the identities SHOULD have the same name.<p>
* *
* Refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a> * Refer to <a href="https://xmpp.org/registrar/disco-categories.html">XMPP Registry for Service Discovery Identities</a>
* in order to get the official registry of values for the <i>category</i> and <i>type</i> * in order to get the official registry of values for the <i>category</i> and <i>type</i>
* attributes. * attributes.
* *
@ -327,7 +327,7 @@ public class DiscoverInfo extends IQ implements TypedCloneable<DiscoverInfo> {
/** /**
* Returns the entity's category. To get the official registry of values for the * Returns the entity's category. To get the official registry of values for the
* 'category' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a> * 'category' attribute refer to <a href="https://xmpp.org/registrar/disco-categories.html">XMPP Registry for Service Discovery Identities</a>.
* *
* @return the entity's category. * @return the entity's category.
*/ */
@ -346,7 +346,7 @@ public class DiscoverInfo extends IQ implements TypedCloneable<DiscoverInfo> {
/** /**
* Returns the entity's type. To get the official registry of values for the * Returns the entity's type. To get the official registry of values for the
* 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a> * 'type' attribute refer to <a href="https://xmpp.org/registrar/disco-categories.html">XMPP Registry for Service Discovery Identities</a>.
* *
* @return the entity's type. * @return the entity's type.
*/ */

View file

@ -48,7 +48,7 @@ public final class JingleTransportMethodManager extends Manager {
super(connection); super(connection);
} }
public static JingleTransportMethodManager getInstanceFor(XMPPConnection connection) { public static synchronized JingleTransportMethodManager getInstanceFor(XMPPConnection connection) {
JingleTransportMethodManager manager = INSTANCES.get(connection); JingleTransportMethodManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new JingleTransportMethodManager(connection); manager = new JingleTransportMethodManager(connection);

View file

@ -38,7 +38,7 @@ public final class JingleIBBTransportManager extends JingleTransportManager<Jing
JingleContentProviderManager.addJingleContentTransportProvider(getNamespace(), new JingleIBBTransportProvider()); JingleContentProviderManager.addJingleContentTransportProvider(getNamespace(), new JingleIBBTransportProvider());
} }
public static JingleIBBTransportManager getInstanceFor(XMPPConnection connection) { public static synchronized JingleIBBTransportManager getInstanceFor(XMPPConnection connection) {
JingleIBBTransportManager manager = INSTANCES.get(connection); JingleIBBTransportManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new JingleIBBTransportManager(connection); manager = new JingleIBBTransportManager(connection);

View file

@ -63,7 +63,7 @@ public final class JingleS5BTransportManager extends JingleTransportManager<Jing
JingleContentProviderManager.addJingleContentTransportProvider(getNamespace(), new JingleS5BTransportProvider()); JingleContentProviderManager.addJingleContentTransportProvider(getNamespace(), new JingleS5BTransportProvider());
} }
public static JingleS5BTransportManager getInstanceFor(XMPPConnection connection) { public static synchronized JingleS5BTransportManager getInstanceFor(XMPPConnection connection) {
JingleS5BTransportManager manager = INSTANCES.get(connection); JingleS5BTransportManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new JingleS5BTransportManager(connection); manager = new JingleS5BTransportManager(connection);

View file

@ -102,7 +102,7 @@ public final class MoodManager extends Manager {
}); });
} }
public static MoodManager getInstanceFor(XMPPConnection connection) { public static synchronized MoodManager getInstanceFor(XMPPConnection connection) {
MoodManager manager = INSTANCES.get(connection); MoodManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new MoodManager(connection); manager = new MoodManager(connection);

View file

@ -620,7 +620,7 @@ public final class JingleSession extends JingleNegotiator implements MediaReceiv
* A XMPP connection * A XMPP connection
* @return a Jingle session * @return a Jingle session
*/ */
public static JingleSession getInstanceFor(XMPPConnection con) { public static synchronized JingleSession getInstanceFor(XMPPConnection con) {
if (con == null) { if (con == null) {
throw new IllegalArgumentException("XMPPConnection cannot be null"); throw new IllegalArgumentException("XMPPConnection cannot be null");
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Florian Schmaus, 2018 Paul Schaub. * Copyright 2017-2019 Florian Schmaus, 2018 Paul Schaub.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -189,7 +189,7 @@ public final class OpenPgpManager extends Manager {
* @param connection xmpp connection. * @param connection xmpp connection.
* @return instance of the manager. * @return instance of the manager.
*/ */
public static OpenPgpManager getInstanceFor(XMPPConnection connection) { public static synchronized OpenPgpManager getInstanceFor(XMPPConnection connection) {
OpenPgpManager manager = INSTANCES.get(connection); OpenPgpManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new OpenPgpManager(connection); manager = new OpenPgpManager(connection);

View file

@ -135,7 +135,7 @@ public final class OXInstantMessagingManager extends Manager {
* @param connection XMPP connection * @param connection XMPP connection
* @return manager instance * @return manager instance
*/ */
public static OXInstantMessagingManager getInstanceFor(XMPPConnection connection) { public static synchronized OXInstantMessagingManager getInstanceFor(XMPPConnection connection) {
OXInstantMessagingManager manager = INSTANCES.get(connection); OXInstantMessagingManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {