private static async Task <TOut> RequestRetryPolicy <TOut>(FlurlHttpException flurlException, IFlurlRequest flurlRequest, WebRequestContext requestContext) { TOut result = default; if (flurlException.Call.Response != null && flurlException.Call.Response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) { _exceptionHandler?.Invoke(flurlException); } else if (flurlException.Call.Response != null)// 재시도 { if (requestContext.NeedEncrypt) { result = await EncryptSendAsync <TOut>(flurlRequest, requestContext); } else { result = await SendAsync <TOut>(flurlRequest, requestContext); } } return(result); }
public static T Try <T>(int howHard, Func <T> createFunc, ExceptionHandlerDelegate exceptionHandler) { var failCount = 0; while (true) { try { return(createFunc()); } catch (Exception e) { failCount++; var timeToThrow = failCount >= howHard; exceptionHandler?.Invoke(e, failCount, howHard, timeToThrow); if (timeToThrow) { throw; } } } }
public static void Try(int howHard, Action createAction, ExceptionHandlerDelegate exceptionHandler) { var failCount = 0; while (true) { try { createAction(); return; } catch (Exception e) { failCount++; var timeToThrow = failCount >= howHard; exceptionHandler?.Invoke(e, failCount, howHard, timeToThrow); if (timeToThrow) { throw; } } } }
private static async Task <TOut> SendAsync <TOut>(IFlurlRequest flurlRequest, WebRequestContext requestContext) { TOut result = default; try { requestContext.AttemptCnt++; if (requestContext.MethodType == WebMethodType.GET) { if (requestContext.SerializeType == SerializeType.Json) { result = await flurlRequest.GetJsonAsync <TOut>(); } else { var rawData = await flurlRequest.GetBytesAsync(); result = MessagePackSerializer.Deserialize <TOut>(rawData); } } else if (requestContext.MethodType == WebMethodType.POST) { if (requestContext.SerializeType == SerializeType.Json) { result = await flurlRequest .PostJsonAsync((string)requestContext) .ReceiveJson <TOut>(); } else { var rawData = await flurlRequest .PostAsync(new ByteArrayContent(requestContext)) .ReceiveBytes(); result = MessagePackSerializer.Deserialize <TOut>(rawData); } } } catch (FlurlHttpException flurlException) { if (requestContext.AttemptCnt < WebConfig.ReTryCount) { result = await RequestRetryPolicy <TOut>(flurlException, flurlRequest, requestContext); } else { if (_exceptionHandler != null) { await _exceptionHandler.Invoke(flurlException); } } } catch (Exception ex) { if (_exceptionHandler != null) { await _exceptionHandler.Invoke(ex); } } return(result); }