Search in sources :

Example 46 with RateLimiter

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;
}
Also used : HttpStatus(org.springframework.http.HttpStatus) HttpServletResponse(javax.servlet.http.HttpServletResponse) RateLimiterException(com.cloud.common.exception.RateLimiterException) RequestContext(com.netflix.zuul.context.RequestContext) URL(java.net.URL) RateLimiter(com.google.common.util.concurrent.RateLimiter) ZuulException(com.netflix.zuul.exception.ZuulException) RateLimiterException(com.cloud.common.exception.RateLimiterException)

Example 47 with RateLimiter

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));
    }
}
Also used : RateLimiter(com.google.common.util.concurrent.RateLimiter)

Example 48 with RateLimiter

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) DLSN(com.twitter.distributedlog.DLSN) CountDownLatch(java.util.concurrent.CountDownLatch) RateLimiter(com.google.common.util.concurrent.RateLimiter) DistributedLogClient(com.twitter.distributedlog.service.DistributedLogClient)

Aggregations

RateLimiter (com.google.common.util.concurrent.RateLimiter)48 ParameterException (com.beust.jcommander.ParameterException)12 Test (org.junit.Test)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ExecutorService (java.util.concurrent.ExecutorService)7 JCommander (com.beust.jcommander.JCommander)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)6 IOException (java.io.IOException)6 Histogram (org.HdrHistogram.Histogram)6 FileInputStream (java.io.FileInputStream)5 Properties (java.util.Properties)5 PulsarClient (org.apache.pulsar.client.api.PulsarClient)5 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Message (com.yahoo.pulsar.client.api.Message)3 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)3 FileOutputStream (java.io.FileOutputStream)3