|
|
|
@ -1,12 +1,12 @@ |
|
|
|
/** |
|
|
|
* Copyright © 2016-2017 The Thingsboard Authors |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* 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
|
|
|
|
* |
|
|
|
* <p> |
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* <p> |
|
|
|
* 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. |
|
|
|
@ -15,12 +15,15 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.common.transport.quota.inmemory; |
|
|
|
|
|
|
|
import com.google.common.collect.Sets; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
@ -35,11 +38,17 @@ public class HostRequestIntervalRegistry { |
|
|
|
private final Map<String, IntervalCount> hostCounts = new ConcurrentHashMap<>(); |
|
|
|
private final long intervalDurationMs; |
|
|
|
private final long ttlMs; |
|
|
|
private final Set<String> whiteList; |
|
|
|
private final Set<String> blackList; |
|
|
|
|
|
|
|
public HostRequestIntervalRegistry(@Value("${quota.host.intervalMs}") long intervalDurationMs, |
|
|
|
@Value("${quota.host.ttlMs}") long ttlMs) { |
|
|
|
@Value("${quota.host.ttlMs}") long ttlMs, |
|
|
|
@Value("${quota.host.whitelist}") String whiteList, |
|
|
|
@Value("${quota.host.blacklist}") String blackList) { |
|
|
|
this.intervalDurationMs = intervalDurationMs; |
|
|
|
this.ttlMs = ttlMs; |
|
|
|
this.whiteList = Sets.newHashSet(StringUtils.split(whiteList, ',')); |
|
|
|
this.blackList = Sets.newHashSet(StringUtils.split(blackList, ',')); |
|
|
|
} |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
@ -47,11 +56,15 @@ public class HostRequestIntervalRegistry { |
|
|
|
if (ttlMs < intervalDurationMs) { |
|
|
|
log.warn("TTL for IntervalRegistry [{}] smaller than interval duration [{}]", ttlMs, intervalDurationMs); |
|
|
|
} |
|
|
|
log.info("Start Host Quota Service with whitelist {}", whiteList); |
|
|
|
log.info("Start Host Quota Service with blacklist {}", blackList); |
|
|
|
} |
|
|
|
|
|
|
|
public long tick(String clientHostId) { |
|
|
|
if ("localhost".equals(clientHostId) || "127.0.0.1".equals(clientHostId)) { |
|
|
|
if (whiteList.contains(clientHostId)) { |
|
|
|
return 0; |
|
|
|
} else if (blackList.contains(clientHostId)) { |
|
|
|
return Long.MAX_VALUE; |
|
|
|
} |
|
|
|
IntervalCount intervalCount = hostCounts.computeIfAbsent(clientHostId, s -> new IntervalCount(intervalDurationMs)); |
|
|
|
return intervalCount.resetIfExpiredAndTick(); |
|
|
|
|