public static Task <IGetReplicaResult> GetAnyReplicaAsync(this ICouchbaseCollection collection, string id, Action <GetAnyReplicaOptions> configureOptions)
        {
            var options = new GetAnyReplicaOptions();

            configureOptions(options);

            return(collection.GetAnyReplicaAsync(id, options));
        }
        public async Task <IGetReplicaResult> GetAnyReplicaAsync(string id, GetAnyReplicaOptions options)
        {
            var vBucket = (VBucket)_bucket.KeyMapper.MapKey(id);

            if (!vBucket.HasReplicas)
            {
                Log.LogWarning($"Call to GetAnyReplica for key [{id}] but none are configured. Only the active document will be retrieved.");
            }

            var tasks = new List <Task <IGetReplicaResult> >(vBucket.Replicas.Length + 1);

            var transcoder = options.Transcoder ?? _transcoder;

            // get primary
            tasks.Add(GetPrimary(id, options.CancellationToken, transcoder));

            // get replicas
            tasks.AddRange(vBucket.Replicas.Select(index => GetReplica(id, index, options.CancellationToken, transcoder)));

            return(await Task.WhenAny(tasks).ConfigureAwait(false).GetAwaiter().GetResult());
        }