本文整理匯總了Java中com.google.common.util.concurrent.RateLimiter.acquire方法的典型用法代碼示例。如果您正苦於以下問題:Java RateLimiter.acquire方法的具體用法?Java RateLimiter.acquire怎麽用?Java RateLimiter.acquire使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.util.concurrent.RateLimiter
的用法示例。
在下文中一共展示了RateLimiter.acquire方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getUserByFeignBatch
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
@Override
public BaseResponse<UserResVO> getUserByFeignBatch(@RequestBody UserReqVO userReqVO) {
//調用遠程服務
OrderNoReqVO vo = new OrderNoReqVO() ;
vo.setReqNo(userReqVO.getReqNo());
RateLimiter limiter = RateLimiter.create(2.0) ;
//批量調用
for (int i = 0 ;i< COUNT ; i++){
double acquire = limiter.acquire();
logger.debug("獲取令牌成功!,消耗=" + acquire);
BaseResponse<OrderNoResVO> orderNo = orderServiceClient.getOrderNo(vo);
logger.debug("遠程返回:"+JSON.toJSONString(orderNo));
}
UserRes userRes = new UserRes() ;
userRes.setUserId(123);
userRes.setUserName("張三");
userRes.setReqNo(userReqVO.getReqNo());
userRes.setCode(StatusEnum.SUCCESS.getCode());
userRes.setMessage("成功");
return userRes ;
}
示例2: scanPages
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
/**
* executes a page scan with scanExpression, limited by limiter for objects of the type T with the class clazz
* and calls action for each object found
*
* @param clazz class of the objects
* @param scanExpression expression for the scan
* @param limiter the read limiter limiting the amount of requests
* @param action the function to call for each object
* @param <T> the type of the objects
*/
private static <T> void scanPages(Class<T> clazz, DynamoDBScanExpression scanExpression, RateLimiter limiter, Consumer<? super T> action) {
// define pageScan and add consumed capacity to scan expression
ScanResultPage<T> pageScan;
scanExpression.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
// initialize counter, permits and mapper
int permitsToConsume = 1;
DynamoDBMapper dynamoDBMapper = DBConnector.getInstance().getDynamoDBMapper();
int scanned = 0;
int count = 0;
do {
// acquire permits and scan
limiter.acquire(permitsToConsume);
pageScan = dynamoDBMapper.scanPage(clazz, scanExpression);
// update page scan
scanExpression.setExclusiveStartKey(pageScan.getLastEvaluatedKey());
// update stats variables
scanned += pageScan.getScannedCount();
count += pageScan.getCount();
// call the action on each result
pageScan.getResults().forEach(action);
// calculate permits for next scan
Double capacityUnits = pageScan.getConsumedCapacity().getCapacityUnits();
permitsToConsume = (int) (capacityUnits - 1);
if (permitsToConsume <= 0) permitsToConsume = 1;
log.info(String.format("Scanned a page for class %s. Results: %d/%d (%d/%d total). Capacity units consumed: %f",
clazz.getSimpleName(), pageScan.getCount(), pageScan.getScannedCount(), count, scanned, capacityUnits));
} while (pageScan.getLastEvaluatedKey() != null);
}
示例3: waitTaskConsumption
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
protected void waitTaskConsumption(boolean returnIfStopped, boolean waitForRunning) {
RateLimiter rateLimiter = RateLimiter.create(1);
int tryCount = 0;
while (true) {
tryCount++;
rateLimiter.acquire();
if (
// if returnIfStopped == true, we consider that wait is done
(returnIfStopped || manager.isActive())
&& manager.getNumberOfWaitingTasks() == 0
// if we don't wait for running, ignore manager.getNumberOfRunningTasks()
&& (!waitForRunning || manager.getNumberOfRunningTasks() == 0)) {
break;
}
if (tryCount > 10) {
throw new IllegalStateException(MessageFormat.format("Task queue not empty after {0} tries.", tryCount));
}
}
}
示例4: buildResourceList
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
protected List<Resource> buildResourceList(RestApi api) {
List<Resource> resourceList = new ArrayList<>();
Resources resources = api.getResources();
resourceList.addAll(resources.getItem());
LOG.debug("Building list of resources. Stack trace: ", new Throwable());
final RateLimiter rl = RateLimiter.create(2);
while (resources._isLinkAvailable("next")) {
rl.acquire();
resources = resources.getNext();
resourceList.addAll(resources.getItem());
}
return resourceList;
}
示例5: replay
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
public int replay(RateLimiter rateLimiter, Set<InetAddress> hintedNodes) throws IOException
{
logger.trace("Replaying batch {}", id);
if (mutations.isEmpty())
return 0;
int gcgs = gcgs(mutations);
if (TimeUnit.MILLISECONDS.toSeconds(writtenAt) + gcgs <= FBUtilities.nowInSeconds())
return 0;
replayHandlers = sendReplays(mutations, writtenAt, hintedNodes);
rateLimiter.acquire(replayedBytes); // acquire afterwards, to not mess up ttl calculation.
return replayHandlers.size();
}
示例6: run
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
public void run()
{
RateLimiter rl = rateLimit != 0 ? RateLimiter.create(rateLimit) : null;
final Random rand = random != null ? random : ThreadLocalRandom.current();
while (!stop)
{
if (rl != null)
rl.acquire();
ByteBuffer key = randomBytes(16, rand);
UpdateBuilder builder = UpdateBuilder.create(Schema.instance.getCFMetaData("Keyspace1", "Standard1"), Util.dk(key));
for (int ii = 0; ii < numCells; ii++)
{
int sz = randomSize ? rand.nextInt(cellSize) : cellSize;
ByteBuffer bytes = randomBytes(sz, rand);
builder.newRow("name" + ii).add("val", bytes);
hash = hash(hash, bytes);
++cells;
dataSize += sz;
}
rp = commitLog.add(new Mutation(builder.build()));
counter.incrementAndGet();
}
}
示例7: testRateLimiter
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
/**
* RateLimiter類似於JDK的信號量Semphore,他用來限製對資源並發訪問的線程數
*/
public static void testRateLimiter() {
ListeningExecutorService executorService = MoreExecutors
.listeningDecorator(Executors.newCachedThreadPool());
RateLimiter limiter = RateLimiter.create(5.0); // 每秒不超過4個任務被提交
for (int i = 0; i < 10; i++) {
limiter.acquire(); // 請求RateLimiter, 超過permits會被阻塞
final ListenableFuture<Integer> listenableFuture = executorService
.submit(new Task("is " + i));
}
}
示例8: startTest
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
private void startTest() {
stopped.set(false);
RateLimiter limiter = RateLimiter.create(rate);
Semaphore s = new Semaphore(100);
while (!stopped.get()) {
limiter.acquire();
s.acquireUninterruptibly();
counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> {
s.release();
if (e == null) {
increments.incrementAndGet();
}
});
}
}
示例9: run
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
@Override
public void run() {
MqttConnection connection = MqttConnection.getInstance();
running = true;
started = System.currentTimeMillis();
// TODO simplification with only one metrics series supported
int ratePerMinute = config.getRate();
RateLimiter limiter = com.google.common.util.concurrent.RateLimiter.create(ratePerMinute / (double) 60.0);
while (running && connection.isConnected()) {
long start = System.currentTimeMillis();
numberTicks++;
// Wait for the configured amount of time to maintain the rate
limiter.acquire();
// int numberOfPayloads = calculatePayloadsPerTick(ratePerMinute, timeForLastTick, MIN_TICK_TIME_MS);
byte[] payload = formatPayload(config.getTemplateId(), functions);
try {
connection.publish(config.getTopic(), 0, payload, config.getId());
} catch (MqttException ex) {
LOG.log(Level.SEVERE, "Could not publish the MQTT-message", ex);
}
long timeForLastTick = System.currentTimeMillis() - start;
LOG.log(Level.INFO, "Took {0}ms to execute tick #: {1}", new Object[]{timeForLastTick, numberTicks});
}
LOG.info("Stopped metrics thread");
}
示例10: testBatchMessagesRateOut
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
public void testBatchMessagesRateOut() throws PulsarClientException, InterruptedException, PulsarAdminException {
log.info("-- Starting {} test --", methodName);
String topicName = "persistent://my-property/cluster/my-ns/testBatchMessagesRateOut";
double produceRate = 17;
int batchSize = 5;
ConsumerConfiguration consumerConf = new ConsumerConfiguration();
consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, "my-subscriber-name", consumerConf);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setBatchingMaxMessages(batchSize);
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(2, TimeUnit.SECONDS);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
AtomicBoolean runTest = new AtomicBoolean(true);
Thread t1 = new Thread(() -> {
RateLimiter r = RateLimiter.create(produceRate);
while (runTest.get()) {
r.acquire();
producer.sendAsync("Hello World".getBytes());
consumer.receiveAsync().thenAccept(message -> consumer.acknowledgeAsync(message));
}
});
t1.start();
Thread.sleep(2000); // Two seconds sleep
runTest.set(false);
pulsar.getBrokerService().updateRates();
double actualRate = admin.persistentTopics().getStats(topicName).msgRateOut;
assertTrue(actualRate > (produceRate / batchSize));
consumer.unsubscribe();
log.info("-- Exiting {} test --", methodName);
}
示例11: rateLimited
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
private Runnable rateLimited(Placement placement, final Runnable delegate) {
final RateLimiter rateLimiter = _rateLimiterCache.get(placement.getKeyspace().getClusterName());
return new Runnable() {
@Override
public void run() {
rateLimiter.acquire();
delegate.run();
}
};
}
示例12: runCmd
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
@Override
protected int runCmd(DistributedLogClient client) throws Exception {
RateLimiter rateLimiter = RateLimiter.create(rate);
for (String stream : streams) {
rateLimiter.acquire();
try {
Await.result(client.release(stream));
System.out.println("Release ownership of stream " + stream);
} catch (Exception e) {
System.err.println("Failed to release ownership of stream " + stream);
throw e;
}
}
return 0;
}
示例13: ready
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
boolean ready(WorkManager permits, RateLimiter rateLimiter)
{
int partitionCount = (int) spec.partitionCount.next();
if (partitionCount <= 0)
return false;
partitionCount = permits.takePermits(partitionCount);
if (partitionCount <= 0)
return false;
int i = 0;
boolean success = true;
for (; i < partitionCount && success ; i++)
{
if (i >= partitionCache.size())
partitionCache.add(PartitionIterator.get(spec.partitionGenerator, spec.seedManager));
success = false;
while (!success)
{
Seed seed = spec.seedManager.next(this);
if (seed == null)
break;
if (spec.useRatio == null)
success = partitionCache.get(i).reset(seed, spec.targetCount, isWrite());
else
success = partitionCache.get(i).reset(seed, spec.useRatio.next(), isWrite());
}
}
partitionCount = i;
if (rateLimiter != null)
rateLimiter.acquire(partitionCount);
partitions = partitionCache.subList(0, partitionCount);
return !partitions.isEmpty();
}
示例14: timedThrottle
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
private void timedThrottle(final String apiName, final RateLimiter limiter, final String tableName, final int permits) {
if (limiter == null) {
throw new IllegalArgumentException("limiter for " + apiName + " on table " + tableName + " was null");
}
final Timer.Context throttleTimerCtxt = getTimerContext(String.format("%sThrottling", apiName), tableName);
try {
limiter.acquire(permits);
} finally {
throttleTimerCtxt.stop();
}
}
示例15: sendJSON
import com.google.common.util.concurrent.RateLimiter; //導入方法依賴的package包/類
public static void sendJSON(WebSocketChannel socket, Object object, ObjectMapper encoder, @Nullable RateLimiter r, @Nullable AtomicLong outBytes) {
byte[] s;
if (object instanceof byte[]) {
s = (byte[])object;
} else {
try {
s = encoder.writeValueAsBytes(object);
} catch (JsonProcessingException t) {
try {
s = encoder.writeValueAsBytes(object.toString()); //could not make json so just use toString()
} catch (JsonProcessingException e) {
e.printStackTrace();
s = object.toString().getBytes();
}
}
}
int size = s.length;
if (r!=null) {
r.acquire(size);
}
WebSockets.sendBinary(ByteBuffer.wrap(s), socket, null);
//WebSockets.sendBinaryBlocking(ByteBuffer.wrap(s), socket);
if (outBytes!=null)
outBytes.addAndGet(size);
}