public boolean checkAndIncrease ( ) { long newTimestamp = System . currentTimeMillis ( ) ; if ( isShutdown ) { if ( newTimestamp > shutdownEndTime ) { isShutdown = false ; largeLimitCalls = 0 ; smallLimitCalls = 0 ; } else { return false ; } } if ( newTimestamp - smallLimitTimestamp > smallTimeFrame ) { smallLimitCalls = 0 ; smallLimitTimestamp = newTimestamp ; } else { if ( smallLimitCalls >= smallCallLimit ) { return false ; } else { smallLimitCalls += 1 ; } } if ( newTimestamp - largeLimitTimestamp > largeTimeFrame ) { largeLimitCalls = 0 ; largeLimitTimestamp = newTimestamp ; } else { if ( largeLimitCalls >= largeCallsLimit ) { isShutdown = true ; shutdownEndTime = newTimestamp + shutdownPeriod ; return false ; } else { largeLimitCalls += 1 ; } } return true ; }
Check if next call is allowed and increase the counts .