本文整理匯總了Java中com.google.common.util.concurrent.RateLimiter類的典型用法代碼示例。如果您正苦於以下問題:Java RateLimiter類的具體用法?Java RateLimiter怎麽用?Java RateLimiter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
RateLimiter類屬於com.google.common.util.concurrent包,在下文中一共展示了RateLimiter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initRateLimiter
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
public RateLimiter initRateLimiter(String target, double rate) {
RateLimiter limiter = requestRateLimiterMap.get(target);
if (limiter == null) {
limiter = RateLimiter.create(rate);
requestRateLimiterMap.put(target, limiter);
return limiter;
}
if (limiter.getRate() == rate) {
return limiter;
}
limiter.setRate(rate);
requestRateLimiterMap.put(target, limiter);
return limiter;
}
示例2: RedisThrottler
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
RedisThrottler(
RedisThrottlerConfig config,
RedisAppender appender,
boolean ignoreExceptions,
boolean debugEnabled) {
this.config = config;
this.appender = appender;
this.ignoreExceptions = ignoreExceptions;
this.buffer = new ArrayBlockingQueue<>(config.getBufferSize());
this.batch = new byte[config.getBatchSize()][];
this.flushTrigger = createFlushTrigger();
this.eventRateLimiter = config.getMaxEventCountPerSecond() > 0 ? RateLimiter.create(config.getMaxEventCountPerSecond()) : null;
this.byteRateLimiter = config.getMaxByteCountPerSecond() > 0 ? RateLimiter.create(config.getMaxByteCountPerSecond()) : null;
this.logger = new DebugLogger(RedisThrottler.class, debugEnabled);
this.jmxBeanName = createJmxBeanName();
}
示例3: create
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
public static IFace create(double maxRequestsPerSecond) {
if (maxRequestsPerSecond <= 0.0) {
return ALLOW_NONE;
}
final RateLimiter rateLimiter = RateLimiter.create(maxRequestsPerSecond);
return new IFace() {
@Override
public boolean allowDequeue(int numJobs) {
return rateLimiter.tryAcquire(numJobs);
}
@Override
public double getRate() {
return rateLimiter.getRate();
}
};
}
示例4: StreamServer
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
public StreamServer(Socket aClientSocket, RateLimiter rateLimiter, long startTime, int duration,
BufferedReader dataSource, AtomicInteger consumedTuples, int maxTupels) {
try {
_duration = duration;
_sourceBuffer = dataSource;
_rateLimiter = rateLimiter;
_clientSocket = aClientSocket;
_startTime = startTime;
_cosumedTuples = consumedTuples;
_maxTuples = maxTupels;
_output = new BufferedOutputStream(_clientSocket.getOutputStream());
this.start();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
示例5: getRateLimiter
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
/**
* 獲取限速器
*
* @param restyCommand
* @return limiter
*/
private RateLimiter getRateLimiter(RestyCommand restyCommand) {
String key = restyCommand.getServiceMethod() + "@" + restyCommand.getPath();
RateLimiter rateLimiter = limiterMap.get(key);
if (rateLimiter == null) {
limiterMap.putIfAbsent(key, RateLimiter.create(restyCommand.getRestyCommandConfig().getLimit()));
rateLimiter = limiterMap.get(key);
} else if (rateLimiter.getRate() != restyCommand.getRestyCommandConfig().getLimit()) {
// 更新rate
rateLimiter.setRate(restyCommand.getRestyCommandConfig().getLimit());
}
return rateLimiter;
}
示例6: main
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
public static void main(String[] args) {
try {
RateLimiter limit = RateLimiter.create(100d);
PropertyConfigurator.configure("conf/log4j.properties");
while (true) {
if (limit.tryAcquire()) {
final HelloCommand command = new HelloCommand();
// System.out.println("result: " + command.execute());
// System.out.println("");
Future<String> future = command.queue();
System.out.println("result: " + future.get());
System.out.println("");
}
}
// Observable<String> observe = command.observe();
// observe.asObservable().subscribe((result) -> {
// System.out.println(result);
// });
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: setParent
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
/**
* @param parent the parent to set.
*/
@Override
public void setParent(Channel parent)
{
super.setParent(parent);
UserQuotas quotas = this.getUserQuotas();
if (quotas != null)
{
if (quotas.getMaxBandwidth() != null)
{
this.rateLimiter =
RateLimiter.create(quotas.getMaxBandwidth().intValue());
}
if (quotas.getMaxSize() != null)
{
this.maxAllowedPermits = quotas.getMaxSize().longValue();
}
}
}
示例8: freshNetRate
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
public void freshNetRate(int rate) {
int downRate = rate - Constants.SYSTEM_NEED_RATE;
if (downRate <= 0) {
downRate = rate / 2;
}
if (downRate == 0) {
logger.error("net rate:{} is illegal", rate);
return;
}
long rateOnByte = downRate * 1024L * 1024L;
boolean updated = false;
try {
for (RateLimiter rateLimiter : rateLimiters) {
if (Math.abs(rateLimiter.getRate() - rateOnByte) >= 1024) {
rateLimiter.setRate(rateOnByte);
updated = true;
}
}
if (updated) {
logger.info("update net rate to {} MB", rate);
}
} catch (Exception e) {
logger.error("E_freshNetRate", e);
}
}
示例9: main
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
CliLoader cliLoader = new CliLoader();
Configuration configuration = cliLoader.parseArgs(args);
LOGGER.info("starting login client");
RateLimiter rateLimiter;
if( configuration.getRequestsPerMinute() == -1) {
rateLimiter = null;
} else {
rateLimiter = RateLimiter.create(
(double)configuration.getRequestsPerMinute()/60);
}
for (long i = 0; i < configuration.getLoops(); i++) {
LOGGER.info("executing login loop {}", i);
configuration = cliLoader.parseArgs(args);
executeLogins(configuration, rateLimiter);
}
}
示例10: RateLimitedExecutor
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
@Inject
public RateLimitedExecutor(Config config) {
// Check arguments.
checkNotNull(config, "config");
// Set class fields.
this.executorService = createExecutorService(config.getThreadCount());
this.rateLimiter = RateLimiter.create(
config.getMaxRequestCountPerSecond(),
config.getRampUpDurationSeconds(),
TimeUnit.SECONDS);
LOGGER.debug(
"instantiated (threadCount={}, maxRequestCountPerSecond={}, rampUpDurationSeconds={})",
config.getThreadCount(), config.getMaxRequestCountPerSecond(), config.getRampUpDurationSeconds());
}
示例11: RemoteConfigRepository
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
/**
* Constructor.
*
* @param namespace the namespace
*/
public RemoteConfigRepository(String namespace) {
m_namespace = namespace;
m_configCache = new AtomicReference<>();
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
m_httpUtil = ApolloInjector.getInstance(HttpUtil.class);
m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class);
remoteConfigLongPollService = ApolloInjector.getInstance(RemoteConfigLongPollService.class);
m_longPollServiceDto = new AtomicReference<>();
m_remoteMessages = new AtomicReference<>();
m_loadConfigRateLimiter = RateLimiter.create(m_configUtil.getLoadConfigQPS());
m_configNeedForceRefresh = new AtomicBoolean(true);
m_loadConfigFailSchedulePolicy = new ExponentialSchedulePolicy(m_configUtil.getOnErrorRetryInterval(),
m_configUtil.getOnErrorRetryInterval() * 8);
gson = new Gson();
this.trySync();
this.schedulePeriodicRefresh();
this.scheduleLongPollingRefresh();
}
示例12: RemoteConfigLongPollService
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
/**
* Constructor.
*/
public RemoteConfigLongPollService() {
m_longPollFailSchedulePolicyInSecond = new ExponentialSchedulePolicy(1, 120); //in second
m_longPollingStopped = new AtomicBoolean(false);
m_longPollingService = Executors.newSingleThreadExecutor(
ApolloThreadFactory.create("RemoteConfigLongPollService", true));
m_longPollStarted = new AtomicBoolean(false);
m_longPollNamespaces =
Multimaps.synchronizedSetMultimap(HashMultimap.<String, RemoteConfigRepository>create());
m_notifications = Maps.newConcurrentMap();
m_remoteNotificationMessages = Maps.newConcurrentMap();
m_responseType = new TypeToken<List<ApolloConfigNotification>>() {
}.getType();
gson = new Gson();
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
m_httpUtil = ApolloInjector.getInstance(HttpUtil.class);
m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class);
m_longPollRateLimiter = RateLimiter.create(m_configUtil.getLongPollQPS());
}
示例13: 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 ;
}
示例14: 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);
}
示例15: updateRuntimeConfig
import com.google.common.util.concurrent.RateLimiter; //導入依賴的package包/類
private static void updateRuntimeConfig(Supplier<StyxConfig> config, RateLimiter rateLimiter) {
try {
double currentRate = rateLimiter.getRate();
Double updatedRate = config.get().submissionRateLimit().orElse(
StyxScheduler.DEFAULT_SUBMISSION_RATE_PER_SEC);
if (Math.abs(updatedRate - currentRate) >= 0.1) {
LOG.info("Updating submission rate limit: {} -> {}", currentRate, updatedRate);
rateLimiter.setRate(updatedRate);
}
} catch (Exception e) {
LOG.warn("Failed to fetch the submission rate config from storage, "
+ "skipping RateLimiter update");
}
}