how to count number of requests in last second, minute and hour

后端 未结 10 2031
情歌与酒
情歌与酒 2020-12-07 09:35

There is a hypothetical web server which supports only one very simple API - count of requests received in the last hour, minute and second. This server is very popular in t

10条回答
  •  -上瘾入骨i
    2020-12-07 10:23

    To do this for time window of T seconds, have a queue data structure where you queue the timestamps of individual requests as they arrive. When you want to read the number of requests arrived during the most recent window of T seconds, first drop from the "old" end of the queue those timestamps that are older than T seconds, then read the size of the queue. You should also drop elements whenever you add a new request to the queue to keep its size bounded (assuming bounded rate for incoming requests).

    This solution works up to arbitrary precision, e.g. millisecond accuracy. If you are content with returning approximate answers, you can e.g. for time window of T = 3600 (an hour), consolidate requests coming within same second into a single queue element, making queue size bounded by 3600. I think that would be more than fine, but theoretically loses accuracy. For T = 1, you can do consolidation on millisecond level if you want.

    In pseudocode:

    queue Q
    
    proc requestReceived()
      Q.insertAtFront(now())
      collectGarbage()
    
    proc collectGarbage()
      limit = now() - T
      while (! Q.empty() && Q.lastElement() < limit)
        Q.popLast()
    
    proc count()
      collectGarbage()
      return Q.size()
    

提交回复
热议问题