use of com.google.common.util.concurrent.RateLimiter in project new-cloud by xie-summer.
the class ServiceRateLimitZuulFilter method run.
@Override
public Object run() {
try {
RequestContext context = RequestContext.getCurrentContext();
HttpServletResponse response = context.getResponse();
String key = null;
// 对于service格式的路由,走RibbonRoutingFilter
String serviceId = (String) context.get(SERVICE_ID_KEY);
if (serviceId != null) {
key = serviceId;
map.putIfAbsent(serviceId, RateLimiter.create(1000.0));
} else // 如果压根不走RibbonRoutingFilter,则认为是URL格式的路由
{
// 对于URL格式的路由,走SimpleHostRoutingFilter
URL routeHost = context.getRouteHost();
if (routeHost != null) {
String url = routeHost.toString();
key = url;
map.putIfAbsent(url, RateLimiter.create(2000.0));
}
}
RateLimiter rateLimiter = map.get(key);
if (!rateLimiter.tryAcquire()) {
HttpStatus httpStatus = HttpStatus.TOO_MANY_REQUESTS;
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
response.setStatus(httpStatus.value());
response.getWriter().append(httpStatus.getReasonPhrase());
context.setSendZuulResponse(false);
throw new RateLimiterException(httpStatus.getReasonPhrase(), httpStatus.value(), httpStatus.getReasonPhrase());
}
} catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
return null;
}
use of com.google.common.util.concurrent.RateLimiter in project yyl_example by Relucent.
the class RateLimiterTest method main.
public static void main(String[] args) {
// 根据指定的稳定吞吐率创建RateLimiter,这里的吞吐率是指每秒多少许可数
// 速率是每秒2个许可
final RateLimiter rateLimiter = RateLimiter.create(2.0);
for (int i = 0; i < 30; i++) {
// 从RateLimiter获取一个许可,该方法会被阻塞直到获取到请求
rateLimiter.acquire();
System.out.println((System.currentTimeMillis() / 1000) + "->" + (i));
}
}
use of com.google.common.util.concurrent.RateLimiter in project distributedlog by twitter.
the class RecordGenerator method main.
public static void main(String[] args) throws Exception {
if (3 != args.length) {
System.out.println(HELP);
return;
}
String finagleNameStr = args[0];
final String streamName = args[1];
double rate = Double.parseDouble(args[2]);
RateLimiter limiter = RateLimiter.create(rate);
DistributedLogClient client = DistributedLogClientBuilder.newBuilder().clientId(ClientId$.MODULE$.apply("record-generator")).name("record-generator").thriftmux(true).finagleNameStr(finagleNameStr).build();
final CountDownLatch keepAliveLatch = new CountDownLatch(1);
final AtomicLong numWrites = new AtomicLong(0);
final AtomicBoolean running = new AtomicBoolean(true);
while (running.get()) {
limiter.acquire();
String record = "record-" + System.currentTimeMillis();
client.write(streamName, ByteBuffer.wrap(record.getBytes(UTF_8))).addEventListener(new FutureEventListener<DLSN>() {
@Override
public void onFailure(Throwable cause) {
System.out.println("Encountered error on writing data");
cause.printStackTrace(System.err);
running.set(false);
keepAliveLatch.countDown();
}
@Override
public void onSuccess(DLSN value) {
long numSuccesses = numWrites.incrementAndGet();
if (numSuccesses % 100 == 0) {
System.out.println("Write " + numSuccesses + " records.");
}
}
});
}
keepAliveLatch.await();
client.close();
}
Aggregations