Example #1
0
        public async Task <JimuRemoteCallResultData> InvokeAsync(JimuServiceRoute service, IDictionary <string, object> paras,
                                                                 string token)
        {
            if (paras == null)
            {
                paras = new ConcurrentDictionary <string, object>();
            }
            var address = await _addressSelector.GetAddressAsyn(service);

            if (_retryTimes < 0)
            {
                _retryTimes = service.Address.Count;
            }
            JimuRemoteCallResultData result = null;
            var retryPolicy = Policy.Handle <TransportException>()
                              .RetryAsync(_retryTimes,
                                          async(ex, count) =>
            {
                address = await _addressSelector.GetAddressAsyn(service);
                _logger.Debug(
                    $"FaultHandling,retry times: {count},serviceId: {service.ServiceDescriptor.Id},Address: {address.Code},RemoteServiceCaller excute retry by Polly for exception {ex.Message}");
            });
            var fallbackPolicy = Policy <JimuRemoteCallResultData> .Handle <TransportException>()
                                 .FallbackAsync(new JimuRemoteCallResultData()
            {
                ErrorCode = "500", ErrorMsg = "error occur when communicate with server. server maybe have been down."
            })
                                 .WrapAsync(retryPolicy);

            return(await fallbackPolicy.ExecuteAsync(async() =>
            {
                var client = _transportClientFactory.CreateClient(address);
                if (client == null)
                {
                    return new JimuRemoteCallResultData
                    {
                        ErrorCode = "400",
                        ErrorMsg = "Server unavailable!"
                    }
                }
                ;
                _logger.Debug($"invoke: serviceId:{service.ServiceDescriptor.Id}, parameters count: {paras.Count()}, token:{token}");
                //Polly.Policy.Handle<>()

                result = await client.SendAsync(new JimuRemoteCallData
                {
                    Parameters = paras,
                    ServiceId = service.ServiceDescriptor.Id,
                    Token = token,
                    Descriptor = service.ServiceDescriptor
                });
                return result;
            }));
        }
Example #2
0
        public Task <JimuRemoteCallResultData> InvokeAsync(RemoteCallerContext context)
        {
            if (_retryTimes <= 0)
            {
                _retryTimes = context.Service.Address.Count;
            }
            var retryPolicy = Policy.Handle <TransportException>()
                              .RetryAsync(_retryTimes,
                                          async(ex, count) =>
            {
                context.ServiceAddress = await _addressSelector.GetAddressAsyn(context.Service);
                _logger.Debug(
                    $"FaultHandling,retry times: {count},serviceId: {context.Service.ServiceDescriptor.Id},Address: { context.ServiceAddress.Code},RemoteServiceCaller execute retry by Polly for exception {ex.Message}");
            });
            var fallbackPolicy = Policy <JimuRemoteCallResultData>
                                 .Handle <TransportException>()
                                 .FallbackAsync(new JimuRemoteCallResultData()
            {
                ErrorCode = "500",
                ErrorMsg  = "error occur when communicate with server. server maybe have been down."
            })
                                 .WrapAsync(retryPolicy);

            return(fallbackPolicy.ExecuteAsync(() =>
            {
                return _next(context);
            }));
        }