public Task <SpannerClient> AcquireClientAsync(ISpannerClientFactory clientFactory, Logger logger) { Task <SpannerClient> result; lock (_sync) { var snapshotMaximumChannels = SpannerOptions.Instance.MaximumGrpcChannels; //first ensure that the pool is of the correct size. while (_clientPriorityList.Count > snapshotMaximumChannels) { _clientPriorityList.RemoveLast(); } while (_clientPriorityList.Count < snapshotMaximumChannels) { var newEntry = new SpannerClientCreator(_key); _clientPriorityList.Add(newEntry); } //now grab the first item in the sorted list, increment refcnt, re-sort and return. //The re-sorting will happen as a consequence of AcquireClientAsync changing its //state and firing an event the prioritylist listens to. result = _clientPriorityList.GetTop().AcquireClientAsync(clientFactory, logger); } return(result); }
public async Task <SpannerClient> AcquireClientAsync(ISpannerClientFactory clientFactory) { if (_creationTask == null || _creationTask.Value.IsFaulted) { //retry an already failed task. _creationTask = new Lazy <Task <SpannerClient> >( () => clientFactory.CreateClientAsync(_parentKey.Endpoint, _parentKey.Credential)); } var spannerClient = await _creationTask.Value.ConfigureAwait(false); Interlocked.Increment(ref _refCount); OnPriorityChanged(); return(spannerClient); }
// ReSharper disable once MemberCanBePrivate.Global internal ClientPool(ISpannerClientFactory clientFactory = null) { _clientFactory = clientFactory ?? SpannerClientFactory.Default; }