committed by
GitHub
17 changed files with 251 additions and 25 deletions
@ -0,0 +1,74 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.transport.limits; |
||||
|
|
||||
|
import com.github.benmanes.caffeine.cache.Cache; |
||||
|
import com.github.benmanes.caffeine.cache.Caffeine; |
||||
|
import com.github.benmanes.caffeine.cache.Expiry; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.jetbrains.annotations.NotNull; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.queue.util.TbTransportComponent; |
||||
|
|
||||
|
import java.util.concurrent.ThreadLocalRandom; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
@Service |
||||
|
@TbTransportComponent |
||||
|
@Slf4j |
||||
|
public class DefaultEntityLimitsCache implements EntityLimitsCache { |
||||
|
|
||||
|
private static final int DEVIATION = 10; |
||||
|
private final Cache<EntityLimitKey, Boolean> cache; |
||||
|
|
||||
|
public DefaultEntityLimitsCache(@Value("${cache.entityLimits.timeToLiveInMinutes:5}") int ttl, |
||||
|
@Value("${cache.entityLimits.maxSize:100000}") int maxSize) { |
||||
|
// We use the 'random' expiration time to avoid peak loads.
|
||||
|
long mainPart = (TimeUnit.MINUTES.toNanos(ttl) / 100) * (100 - DEVIATION); |
||||
|
long randomPart = (TimeUnit.MINUTES.toNanos(ttl) / 100) * DEVIATION; |
||||
|
cache = Caffeine.newBuilder() |
||||
|
.expireAfter(new Expiry<EntityLimitKey, Boolean>() { |
||||
|
@Override |
||||
|
public long expireAfterCreate(@NotNull EntityLimitKey key, @NotNull Boolean value, long currentTime) { |
||||
|
return mainPart + (long) (randomPart * ThreadLocalRandom.current().nextDouble()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public long expireAfterUpdate(@NotNull EntityLimitKey key, @NotNull Boolean value, long currentTime, long currentDuration) { |
||||
|
return currentDuration; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public long expireAfterRead(@NotNull EntityLimitKey key, @NotNull Boolean value, long currentTime, long currentDuration) { |
||||
|
return currentDuration; |
||||
|
} |
||||
|
}) |
||||
|
.maximumSize(maxSize) |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean get(EntityLimitKey key) { |
||||
|
var result = cache.getIfPresent(key); |
||||
|
return result != null ? result : false; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void put(EntityLimitKey key, boolean value) { |
||||
|
cache.put(key, value); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.transport.limits; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
@Data |
||||
|
public class EntityLimitKey { |
||||
|
|
||||
|
private final TenantId tenantId; |
||||
|
private final String deviceName; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.transport.limits; |
||||
|
|
||||
|
public interface EntityLimitsCache { |
||||
|
|
||||
|
boolean get(EntityLimitKey key); |
||||
|
|
||||
|
void put(EntityLimitKey key, boolean value); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.exception; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
public class EntitiesLimitException extends DataValidationException { |
||||
|
private static final long serialVersionUID = -9211462514373279196L; |
||||
|
|
||||
|
@Getter |
||||
|
private final TenantId tenantId; |
||||
|
@Getter |
||||
|
private final EntityType entityType; |
||||
|
|
||||
|
public EntitiesLimitException(TenantId tenantId, EntityType entityType) { |
||||
|
super(entityType.getNormalName() + "s limit reached"); |
||||
|
this.tenantId = tenantId; |
||||
|
this.entityType = entityType; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue