This is a simple implementation of a Leaky bucket data structure as it was described in the book Release it.
In short, a leaky bucket can be set to monitor an operation and keep track of how often the operation fails and throw an exception if the operation fails too many times within a specific time frame.
Basic usage:
var action = LeakyBucket.monitor(s -> System.out.println(s));
try {
action.invoke("Hello!");
} catch (OperationFailedException e) {
e.getTargetException().printStackTrace();
} catch (LeakyBucket.BucketOverflowException e) {
// Do something in response to the failing operation
}