Tabnine Logo
RateLimit
Code IndexAdd Tabnine to your IDE (free)

How to use
RateLimit
in
sirius.kernel.commons

Best Java code snippets using sirius.kernel.commons.RateLimit (Showing top 8 results out of 315)

origin: com.scireum/sirius-kernel

/**
 * Creates a new call based rate limit.
 * <p>
 * Calling {@link #check()} on will only return <tt>true</tt> every n-th call and <tt>false</tt> otherwise.
 *
 * @param n the number of calls to skip (returning <tt>false</tt>) by {@link #check()} before <tt>true</tt>
 *          is returned
 * @return a new call based rate limit
 */
public static RateLimit everyNthCall(long n) {
  return new RateLimit(n, 0, Mode.CALL_BASED);
}
origin: com.scireum/sirius-kernel

/**
 * Creates a new time based rate limit.
 * <p>
 * Calling {@link #check()} on will only return <tt>true</tt> every after the given amount of time has be passed
 * since the last time it returned <tt>true</tt>. Returns <tt>false</tt> otherwise.
 *
 * @param interval the amount of time after a call to {@link #check()} returns <tt>true</tt> again
 * @param unit     the unit for amount
 * @return a new time based rate limit
 */
public static RateLimit timeInterval(long interval, TimeUnit unit) {
  return nTimesPerInterval(interval, unit, 1);
}
origin: com.scireum/sirius-db

try {
  TaskContext ctx = TaskContext.get();
  RateLimit rateLimit = RateLimit.timeInterval(1, TimeUnit.SECONDS);
  Limit effectiveLimit = new Limit(skip, limit);
  scrollResponse = executeScroll(entity -> {
    if (rateLimit.check() && !ctx.isActive()) {
      return false;
origin: com.scireum/sirius-search

RateLimit rateLimit = RateLimit.timeInterval(1, TimeUnit.SECONDS);
origin: com.scireum/sirius-search

private boolean processEntity(Function<? super E, Boolean> consumer,
               Limit lim,
               TaskContext ctx,
               RateLimit rateLimit,
               Set<String> entityDeDuplicator,
               E entity) {
  if (!entityDeDuplicator.contains(entity.getId())) {
    if (lim.nextRow()) {
      if (!consumer.apply(entity)) {
        return false;
      }
      if (!lim.shouldContinue()) {
        return false;
      }
    }
    if (rateLimit.check()) {
      // Check is the user tries to cancel this task
      if (!ctx.isActive()) {
        return false;
      }
    }
  }
  return true;
}
origin: com.scireum/sirius-search

private void executeScroll(SearchResponse initialSearchResponse,
              ResultHandler<? super E> handler,
              EntityDescriptor entityDescriptor) {
  SearchResponse searchResponse = initialSearchResponse;
  TaskContext ctx = TaskContext.get();
  RateLimit rateLimit = RateLimit.timeInterval(1, TimeUnit.SECONDS);
  long lastScroll = 0;
  Limit lim = new Limit(start, limit);
  while (true) {
    lastScroll = performScrollMonitoring(lastScroll);
    for (SearchHit hit : searchResponse.getHits()) {
      if (!processHit(handler, entityDescriptor, ctx, rateLimit, lim, hit)) {
        return;
      }
    }
    if (searchResponse.getHits().getHits().length == 0) {
      return;
    }
    searchResponse = scrollFurther(entityDescriptor, searchResponse.getScrollId());
  }
}
origin: com.scireum/sirius-search

private boolean processHit(ResultHandler<? super E> handler,
              EntityDescriptor entityDescriptor,
              TaskContext ctx,
              RateLimit rateLimit,
              Limit lim,
              SearchHit hit) {
  try {
    E entity = clazz.newInstance();
    entity.setId(hit.getId());
    entity.initSourceTracing();
    entity.setVersion(hit.getVersion());
    entity.setMatchedNamedQueries(hit.getMatchedQueries());
    entityDescriptor.readSource(entity, hit.getSourceAsMap());
    if (lim.nextRow()) {
      if (!handler.handleRow(entity)) {
        return false;
      }
      if (!lim.shouldContinue()) {
        return false;
      }
    }
    if (rateLimit.check() && !ctx.isActive()) {
      return false;
    }
  } catch (Exception e) {
    Exceptions.handle().to(IndexAccess.LOG).error(e).handle();
  }
  return true;
}
origin: com.scireum/sirius-kernel

/**
 * Creates a new time based rate limit which permits up to N calls per interval.
 * <p>
 * Calling {@link #check()} on will only return <tt>true</tt> N times in every given interval,
 * <tt>false</tt> otherwise.
 *
 * @param interval           the amount of time after a call to {@link #check()} returns <tt>true</tt> again
 * @param unit               the unit for amount
 * @param permitsPerInterval the number of times to return <tt>true</tt> per interval
 * @return a new time based rate limit
 */
public static RateLimit nTimesPerInterval(long interval, TimeUnit unit, int permitsPerInterval) {
  return new RateLimit(TimeUnit.MILLISECONDS.convert(interval, unit), permitsPerInterval, Mode.TIME_BASED);
}
sirius.kernel.commonsRateLimit

Javadoc

Limits calls to specified rate. Can be either time based, or invocation based.

This method #check() can be called frequently. It will however only return true after a certain amount of time has elapsed or after a number of calls where made. The mode depends on how the object was created.

As an example, this can be used in an inner loop if one wants to print out the current status every once in a while. Since writing to System.out is comparatively slow, it's a good idea to apply a rate limit:

 
RateLimit limit = RateLimit.everyNthCall(1000);} 
} 

Most used methods

  • check
    Checks whether the rate limit constraints permit another call or not.
  • timeInterval
    Creates a new time based rate limit. Calling #check() on will only return true every after the given
  • <init>
  • nTimesPerInterval
    Creates a new time based rate limit which permits up to N calls per interval. Calling #check() on w

Popular in Java

  • Making http requests using okhttp
  • scheduleAtFixedRate (Timer)
  • getResourceAsStream (ClassLoader)
  • getContentResolver (Context)
  • GridBagLayout (java.awt)
    The GridBagLayout class is a flexible layout manager that aligns components vertically and horizonta
  • HttpURLConnection (java.net)
    An URLConnection for HTTP (RFC 2616 [http://tools.ietf.org/html/rfc2616]) used to send and receive d
  • URI (java.net)
    A Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC
  • Date (java.util)
    A specific moment in time, with millisecond precision. Values typically come from System#currentTime
  • HttpServlet (javax.servlet.http)
    Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A sub
  • JComboBox (javax.swing)
  • Top PhpStorm plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now