import java.io.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ConfigGuard {
private static final int MAX_REQUESTS = 10; // Maximum number of requests allowed per time window
private static final long TIME_WINDOW_MS = 60000; // Time window in milliseconds (e.g., 60 seconds)
private static final AtomicInteger requestCount = new AtomicInteger(0);
private static final Object lock = new Object();
public static boolean isAllowed() {
synchronized (lock) {
if (requestCount.get() >= MAX_REQUESTS) {
return false; // Rate limit exceeded
}
requestCount.incrementAndGet();
return true; // Request allowed
}
}
public static void resetCount() {
synchronized (lock) {
requestCount.set(0);
}
}
public static void main(String[] args) throws IOException {
// Example Usage (Simulate reading a configuration file)
if (isAllowed()) {
System.out.println("Accessing configuration file...");
// Simulate reading from a config file
try (BufferedReader reader = new BufferedReader(new FileReader("config.properties"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Line: " + line);
}
} catch (IOException e) {
System.err.println("Error reading config file: " + e.getMessage());
}
resetCount(); // Reset count after successful access
} else {
System.out.println("Rate limit exceeded. Access denied.");
}
}
}
Add your comment