gc method
void
gc()
Drops the peer, protocol and per-peer sub-scopes that nothing uses. Runs every minute; a scope that is used again later is made again.
The manager holds one reference on each scope in its maps, so a scope with only that reference and no resources is unused. (Before, that reference kept every scope, and the maps grew by one entry for each peer the node ever saw.)
Implementation
void gc() {
_logger.fine('Running GC...');
for (final scope in _serviceScopes.values) {
scope.gcPeerSubScopes();
}
for (final scope in _protocolScopes.values) {
scope.gcPeerSubScopes();
}
_protocolScopes.removeWhere((id, scope) {
if (_stickyProtocols.contains(id)) return false;
if (scope.peerSubScopeCount == 0 && scope.isUnused(ownRefs: 1)) {
scope.done();
_logger.fine('GC: Removed unused protocol scope: $id');
return true;
}
return false;
});
_peerScopes.removeWhere((id, scope) {
if (_stickyPeers.contains(id)) return false;
if (scope.isUnused(ownRefs: 1)) {
scope.done();
_logger.fine('GC: Removed unused peer scope: $id');
return true;
}
return false;
});
_logger.fine('GC finished.');
}