|
|
|
@ -13,14 +13,13 @@ |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.dao.util.limits; |
|
|
|
package org.thingsboard.server.cache.limits; |
|
|
|
|
|
|
|
import com.github.benmanes.caffeine.cache.Cache; |
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine; |
|
|
|
import lombok.Data; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.server.common.data.StringUtils; |
|
|
|
import org.thingsboard.server.common.data.TenantProfile; |
|
|
|
@ -28,10 +27,9 @@ import org.thingsboard.server.common.data.exception.TenantProfileNotFoundExcepti |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.limit.LimitedApi; |
|
|
|
import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor; |
|
|
|
import org.thingsboard.server.common.data.notification.rule.trigger.RateLimitsTrigger; |
|
|
|
import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor; |
|
|
|
import org.thingsboard.server.common.msg.tools.TbRateLimits; |
|
|
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@ -39,14 +37,14 @@ import java.util.concurrent.TimeUnit; |
|
|
|
@Slf4j |
|
|
|
public class DefaultRateLimitService implements RateLimitService { |
|
|
|
|
|
|
|
private final TbTenantProfileCache tenantProfileCache; |
|
|
|
private final TenantProfileProvider tenantProfileProvider; |
|
|
|
private final NotificationRuleProcessor notificationRuleProcessor; |
|
|
|
|
|
|
|
public DefaultRateLimitService(TbTenantProfileCache tenantProfileCache, |
|
|
|
@Lazy NotificationRuleProcessor notificationRuleProcessor, |
|
|
|
public DefaultRateLimitService(TenantProfileProvider tenantProfileProvider, |
|
|
|
NotificationRuleProcessor notificationRuleProcessor, |
|
|
|
@Value("${cache.rateLimits.timeToLiveInMinutes:120}") int rateLimitsTtl, |
|
|
|
@Value("${cache.rateLimits.maxSize:200000}") int rateLimitsCacheMaxSize) { |
|
|
|
this.tenantProfileCache = tenantProfileCache; |
|
|
|
this.tenantProfileProvider = tenantProfileProvider; |
|
|
|
this.notificationRuleProcessor = notificationRuleProcessor; |
|
|
|
this.rateLimits = Caffeine.newBuilder() |
|
|
|
.expireAfterAccess(rateLimitsTtl, TimeUnit.MINUTES) |
|
|
|
@ -66,7 +64,7 @@ public class DefaultRateLimitService implements RateLimitService { |
|
|
|
if (tenantId.isSysTenantId()) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
TenantProfile tenantProfile = tenantProfileCache.get(tenantId); |
|
|
|
TenantProfile tenantProfile = tenantProfileProvider.get(tenantId); |
|
|
|
if (tenantProfile == null) { |
|
|
|
throw new TenantProfileNotFoundException(tenantId); |
|
|
|
} |