public async Task <T> Call <T>(ITgFunc <T> func) { TgCallHandler <T> handler = f => Transport.Call(f); handler = CallMiddlewareChain.Apply(handler); // TODO: refactor it const int attemptsCount = 10; var currentAttempt = 1; while (true) { try { var respTask = await handler(func); return(await respTask); } catch (TgBadSaltException) when(currentAttempt < attemptsCount) { } catch (TgRpcBadMsgException e) when(currentAttempt < attemptsCount && e.ErrorCode == TgRpcBadMsgCodes.MsgSeqNoLow) { } currentAttempt++; } }
public TgCallHandler <T> Handle <T>(TgCallHandler <T> next) => func => _taskQueue.Put(async() => { var timeSinceLastReq = DateTime.Now - _lastReqTimestamp; var isDelayNeeded = timeSinceLastReq.TotalMilliseconds < MaxMsDelay && !IsIgnored(func); if (isDelayNeeded) { await Task.Delay(Rnd.NextInt32(MinMsDelay, MaxMsDelay)); } _lastReqTimestamp = DateTime.Now; return(await next(func)); });
public TgCallHandler <T> Handle <T>(TgCallHandler <T> next) => async func =>
public TgCallHandler <T> Apply <T>(TgCallHandler <T> handler) => Middleware.Fold(handler, (a, x) => x.Handle(a));