public async Task <CdnClient> GetClientAsync(AppId appId, DepotId depotId, CancellationToken cancellationToken = default) { await _getClientSemaphore.WaitAsync(cancellationToken); if (_cdnClientWrappersAvailable.Count == 0) { await FillServerPoolAsync(); } await _poolFilledEvent.WaitAsync(cancellationToken); CdnClient recommendedClient = null; lock (this) { recommendedClient = _cdnClientWrappersAvailable .OrderByDescending(x => x.ServerWrapper.Score) .ThenBy(x => x.ServerWrapper.Server.WeightedLoad) .ThenBy(x => x.ServerWrapper.Server.Load) .Take(10) .OrderBy(x => _random.Next()) .First(); _cdnClientWrappersAvailable.Remove(recommendedClient); } if (recommendedClient.RequiresAuthentication(appId, depotId)) { await AuthenticateClientInfoAsync(recommendedClient, appId, depotId); } _getClientSemaphore.Release(); return(recommendedClient); }