Guava Tutorial-ratelimiter

Source: Internet
Author: User
Tags semaphore google guava
Introduction

The Ratelimiter class is available in the Google Guava library, which is often used to limit the rate of access to some physical or logical resources. Compared to Semaphore, Semaphore limits the number of concurrent accesses rather than the usage rate.

The Ratelimiter class is defined as follows:

Com.google.common.util.concurrent.RateLimiter

@ThreadSafe
@Beta public
abstract class Ratelimiter
extends Object

Guava docs about Ratelimiter is introduced as follows:

A rate limiter. Conceptually, a rate limiter distributes permits at a configurable rate. Each acquire () blocks if necessary until a permit are available, and then takes it. Once acquired, permits need not being released.

Rate limiters was often used to restrict the rate at which some physical or logical resource is accessed. This was in contrast to Semaphore which restricts the number of concurrent accesses instead of the the rate (note though that C Oncurrency and rate is closely related, e.g. see Little's Law).

A ratelimiter is defined primarily by the-the-which permits is issued. Absent additional configuration, permits'll be the distributed at a fixed rate, defined in terms of permits per second. Permits'll be distributed smoothly, with the delay between individual permits being adjusted to ensure that the Configur Ed rate is maintained.

It is possible to configure a ratelimiter to has a warmup period during which time the permits issued each second Steadil Y increases until it hits the stable rate.

Define the Ratelimiter by setting the rate of the license. Under the default configuration, the license is assigned at a fixed rate, and the rate is the number of licenses per second. To ensure that the configuration is maintained at a rate, the license is distributed smoothly and the delay between licenses is adjusted.

There may be a case of configuring a ratelimiter with a warm-up period, during which time the number of licenses allocated per second will grow steadily until a steady rate is reached.

principle
Ratelimiter is using a flow control algorithm called a token bucket , Ratelimiter will throw tokens at a certain frequency to the bucket, the thread gets the token to execute, such as you want your application QPS not more than 1000, Then Ratelimiter sets the rate of 1000 and throws 1000 tokens into the bucket every second. usage

For example, how to use ratelimiter, imagine that we need to work on a task list, but we don't want to submit more than two tasks per second:

Final Ratelimiter Ratelimiter = ratelimiter.create (2.0); Rate is ' 2 permits per second '
  void Submittasks (list<runnable> tasks, Executor Executor) {for
    (Runnable Task:tasks) {
      ratelimiter.acquire ();//May wait
      executor.execute (Task);
    }
  }

As another example, imagine that we have created a data stream and want to process it at a rate of 5kb per second. You can do this by requiring each byte to represent a license, and then specifying 5,000 licenses per second:

  Final Ratelimiter ratelimiter = ratelimiter.create (5000.0); Rate = permits per second
  void Submitpacket (byte[] packet) {
    ratelimiter.acquire (packet.length);
    Networkservice.send (packet);
  }

Finally, in Ratelimiter's doc, there was this sentence.

It is important to note that the number of permits requested never affect the throttling of the request itself (an Invocat Ion to acquire (1) and a invocation to acquire (+) would result in exactly the same throttling, if any), but it affects t He throttling of the next request. i.e., if an expensive task arrives at an idle ratelimiter, it'll be granted immediately, but it's the next request that Would experience extra throttling, thus paying for the cost of the expensive task.

Note:ratelimiter does not provide fairness guarantees.

Translate as follows:
It is important to note that the requested number of licenses never affects the limits of the request itself (calling acquire (1) and calling acquire (1000) will get the same throttling effect if such a call exists) but will affect the next request limit, that is, If a high-overhead task arrives at an idle ratelimiter, it will be licensed immediately, but the next request will experience additional restrictions to pay for high-overhead tasks. Note: Ratelimiter does not provide a guarantee of fairness. Application

We can easily use Guava's ratelimiter to achieve flow control.
For example, a company needs to be on-line with a business and the partner provides API interfaces to get business data, although the server load pressure partner restricts API interface calls: The requirement for QPS to be up to 60. The API call code is implemented as follows:

Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.TimeUnit;

Import Com.google.common.util.concurrent.RateLimiter;

    public class Apicalldemo {private int permitspersecond = 55;//55 licenses per second private int threadnum = 10;
    public static void Main (string[] args) {new Apicalldemo (). Call ();
        private void Call () {Executorservice executor = Executors.newfixedthreadpool (threadnum); 
        Final Ratelimiter ratelimiter = ratelimiter.create (Permitspersecond);
        for (int i=0; i<threadnum; i++) {Executor.execute (new Apicalltask (Ratelimiter));
    } executor.shutdown ();
    }} class Apicalltask implements runnable{private Ratelimiter ratelimiter;
    Private Boolean runing = true;
    Public Apicalltask (Ratelimiter ratelimiter) {this.ratelimiter = Ratelimiter; } @Override public void Run () {while (runing) {RatelimIter.acquire ();
        May wait getData (); }}/** impersonate the calling Partner API */private void GetData () {System.out.println (Thread.CurrentThread (). GetName () + "
        Runing! ");
        try {TimeUnit.MILLISECONDS.sleep (100);
        } catch (Interruptedexception e) {e.printstacktrace ();
 }
    }
}

Reference:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/ ratelimiter.html
Guava Official document-ratelimiter class: http://ifeve.com/guava-ratelimiter/
Traffic control and token bucket algorithm: https:// blog.jamespan.me/2015/10/19/traffic-shaping-with-token-bucket/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.