控制资源每秒被访问次数的实现

在平时做接口开发时,可能遇到需要控制接口访问频率的需求,比如某个接口每秒最多允许被访问100次,我们可以借助并发工具类中的Semaphore实现,以下为示例代码:

public class TPSLimiter {

    private Semaphore semaphore = null;

    public OPSLimiter(int maxOps) {
        if (maxOps < 1) {
            throw new IllegalArgumentException("maxOps must be greater than zero");
        }
        this.semaphore = new Semaphore(maxOps);
        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() ->
                // 每秒释放给定数目的许可,将其返回到信号量
                semaphore.release(maxOps), 1000, 1000, TimeUnit.MILLISECONDS);
    }

    /**
     * 调用接口之前先调用此方法,当超过最大ops时该方法会阻塞
     */
    public void await() {
        semaphore.acquireUninterruptibly(1);
    }
}

以下为测试代码:

public class TPSLimiterTest {
    static AtomicInteger at = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        TPSLimiter limiter = new TPSLimiter(100);

        new Thread(() -> {
            while (true) {
                // 每秒输出一次AtomicInteger的最新值
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(at.get());
            }
        }).start();

        // 启动100个线程对AtomicInteger进行累加,为了方便就没有使用线程池
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                while (true) {
                    // 每次累加操作前先调用await方法,超过设定的ops时会阻塞线程
                    limiter.await();
                    at.incrementAndGet();
                }
            }).start();
        }
    }
}

该博文为本人笔记,仅供大家参考,有不妥之处欢迎大家指正。谢谢!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章