public async Task <List <TRslt> > DoAsync(AbstractRequestInstructions <TRslt, TReq, TRspns> request,
                                                  List <IHost> hosts, IAggregator <TRslt> gatherer)
        {
            if (request.ClientManager == null)
            {
                throw new ArgumentNullException($"{nameof(request.ClientManager)}");
            }
            var listResults = new List <ResultEnvelope <TRslt> >();
            var taskBag     = new List <Task>();
            var cts         = new CancellationTokenSource();

            cts.CancelAfter(request.Timeout);
            foreach (var host in hosts)
            {
                var client = request.ClientManager.GetClient(host);
                if (client != null)
                {
                    taskBag.Add(client.SendAsync(request.Request, cts.Token)
                                .ContinueWith(x => listResults.Add(request.ResponseTransformer(x.Result)), cts.Token));
                }
            }
            await Task.WhenAll(taskBag).ConfigureAwait(false);

            var result = gatherer.Aggregate(listResults);

            return(result.Select(x => x.Result).ToList());
        }
Esempio n. 2
0
 private WorkloadType GetTypeOfWorkload <TRslt, TReq, TRspns>(AbstractRequestInstructions <TRslt, TReq, TRspns> request) where TReq : IRequest
 {
     if (request is HttpRequestInstructions <TRslt> )
     {
         return(WorkloadType.Http);
     }
     return(WorkloadType.Unsupported);
 }
Esempio n. 3
0
 public async Task <List <TRslt> > MulticastAsync <TRslt, TReq, TRspns>(AbstractRequestInstructions <TRslt, TReq, TRspns> request,
                                                                        List <IHost> hosts, IAggregator <TRslt> gatherer) where TReq : IRequest
 {
     if (GetTypeOfWorkload(request) == WorkloadType.Http)
     {
         if (hosts?.All(x => x.GetType() == typeof(HttpHost)) ?? false)
         {
             return(await(new HttpWorkload().DoHttpRequestForSeveralRecipientsAsync(request as HttpRequestInstructions <TRslt>,
                                                                                    hosts.Select(x => new Uri(x.Host.ToString())).ToList(), gatherer)));
         }
     }
     else
     {
         return(await(new BaseWorkload <TRslt, TReq, TRspns>().DoAsync(request, hosts, gatherer).ConfigureAwait(false)));
     }
     throw new NotImplementedException("Unable to parse the type of workload");
 }