identifyWait method
IdentifyWait triggers an identify (if the connection has not already been identified) and returns a future that completes when the identify protocol completes.
Implementation
@override
Future<void> identifyWait(Conn conn) async {
final peerId = conn.remotePeer;
final identifyWaitStart = DateTime.now();
Completer<void>? completerToAwait;
await _connsMutex.synchronized( () async {
final mutexAcquiredTime = DateTime.now().difference(identifyWaitStart);
var entry = _conns[conn];
if (entry != null) {
if (entry.identifyWaitCompleter != null && !entry.identifyWaitCompleter!.isCompleted) {
completerToAwait = entry.identifyWaitCompleter;
// No need to spawn _identifyConn again if one is already running for this entry.
} else {
entry.identifyWaitCompleter = Completer<void>();
completerToAwait = entry.identifyWaitCompleter;
// Spawn _identifyConn as this is a new request for this entry or previous one completed/failed.
_spawnIdentifyConn(conn, entry);
}
} else {
if (conn.isClosed) {
_log.warning(' [IDENTIFY-WAIT-PHASE-1-CONN-CLOSED] Connection to peer=$peerId is already closed. Not creating entry or starting identify.');
// Completer to await will remain null, function will return.
return;
}
entry = _addConnWithLock(conn); // _addConnWithLock returns the new/existing entry
entry.identifyWaitCompleter = Completer<void>();
completerToAwait = entry.identifyWaitCompleter;
_spawnIdentifyConn(conn, entry);
}
});
final mutexReleasedTime = DateTime.now().difference(identifyWaitStart);
if (completerToAwait == null) {
_log.warning(' [IDENTIFY-WAIT-NO-COMPLETER] No completer to await for peer=$peerId (e.g., connection was closed). Identify will not complete.');
return; // Or throw, depending on desired behavior for closed conns.
}
try {
await completerToAwait!.future;
final totalDuration = DateTime.now().difference(identifyWaitStart);
} catch (e, st) {
final totalDuration = DateTime.now().difference(identifyWaitStart);
_log.warning(' [IDENTIFY-WAIT-ERROR] Identify completer for peer=$peerId completed with error, total_duration=${totalDuration.inMilliseconds}ms, error=$e\n$st');
// The error should have been emitted by _spawnIdentifyConn.
// Rethrow if this path needs to signal failure upwards.
// For now, just log, as the event bus should have handled it.
}
}