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

后端 未结 10 2100
情歌与酒
情歌与酒 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条回答
  •  渐次进展
    2020-12-07 10:29

    Why not just use a circular array? We have 3600 elements in that array.

    index = 0;
    Array[index % 3600] = count_in_one_second. 
    ++index;
    

    if you want last second, return the last element of this array. if you want last minute, return the sum of last 60 elements. if you want last hour, return the sum of the whole array (3600 elements).

    Isn't his a simple and effective solution?

    Thanks

    Deryk

提交回复
热议问题