java - 在特定时间间隔内阻止特定机器

标签 java multithreading guava atomic google-guava-cache

我正在开发一个库,在其中对我的服务进行 Http 调用,如果我的服务计算机没有响应(存在套接字超时或连接超时),我会将它们添加到我的本地 blockList 如果机器被阻塞 5 次,那么我不会调用他们。

所以假设如果 machineA 没有响应(抛出 RestClientException),我将每次调用 onFailure 方法并不断增加计数器,然后再次调用 machineA 时,我通过传递 machineA 作为主机名和 5 作为阈值来检查 isBlocked 方法,因此如果 machineA > 已经被屏蔽5次了,然后我就根本不给他们打电话了。我的库是多线程的,所以这就是我在这里使用 volatile 的原因,因为我希望所有线程看到相同的值。

下面是我在 DataMapping 类中的内容:

public static volatile ConcurrentHashMap<String, AtomicInteger> blockedHosts =
      new ConcurrentHashMap<String, AtomicInteger>();

boolean isBlocked(String hostname, int threshold) {
    AtomicInteger count = blockedHosts.get(hostname);
    return count != null && count.get() >= threshold;
}

void onFailure(String hostname) {
    AtomicInteger newValue = new AtomicInteger();
    AtomicInteger val = blockedHosts.putIfAbsent(hostname, newValue);
    // no need to care about over-reaching 5 here
    (val == null ? newValue : val).incrementAndGet();
}

void onSuccess(String hostname) {
    blockedHosts.remove(hostname);
}

问题陈述:-

现在我想再添加一项功能,即 - 如果 machineA 被阻止(因为它的阻止计数 >= 5),那么我想让它保持阻止 x 间隔。我将有另一个参数 (key.getInterval()) ,它会告诉我们我想让这台机器保持阻塞多长时间,并且在该间隔过去之后,只有我才会开始调用它们。我不明白如何添加此功能?

下面是我的主线程代码,我在其中使用 DataMapping 方法来检查主机名是否被阻止以及阻止主机名。

@Override
public DataResponse call() {
    ResponseEntity<String> response = null;

    List<String> hostnames = some_code_here;

    for (String hostname : hostnames) {
        // If hostname is in block list, skip sending request to this host
        if (DataMapping.isBlocked(hostname)) {
            continue;
        }
        try {
            String url = createURL(hostname);
            response = restTemplate.exchange(url, HttpMethod.GET, key.getEntity(), String.class);
            DataMapping.onSuccess(hostname);

            // some code here to return the response if successful
        } catch (RestClientException ex) {
            // adding to block list
            DataMapping.onFailure(hostname);
        }
    }

    return new DataResponse(DataErrorEnum.SERVER_UNAVAILABLE, DataStatusEnum.ERROR);        
}

如何在特定的时间段内阻止特定的计算机,并且一旦该时间间隔过去,才开始调用它们?

最佳答案

您可以使用 ScheduledExecutorService安排在一定的超时后重置计数器。

您可以在 DataMapping 类中声明这一点:

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // or perhaps the thread pool version ?

在您的 onFailure() 方法中,您可以决定是要重置还是在一定的超时后减少计数器:

void onFailure(String hostname) {
    // you can use `computeIfAbsent` in java8
    AtomicInteger val = blockedHosts.computeIfAbsent(hostname, key -> new AtomicInteger());
    int count = val.incrementAndGet();
    // the test here is `==` to make sure the task is scheduled only once
    if (count == threshold) {
        scheduler.schedule(() -> blockedHosts.remove(hostname), 5L, TimeUnit.MINUTES);  // or you may choose to just decrement the counter
    }
}

顺便说一句,没有理由使 blockedHosts volatile 。该引用永远不会改变;它应该是final;可能是私有(private)

<小时/>

在java7中,上面的代码看起来像这样:

void onFailure(String hostname) {
    AtomicInteger newValue = new AtomicInteger();
    AtomicInteger val = blockedHosts.putIfAbsent(hostname, newValue);
    int count = (val == null ? newValue : val).incrementAndGet();
    // the test here is `==` to make sure the task is scheduled only once
    if (count == threshold) {
        scheduler.schedule(new Runnable() {
            @Override public void run() {
                blockedHosts.remove(hostname);  // or you may choose to just decrement the counter
            }
        }, 5L, TimeUnit.MINUTES);
    }
}

关于java - 在特定时间间隔内阻止特定机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37244721/

相关文章:

java - 从 Java 结果集中读取 Unicode 文本

java - Copy-On-Write 与直接锁定/同步写入方法有何不同?

c - 多线程程序中段错误的原因

java - 使用 Java Guava 库,如何使用 Builder 创建 ImmutableSortedSet?

java - Junit 在 Suite 类之间共享外部资源

java - 读入和写出

c# - 我真的锁定了这些东西吗

java - 如何在没有正则表达式的情况下从字符串中提取数字

java - 有没有一种简单的方法可以在 Guava 中循环标准输入?

java - 如何确定是否以编程方式在 android 中检查了 USE NETWORK PROVIDED VALUES?