Don't try to reconnect if the connection was closed cleanly.

This commit is contained in:
akwizgran
2020-05-25 17:15:00 +01:00
parent 35d1b406f7
commit 6eb77465f6
8 changed files with 116 additions and 55 deletions

View File

@@ -136,7 +136,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
@Override
public void unregisterConnection(ContactId c, TransportId t,
InterruptibleConnection conn, boolean incoming) {
InterruptibleConnection conn, boolean incoming, boolean exception) {
if (LOG.isLoggable(INFO)) {
if (incoming) LOG.info("Incoming connection unregistered: " + t);
else LOG.info("Outgoing connection unregistered: " + t);
@@ -148,7 +148,8 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
throw new IllegalArgumentException();
lastConnection = recs.isEmpty();
}
eventBus.broadcast(new ConnectionClosedEvent(c, t, incoming));
eventBus.broadcast(
new ConnectionClosedEvent(c, t, incoming, exception));
if (lastConnection) {
LOG.info("Contact disconnected");
eventBus.broadcast(new ContactDisconnectedEvent(c));
@@ -178,8 +179,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
@Override
public Collection<ContactId> getConnectedOrBetterContacts(TransportId t) {
synchronized (lock) {
List<TransportId> better = betterTransports.get(t);
if (better == null) better = emptyList();
List<TransportId> better = getBetterTransports(t);
List<ContactId> contactIds = new ArrayList<>();
for (Entry<ContactId, List<ConnectionRecord>> e :
contactConnections.entrySet()) {

View File

@@ -74,12 +74,13 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
createIncomingSession(ctx, reader, handler).run();
reader.dispose(false, true);
interruptOutgoingSession();
connectionRegistry.unregisterConnection(contactId, transportId,
this, true, false);
} catch (DbException | IOException e) {
logException(LOG, WARNING, e);
onReadError(true);
} finally {
connectionRegistry.unregisterConnection(contactId, transportId,
this, true);
this, true, true);
}
}

View File

@@ -118,12 +118,13 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
createIncomingSession(ctx, reader, handler).run();
reader.dispose(false, true);
interruptOutgoingSession();
connectionRegistry.unregisterConnection(contactId, transportId,
this, false, false);
} catch (DbException | IOException e) {
logException(LOG, WARNING, e);
onReadError();
} finally {
connectionRegistry.unregisterConnection(contactId, transportId,
this, false);
this, false, true);
}
}

View File

@@ -98,8 +98,8 @@ class PollerImpl implements Poller, EventListener {
ConnectionClosedEvent c = (ConnectionClosedEvent) e;
// Reschedule polling, the polling interval may have decreased
reschedule(c.getTransportId());
if (!c.isIncoming()) {
// Connect to the disconnected contact
// If an outgoing connection failed, try to reconnect
if (!c.isIncoming() && c.isException()) {
connectToContact(c.getContactId(), c.getTransportId());
}
} else if (e instanceof ConnectionOpenedEvent) {