-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from thingsboard/sessions-limit-update
[1.3.1] Sessions limit improvement
- Loading branch information
Showing
16 changed files
with
451 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ation/src/main/java/org/thingsboard/mqtt/broker/service/limits/RateLimitCacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright © 2016-2024 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 | ||
* | ||
* https://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.mqtt.broker.service.limits; | ||
|
||
public interface RateLimitCacheService { | ||
|
||
void initSessionCount(int count); | ||
|
||
long incrementSessionCount(); | ||
|
||
void decrementSessionCount(); | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...in/java/org/thingsboard/mqtt/broker/service/limits/RateLimitCaffeineCacheServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright © 2016-2024 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 | ||
* | ||
* https://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.mqtt.broker.service.limits; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.stereotype.Service; | ||
import org.thingsboard.mqtt.broker.cache.CacheConstants; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
@Service | ||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class RateLimitCaffeineCacheServiceImpl implements RateLimitCacheService { | ||
|
||
private final CacheManager cacheManager; | ||
|
||
private Cache<String, Long> clientSessionsLimitCache; | ||
|
||
@Value("${mqtt.sessions-limit:0}") | ||
@Setter | ||
private int sessionsLimit; | ||
|
||
@PostConstruct | ||
public void init() { | ||
if (sessionsLimit <= 0) { | ||
return; | ||
} | ||
clientSessionsLimitCache = (Cache<String, Long>) cacheManager.getCache(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE).getNativeCache(); | ||
} | ||
|
||
@Override | ||
public void initSessionCount(int count) { | ||
if (sessionsLimit <= 0) { | ||
return; | ||
} | ||
log.debug("Initializing session count"); | ||
clientSessionsLimitCache.asMap().putIfAbsent(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE_KEY, (long) count); | ||
} | ||
|
||
@Override | ||
public long incrementSessionCount() { | ||
log.debug("Incrementing session count"); | ||
return clientSessionsLimitCache.asMap().compute(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE_KEY, (k, v) -> (v == null ? 1L : v + 1)); | ||
} | ||
|
||
@Override | ||
public void decrementSessionCount() { | ||
if (sessionsLimit <= 0) { | ||
return; | ||
} | ||
log.debug("Decrementing session count"); | ||
clientSessionsLimitCache.asMap().computeIfPresent(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE_KEY, (k, v) -> v > 0 ? v - 1 : 0); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
.../main/java/org/thingsboard/mqtt/broker/service/limits/RateLimitRedisCacheServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright © 2016-2024 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 | ||
* | ||
* https://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.mqtt.broker.service.limits; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.stereotype.Service; | ||
import org.thingsboard.mqtt.broker.cache.CacheConstants; | ||
|
||
/** | ||
* should not be null since not used in pipeline / transaction. | ||
*/ | ||
@Service | ||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class RateLimitRedisCacheServiceImpl implements RateLimitCacheService { | ||
|
||
private final RedisTemplate<String, Object> redisTemplate; | ||
|
||
@Value("${mqtt.sessions-limit:0}") | ||
@Setter | ||
private int sessionsLimit; | ||
|
||
@Override | ||
public void initSessionCount(int count) { | ||
if (sessionsLimit <= 0) { | ||
return; | ||
} | ||
ValueOperations<String, Object> valueOps = redisTemplate.opsForValue(); | ||
boolean initialized = valueOps.setIfAbsent(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE_KEY, Integer.toString(count)); | ||
if (initialized) { | ||
log.info("Session count initialized to {}", count); | ||
} | ||
} | ||
|
||
@Override | ||
public long incrementSessionCount() { | ||
log.debug("Incrementing session count"); | ||
ValueOperations<String, Object> valueOps = redisTemplate.opsForValue(); | ||
return valueOps.increment(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE_KEY); | ||
} | ||
|
||
@Override | ||
public void decrementSessionCount() { | ||
if (sessionsLimit <= 0) { | ||
return; | ||
} | ||
log.debug("Decrementing session count"); | ||
ValueOperations<String, Object> valueOps = redisTemplate.opsForValue(); | ||
valueOps.decrement(CacheConstants.CLIENT_SESSIONS_LIMIT_CACHE_KEY); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.