public async Task <bool> TryHandleRequest(int clientId) { if (!ClientsRequestsData.ContainsKey(clientId)) { ClientsRequestsData[clientId] = new ClientRequestsData(clientId); } ClientRequestsData client = ClientsRequestsData[clientId]; return(await Task.Run(() => IsRequestAccepted(client))); }
private bool IsRequestAccepted(ClientRequestsData client) { lock (client) { int diffInSeconds = (DateTime.Now - client.TimeFrameStart).Seconds; if (diffInSeconds <= TimeFrameInSeconds) { client.CurrentRequests++; return(client.CurrentRequests <= MaxRequestsInTimeFrame); } client.ResetTimeFrame(); return(true); } }