doum
13 小时以前 36f691267e45ca2861bed663fdcf5f2efcefdfce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.doumee.core.jiandaoyun.util;
 
import java.util.Arrays;
import java.util.List;
 
public class LimitUtil {
    private static volatile int seq = 0;
 
    private static final int bucket = 1000;
 
    private static volatile List<Long> requestTimeList = Arrays.asList(0L, 0L, 0L, 0L, 0L);
 
    private LimitUtil() {
    }
 
    /**
     * 限流实现 用 static synchronized 修饰 表示 对类上锁
     *
     * @throws InterruptedException
     */
    public static synchronized void tryBeforeRun() throws InterruptedException {
        long now = System.currentTimeMillis();
        // 当前时间 与 前limit个请求的时间 作比较
        long interval = now - requestTimeList.get(seq);
        if (interval < 0) {
            // 执行时间: t[seq]+bucket=now-interval+bucket
            // 等待时间: bucket-interval
            Thread.sleep(bucket - interval);
            // 重新等待
            tryBeforeRun();
        }
        if (interval < bucket) {
            requestTimeList.set(seq, requestTimeList.get(seq) + bucket);
            Thread.sleep(bucket - interval);
        } else {
            requestTimeList.set(seq, now);
        }
        seq = (seq + 1) % requestTimeList.size();
    }
}