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

Block a partcular machine for a particular period of time interval

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

所以假设如果 machineA 没有响应 (throwing RestClientException),我将每次调用 onFailure 方法并继续递增计数器,然后再次调用 machineA ,我通过传递 machineA 作为主机名和 5 作为阈值来检查 isBlocked 方法,因此如果 machineA 已被阻止 5 次,那么我根本不会调用它们。我的库是多线程的,这就是我在这里使用 volatile 的原因,因为我希望所有线程都看到相同的值。

下面是我在 DataMapping class:

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);        
}

如何在特定时间段内阻止特定机器​​,并在该时间间隔结束后才开始调用它们?

您可以使用 ScheduledExecutorServiceschedule 在特定超时后重置计数器。

您可以在 DataMapping class:

中声明
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);
    }
}