Tuesday 5 July 2022

Bucket4j: refill the tokens in interval manner

Refill#intervally method refill the tokens in intervally manner. This refill strategy will wait until whole period will be elapsed before regenerate tokens.

 

Signature

public static Refill intervally(long tokens, Duration period)

'tokens' specifies the amount of tokens and 'period' specifies the period within tokens will be fully regenerated.

 

Example

Bandwidth limit = Bandwidth.classic(5, Refill.intervally(5, Duration.ofSeconds(10)));

Unlike ‘Refill.greedy’ method, Refill.intervally method refills the bucket at the beginning of the time window, in this example, 5 tokens at the start of the 10 seconds.

 

Find the below working application.

 

RefillTokensIntervally.java

package com.sample.app;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Refill;

public class RefillTokensIntervally {

	private static Bucket bucket;

	private static void getResource() {

		final long noOfTokensLeft = bucket.getAvailableTokens();

		if (noOfTokensLeft == 0L) {
			System.err.println("Number of tokens left : " + bucket.getAvailableTokens());
		} else {
			System.out.println("Number of tokens left : " + bucket.getAvailableTokens());
		}

		if (bucket.tryConsume(1)) {
			System.out.println("Resource accessed....\n");
			return;
		}

		System.err.println("Too Many Requests, all the tokens consumed\n");
	}

	public static void main(String[] args) throws InterruptedException {
		Bandwidth limit = Bandwidth.classic(5, Refill.intervally(5, Duration.ofSeconds(10)));
		bucket = Bucket.builder().addLimit(limit).build();

		while (true) {
			TimeUnit.SECONDS.sleep(1);
			getResource();
		}
	}

}

Output

Number of tokens left : 5
Resource accessed....

Number of tokens left : 4
Resource accessed....

Number of tokens left : 3
Resource accessed....

Number of tokens left : 2
Resource accessed....

Number of tokens left : 1
Resource accessed....

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 5
Resource accessed....

Number of tokens left : 4
Resource accessed....

Number of tokens left : 3
Resource accessed....

Number of tokens left : 2
Resource accessed....

Number of tokens left : 1
Resource accessed....

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 5
Resource accessed....

Number of tokens left : 4
Resource accessed....

Number of tokens left : 3
Resource accessed....

Number of tokens left : 2
Resource accessed....

Number of tokens left : 1
Resource accessed....

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 0
Too Many Requests, all the tokens consumed

Number of tokens left : 5
Resource accessed....

Number of tokens left : 4
Resource accessed....

Number of tokens left : 3
Resource accessed....

Number of tokens left : 2
Resource accessed....

Number of tokens left : 1
Resource accessed....

Number of tokens left : 0
Too Many Requests, all the tokens consumed




 

Previous                                                 Next                                                 Home

No comments:

Post a Comment