Filter out generated error messages during shutdown

This commit is contained in:
Andrea Cavalli 2021-03-06 17:27:08 +01:00
parent 2339ae7284
commit ad8d48ecb1

View File

@ -1,6 +1,12 @@
package it.tdlight.common; package it.tdlight.common;
import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -37,11 +43,15 @@ public class InternalClientManager implements AutoCloseable {
if (handler != null) { if (handler != null) {
handler.handleEvents(isClosed, clientEventIds, clientEvents); handler.handleEvents(isClosed, clientEventIds, clientEvents);
} else { } else {
logger.error("Unknown client id \"{}\"! {} events have been dropped!", clientId, clientEvents.length); java.util.List<Entry<Long, TdApi.Object>> droppedEvents = getEffectivelyDroppedEvents(clientEventIds, clientEvents);
for (int i = 0; i < clientEvents.length; i++) {
long id = clientEventIds[i]; if (!droppedEvents.isEmpty()) {
TdApi.Object event = clientEvents[i]; logger.error("Unknown client id \"{}\"! {} events have been dropped!", clientId, droppedEvents.size());
logger.error("The following event, with id \"{}\", has been dropped: {}", id, event); for (Entry<Long, Object> droppedEvent : droppedEvents) {
logger.error("The following event, with id \"{}\", has been dropped: {}",
droppedEvent.getKey(),
droppedEvent.getValue());
}
} }
} }
@ -50,6 +60,28 @@ public class InternalClientManager implements AutoCloseable {
} }
} }
/**
* Get only events that have been dropped, ignoring synthetic errors related to the closure of a client
*/
private List<Entry<Long, TdApi.Object>> getEffectivelyDroppedEvents(long[] clientEventIds, TdApi.Object[] clientEvents) {
java.util.List<Entry<Long, TdApi.Object>> droppedEvents = new ArrayList<>(clientEvents.length);
for (int i = 0; i < clientEvents.length; i++) {
long id = clientEventIds[i];
TdApi.Object event = clientEvents[i];
boolean mustPrintError = true;
if (event instanceof TdApi.Error) {
var errorEvent = (TdApi.Error) event;
if (Objects.equals("Request aborted", errorEvent.message)) {
mustPrintError = false;
}
}
if (mustPrintError) {
droppedEvents.add(Map.entry(id, event));
}
}
return droppedEvents;
}
public void registerClient(int clientId, ClientEventsHandler internalClient) { public void registerClient(int clientId, ClientEventsHandler internalClient) {
boolean replaced = registeredClientEventHandlers.put(clientId, internalClient) != null; boolean replaced = registeredClientEventHandlers.put(clientId, internalClient) != null;
if (replaced) { if (replaced) {