/// <summary> /// 异步结果返回,回调继续 /// </summary> /// <param name="result"></param> protected virtual void AsyncBackResult(Result result) { if (AsyncResultDict.TryRemove(result.Id, out ManualResetValueTaskSource <Result> asyncback)) { asyncback.SetResult(result); } else { if (result.IsError) { try { Log.Error($"ErrorType:{(ErrorType)result.ErrorId} ErrMsg:\r\n{result.ErrorMsg} "); } catch { Log.Error($"ErrorType:{result.ErrorId} ErrMsg:\r\n{result.ErrorMsg} "); } } else { Log.ErrorFormat("not find back ruest id:{0}", result.Id); } } }
/// <summary> /// 处理超时请求 /// </summary> public virtual void RequestTimeOutHandle() { while (RequestOutTimeQueue.Count > 0) { if (RequestOutTimeQueue.TryPeek(out RequestKeyTime keyTime)) { long outtime = RequestOutTime * 10000; if ((TimeHelper.GetTime() - keyTime.Time) > outtime) { if (RequestOutTimeQueue.TryDequeue(out keyTime)) { if (AsyncResultDict.ContainsKey(keyTime.Key)) { Task.Factory.StartNew(() => { AsyncBackResult(new Result() { Id = keyTime.Key, ErrorMsg = "time out", ErrorId = (int)ErrorType.TimeOut }); }); } } } else { break; } } else { break; } } }
/// <summary> /// 异步结果返回,回调继续 /// </summary> /// <param name="result"></param> protected virtual void AsyncBackResult(Result result) { if (AsyncResultDict.TryRemove(result.Id, out ManualResetValueTaskSource <Result> asyncback)) { if (asyncback.GetStatus(asyncback.Version) == ValueTaskSourceStatus.Pending) { asyncback.SetResult(result); } } else { if (result.IsError) { try { Log.ErrorFormat("ErrorType:{ErrorId} ErrMsg:\r\n{ErrorMsg}" , (ErrorType)result.ErrorId , result?.ErrorMsg ?? "null"); } catch { Log.ErrorFormat("ErrorType:{ErrorId} ErrMsg:\r\n{ErrorMsg}" , result.ErrorId , result?.ErrorMsg ?? "null"); } } else { Log.ErrorFormat("not find back ruest id:{0}", result.Id); } } }
/// <summary> /// 异步结果返回,回调继续 /// </summary> /// <param name="result"></param> protected virtual void AsyncBackResult(Result result) { if (AsyncResultDict.TryRemove(result.Id, out AsyncResultAwaiter <Result> asyncback)) { asyncback.Completed(result); } else { if (result.IsError) { try { Log.Error($"ErrorType:{(ErrorType)result.ErrorId} ErrMsg:\r\n{result.ErrorMsg} "); } catch { Log.Error($"ErrorType:{result.ErrorId} ErrMsg:\r\n{result.ErrorMsg} "); } } else { Log.ErrorFormat("not find back ruest id:{0}", result.Id); } } }
/// <summary> /// 添加异步回调到表中 /// </summary> /// <param name="Ids"></param> /// <returns></returns> protected virtual ManualResetValueTaskSource <Result> AddAsyncResult(long ids) { ManualResetValueTaskSource <Result> asyncResult = new ManualResetValueTaskSource <Result>(); if (!AsyncResultDict.TryAdd(ids, asyncResult)) { Log.InfoFormat("add async back have id:{ids}", ids); AsyncResultDict[ids] = asyncResult; } if (RequestOutTime > 0) { RequestOutTimeQueue.Enqueue(new RequestKeyTime(ids, TimeHelper.GetTime())); } return(asyncResult); }