Search in sources :

Example 1 with RequestLimitException

use of com.cloud.legacymodel.exceptions.RequestLimitException in project cosmic by MissionCriticalCloud.

the class ApiRateLimitServiceImpl method checkAccess.

@Override
public boolean checkAccess(final User user, final String apiCommandName) throws PermissionDeniedException {
    // check if api rate limiting is enabled or not
    if (!enabled) {
        return true;
    }
    final Long accountId = user.getAccountId();
    final Account account = _accountService.getAccount(accountId);
    if (_accountService.isRootAdmin(account.getId())) {
        // no API throttling on root admin
        return true;
    }
    StoreEntry entry = _store.get(accountId);
    if (entry == null) {
        /* Populate the entry, thus unlocking any underlying mutex */
        entry = _store.create(accountId, timeToLive);
    }
    /* Increment the client count and see whether we have hit the maximum allowed clients yet. */
    final int current = entry.incrementAndGet();
    if (current <= maxAllowed) {
        s_logger.trace("account (" + account.getAccountId() + "," + account.getAccountName() + ") has current count = " + current);
        return true;
    } else {
        final long expireAfter = entry.getExpireDuration();
        // for this exception, we can just show the same message to user and admin users.
        final String msg = "The given user has reached his/her account api limit, please retry after " + expireAfter + " ms.";
        s_logger.warn(msg);
        throw new RequestLimitException(msg);
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) RequestLimitException(com.cloud.legacymodel.exceptions.RequestLimitException)

Aggregations

RequestLimitException (com.cloud.legacymodel.exceptions.RequestLimitException)1 Account (com.cloud.legacymodel.user.Account)1