diff --git a/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/NetworkPeer.kt b/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/NetworkPeer.kt index 65505dd..6e3d17f 100644 --- a/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/NetworkPeer.kt +++ b/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/NetworkPeer.kt @@ -40,18 +40,22 @@ class NetworkPeer( statistics.direction = direction statistics.opened = Instant.now() connections.withLock { - val connection = SwarmConnection(scope, transportConnection, swarm, resourceManager, streamHandler, nextConnectionId()) + val connection = SwarmConnection(scope, transportConnection, resourceManager, streamHandler, nextConnectionId()) { + swarm.removeConnection(it) + } connections.add(connection) return Ok(connection) } } fun removeConnection(connection: SwarmConnection) { - connections.remove(connection) + connections.withLock { + connections.remove(connection) + } } fun connections(): List { - return connections.toList() + return connections.withLock { connections.toList() } } fun connectedness(): Connectedness { diff --git a/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/Swarm.kt b/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/Swarm.kt index 47962bf..e266d44 100644 --- a/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/Swarm.kt +++ b/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/Swarm.kt @@ -169,11 +169,6 @@ class Swarm( _context.complete() } - internal fun removeConnection(swarmConnection: SwarmConnection) { - getPeer(swarmConnection.remoteIdentity.peerId)?.removeConnection(swarmConnection) - notifyAll { subscriber -> subscriber.disconnected(this, swarmConnection) } - } - private fun bestConnectionToPeer(peerId: PeerId): SwarmConnection? { return getPeer(peerId)?.bestConnectionToPeer() } @@ -204,6 +199,11 @@ class Swarm( } } + internal fun removeConnection(swarmConnection: SwarmConnection) { + getPeer(swarmConnection.remoteIdentity.peerId)?.removeConnection(swarmConnection) + notifyAll { subscriber -> subscriber.disconnected(this, swarmConnection) } + } + internal fun notifyAll(notify: (Subscriber) -> Unit) { subscribersLock.withLock { subscribers.toList() diff --git a/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/SwarmConnection.kt b/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/SwarmConnection.kt index f755b38..b30016e 100644 --- a/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/SwarmConnection.kt +++ b/libp2p-core/src/main/kotlin/org/erwinkok/libp2p/core/network/swarm/SwarmConnection.kt @@ -40,10 +40,10 @@ private val logger = KotlinLogging.logger {} class SwarmConnection( scope: CoroutineScope, private val transportConnection: TransportConnection, - private val swarm: Swarm, private val resourceManager: ResourceManager, private val streamHandler: StreamHandler?, private val identifier: Long, + private val onClose: (SwarmConnection) -> Unit, ) : AwaitableClosable, NetworkConnection { private val _context = Job() private val _streams = closableLockedList() @@ -136,7 +136,7 @@ class SwarmConnection( transportConnection.close() streams.forEach { it.reset() } _context.complete() - swarm.removeConnection(this) + onClose(this) } private fun addStream(muxedStream: MuxedStream, direction: Direction, streamScope: StreamManagementScope): Result { diff --git a/test-plans/src/main/resources/logback.xml b/test-plans/src/main/resources/logback.xml index 4af14cd..9257708 100644 --- a/test-plans/src/main/resources/logback.xml +++ b/test-plans/src/main/resources/logback.xml @@ -14,7 +14,7 @@ - +