4.如何做监控统计

自然分钟很简单,不是自然分钟呢?

下面是伪代码:

    function circuitBreaker($count, $limitTime = 60)
    {
        $redisKey = "test_redis";
        $currentTime = time();
        $length = $this->redis->lPush($redisKey, $currentTime);
        // 避免只入不出, 削个峰
        if ($length >= $count) {
            if ($length > $count * 1.5) {
                $this->redis->lTrim($redisKey, 0, $count - 1);
            }
            $previousTime = $this->redis->rPop($redisKey);
            if ($previousTime && ($currentTime - $previousTime <= $limitTime)) {
                return true;
            }
        }
        return false;
    }

Last updated