ibatis里面的流控实现是通过Throttle这个类来实现,既信号量,实例化时指定最大的访问量,每次请求时从这里获取一个信号量,如果已经达到阈值将阻塞或者抛出异常,以免大量的请求导致服务当机,每次请求时申请一个,请求结束返回,既信号量的实现,代码如下
申请资源
public void increment() {
synchronized (LOCK) {
long totalWaitTime = 0;
while (count >= limit) {
if (maxWait > 0) {
long waitTime = System.currentTimeMillis();
try {
LOCK.wait(maxWait - totalWaitTime);
} catch (InterruptedException e) {
//ignore
}
totalWaitTime += System.currentTimeMillis() - waitTime;
if (totalWaitTime > maxWait) {
throw new RuntimeException("Throttle waited too long (" + totalWaitTime + " milliseconds) for lock.");
}
} else {
try {
LOCK.wait();
} catch (InterruptedException e) {
//ignore
}
}
}
count++;
}
}
释放资源
/**
* Remove a reference
*/
public void decrement() {
synchronized (LOCK) {
count--;
LOCK.notify();
}
}
客户端调用方式
/**
* Pop an object from the pool
* @return - the Object
*/
public Object pop() {
throttle.increment();
return pool.remove(0);
}
/**
* Push an object onto the pool
* @param o - the object to put into the pool
*/
public void push(Object o) {
if (o != null && o.getClass() == type) {
pool.add(o);
throttle.decrement();
}
}