Search in sources :

Example 1 with TbRateLimits

use of org.thingsboard.server.common.msg.tools.TbRateLimits in project thingsboard by thingsboard.

the class TbWebSocketHandler method send.

@Override
public void send(TelemetryWebSocketSessionRef sessionRef, int subscriptionId, String msg) throws IOException {
    String externalId = sessionRef.getSessionId();
    log.debug("[{}] Processing {}", externalId, msg);
    String internalId = externalSessionMap.get(externalId);
    if (internalId != null) {
        SessionMetaData sessionMd = internalSessionMap.get(internalId);
        if (sessionMd != null) {
            if (!StringUtils.isEmpty(perSessionUpdatesConfiguration)) {
                TbRateLimits rateLimits = perSessionUpdateLimits.computeIfAbsent(sessionRef.getSessionId(), sid -> new TbRateLimits(perSessionUpdatesConfiguration));
                if (!rateLimits.tryConsume()) {
                    if (blacklistedSessions.putIfAbsent(externalId, sessionRef) == null) {
                        log.info("[{}][{}][{}] Failed to process session update. Max session updates limit reached", sessionRef.getSecurityCtx().getTenantId(), sessionRef.getSecurityCtx().getId(), externalId);
                        sessionMd.sendMsg("{\"subscriptionId\":" + subscriptionId + ", \"errorCode\":" + ThingsboardErrorCode.TOO_MANY_UPDATES.getErrorCode() + ", \"errorMsg\":\"Too many updates!\"}");
                    }
                    return;
                } else {
                    log.debug("[{}][{}][{}] Session is no longer blacklisted.", sessionRef.getSecurityCtx().getTenantId(), sessionRef.getSecurityCtx().getId(), externalId);
                    blacklistedSessions.remove(externalId);
                }
            }
            sessionMd.sendMsg(msg);
        } else {
            log.warn("[{}][{}] Failed to find session by internal id", externalId, internalId);
        }
    } else {
        log.warn("[{}] Failed to find session by external id", externalId);
    }
}
Also used : TbRateLimits(org.thingsboard.server.common.msg.tools.TbRateLimits)

Example 2 with TbRateLimits

use of org.thingsboard.server.common.msg.tools.TbRateLimits in project thingsboard by thingsboard.

the class RateLimitProcessingFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    SecurityUser user = getCurrentUser();
    if (user != null && !user.isSystemAdmin()) {
        if (perTenantLimitsEnabled) {
            TbRateLimits rateLimits = perTenantLimits.computeIfAbsent(user.getTenantId(), id -> new TbRateLimits(perTenantLimitsConfiguration));
            if (!rateLimits.tryConsume()) {
                errorResponseHandler.handle(new TbRateLimitsException(EntityType.TENANT), (HttpServletResponse) response);
                return;
            }
        }
        if (perCustomerLimitsEnabled && user.isCustomerUser()) {
            TbRateLimits rateLimits = perCustomerLimits.computeIfAbsent(user.getCustomerId(), id -> new TbRateLimits(perCustomerLimitsConfiguration));
            if (!rateLimits.tryConsume()) {
                errorResponseHandler.handle(new TbRateLimitsException(EntityType.CUSTOMER), (HttpServletResponse) response);
                return;
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) TbRateLimits(org.thingsboard.server.common.msg.tools.TbRateLimits) TbRateLimitsException(org.thingsboard.server.common.msg.tools.TbRateLimitsException)

Example 3 with TbRateLimits

use of org.thingsboard.server.common.msg.tools.TbRateLimits in project thingsboard by thingsboard.

the class ActorSystemContext method checkLimits.

private boolean checkLimits(TenantId tenantId, TbMsg tbMsg, Throwable error) {
    if (debugPerTenantEnabled) {
        DebugTbRateLimits debugTbRateLimits = debugPerTenantLimits.computeIfAbsent(tenantId, id -> new DebugTbRateLimits(new TbRateLimits(debugPerTenantLimitsConfiguration), false));
        if (!debugTbRateLimits.getTbRateLimits().tryConsume()) {
            if (!debugTbRateLimits.isRuleChainEventSaved()) {
                persistRuleChainDebugModeEvent(tenantId, tbMsg.getRuleChainId(), error);
                debugTbRateLimits.setRuleChainEventSaved(true);
            }
            if (log.isTraceEnabled()) {
                log.trace("[{}] Tenant level debug mode rate limit detected: {}", tenantId, tbMsg);
            }
            return false;
        }
    }
    return true;
}
Also used : DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits) TbRateLimits(org.thingsboard.server.common.msg.tools.TbRateLimits) DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits)

Aggregations

TbRateLimits (org.thingsboard.server.common.msg.tools.TbRateLimits)3 DebugTbRateLimits (org.thingsboard.server.actors.tenant.DebugTbRateLimits)1 TbRateLimitsException (org.thingsboard.server.common.msg.tools.TbRateLimitsException)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1