public async Task RemoveModuleAsync(ModuleIdentifier module, CancellationToken cancellation) { if (module == default) { throw new ArgumentDefaultException(nameof(module)); } var session = await _coordinationManager.GetSessionAsync(cancellation); var runningModulePath = GetRunningModulePath(module, session); var entry = await _coordinationManager.GetAsync(runningModulePath, cancellation); if (entry == null) { return; } await _coordinationManager.DeleteAsync(runningModulePath, cancellation : cancellation); var(endPoint, prefixes) = ReadRunningModuleEntry(entry); foreach (var prefix in prefixes) { var prefixPath = GetPrefixPath(prefix, endPoint, session, normalize: false); await _coordinationManager.DeleteAsync(prefixPath, cancellation : cancellation); } }
public async Task RemoveRoutesAsync(EndPointAddress endPoint, bool removePersistentRoutes, CancellationToken cancellation) { if (endPoint == default) { throw new ArgumentDefaultException(nameof(endPoint)); } var session = (await _coordinationManager.GetSessionAsync(cancellation)).ToString(); var path = GetReversePath(session, endPoint); var reverseEntry = await _coordinationManager.GetAsync(path, cancellation); if (reverseEntry == null) { return; } var tasks = new List <Task>(capacity: reverseEntry.Children.Count); foreach (var reverseRouteEntry in await reverseEntry.GetChildrenEntriesAsync(cancellation)) { var route = new Route(reverseRouteEntry.Name.Segment.ConvertToString()); var routePath = GetPath(route, endPoint, session); if (!removePersistentRoutes) { using (var stream = reverseRouteEntry.OpenStream()) using (var reader = new BinaryReader(stream)) { var registrationOptions = (RouteRegistrationOptions)reader.ReadInt32(); if (!registrationOptions.IncludesFlag(RouteRegistrationOptions.Transient)) { continue; } var reverseRouteEntryDeletion = _coordinationManager.DeleteAsync(reverseRouteEntry.Path, recursive: true, cancellation: cancellation); tasks.Add(reverseRouteEntryDeletion.AsTask()); } } var routeEntryDeletion = _coordinationManager.DeleteAsync(routePath, cancellation: cancellation); tasks.Add(routeEntryDeletion.AsTask()); } await Task.WhenAll(tasks); if (removePersistentRoutes) { await _coordinationManager.DeleteAsync(path, recursive : true, cancellation : cancellation); } }
private async Task <bool> ValidateClientCoreAsync(EndPointAddress endPoint, string securityToken, CancellationToken cancellation) { var now = _dateTimeProvider.GetCurrentTime(); var path = GetPath(endPoint); do { var entry = await _coordinationManager.GetAsync(path, cancellation : cancellation); if (entry == null) { _logger?.LogDebug($"Cannot update session for client with end-point '{endPoint.ToString()}'. Session is terminated."); return(false); } var(comparandSecurityToken, leaseEnd) = DecodePayload(entry.Value.Span); // We have to assume that the client is not connected anymore. // This is a race condition, that has to be prevented. if (now >= leaseEnd) { await _coordinationManager.DeleteAsync(entry.Path, cancellation : cancellation); _logger?.LogDebug($"Session for client with end-point '{endPoint.ToString()}' is terminated. Removing entry."); _logger?.LogDebug($"Cannot update session for client with end-point '{endPoint.ToString()}'. Session is terminated."); return(false); } if (securityToken != comparandSecurityToken) { _logger?.LogDebug($"Cannot update session for client with end-point '{endPoint.ToString()}'. Session is terminated."); return(false); } var newLeaseEnd = now + Timeout; if (newLeaseEnd > leaseEnd) { leaseEnd = newLeaseEnd; } var securityTokenBytesCount = Encoding.UTF8.GetByteCount(securityToken); using (ArrayPool <byte> .Shared.Rent(8 + 4 + securityTokenBytesCount, out var bytes)) { var payloadLength = EncodePayload(bytes, securityToken.AsSpan(), leaseEnd); var payload = bytes.AsMemory().Slice(start: 0, payloadLength); endPoint = new EndPointAddress("client/" + Guid.NewGuid().ToString()); var version = await _coordinationManager.SetValueAsync(path, payload, version : entry.Version, cancellation : cancellation); if (version == entry.Version) { _logger?.LogDebug($"Updated session for client with end-point '{endPoint.ToString()}'."); return(true); } } }while (true); }