/** * 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); }
/** * 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); }
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;
RateLimit rateLimit = RateLimit.timeInterval(1, TimeUnit.SECONDS);
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; }
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()); } }
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; }
/** * 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); }