控制資源每秒被訪問次數的實現

在平時做接口開發時,可能遇到需要控制接口訪問頻率的需求,比如某個接口每秒最多允許被訪問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();
        }
    }
}

該博文爲本人筆記,僅供大家參考,有不妥之處歡迎大家指正。謝謝!

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