某个接口有访问频率限制,限制一分钟最多能够访问XX次,在访问代码中增长限流器,以下 java
// 限制每分钟访问次数 int tpm = SystemConfigs.getInt(SysConfigKeys.XXX_TPM); int count = 0, round = 0; List<XXX> list = new ArrayList<XXX>(); long startMillis = System.currentTimeMillis(); while (start.before(end)) {//按时间段不停的访问接口 // 预计时间开销 long predict = round * 60000 / tpm; long duration = System.currentTimeMillis() - startMillis; if(predict > duration) { // 实际耗时小于预计耗时,sleep long waitting = predict - duration; try { Thread.sleep(waitting); LOG.debug("XXX同步休眠{}ms. ", waitting); } catch (InterruptedException e) { LOG.error("XXX访问频率限制失败:", e); } } round ++; count += this.syncData(start); start = DateUtils.addDay(start, 1); } LOG.info("总共同步了{}条数据!", count); return "XXX同步了" + count + "条数据!";