You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

48 lines
1.3 KiB

package com.fr.plugin.pack.traffic;
import com.fr.log.FineLoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
/**
* @author Jonas
* @version 5.1.3
* Created by Jonas on 2020-10-19
*/
public class PackTrafficImpl implements PackTraffic {
private Map<String, Integer> mapCount = new ConcurrentHashMap<>();
private Supplier<Integer> maxCount;
private String name;
public PackTrafficImpl(String name, Supplier<Integer> maxCount) {
this.name = name;
this.maxCount = maxCount;
}
@Override
public synchronized boolean offer(String id) {
Integer count = mapCount.get(id);
if (count == null) {
count = 0;
}
int addCount = count + 1;
if (addCount > maxCount.get()) {
FineLoggerFactory.getLogger().info("PackTraffic({}) false {} addCount {}", name, id, addCount);
return false;
}
mapCount.put(id, addCount);
FineLoggerFactory.getLogger().info("PackTraffic({}) true {} addCount {}", name, id, addCount);
return true;
}
@Override
public synchronized void release(String id) {
Integer count = mapCount.get(id);
if (count <= 1) {
mapCount.remove(id);
}
mapCount.put(id, count - 1);
}
}