/** * Check whether that caller can access or not ,based on current state and pre-defined policy * * @param throttleContext -The Context for this caller - runtime state * @param configuration -The Configuration for this caller - data from policy * @param currentTime - The current system time * @return boolean -The boolean value which say access will allow or not * @throws ThrottleException throws for invalid throttle configuration */ public boolean canAccess( ThrottleContext throttleContext, CallerConfiguration configuration, long currentTime) throws ThrottleException { boolean canAccess; if (configuration == null) { if (log.isDebugEnabled()) { log.debug("Couldn't find the configuration ."); } return true; } if (configuration.getMaximumRequestPerUnitTime() < 0 || configuration.getUnitTime() <= 0 || configuration.getProhibitTimePeriod() < 0) { throw new ThrottleException("Invalid Throttle Configuration"); } // if caller access first time in his new session if (this.firstAccessTime == 0) { initAccess(configuration, throttleContext, currentTime); } // if unit time period (session time) is not over if (this.nextTimeWindow > currentTime) { canAccess = canAccessIfUnitTimeNotOver(configuration, throttleContext, currentTime); } else { canAccess = canAccessIfUnitTimeOver(configuration, throttleContext, currentTime); } return canAccess; }
/** * To verify access if the unit time has already not over * * @param configuration - -The Configuration for this caller * @param throttleContext -The Throttle Context * @param currentTime -The system current time * @return boolean -The boolean value which say access will allow or not */ private boolean canAccessIfUnitTimeNotOver( CallerConfiguration configuration, ThrottleContext throttleContext, long currentTime) { boolean canAccess = false; int maxRequest = configuration.getMaximumRequestPerUnitTime(); if (maxRequest != 0) { if ((this.globalCount.get() + this.localCount.get()) < maxRequest) { // If the globalCount is less than max request if (log.isDebugEnabled()) { log.debug( "CallerContext Checking access if unit time is not over and less than max count>> Access " + "allowed=" + maxRequest + " available=" + (maxRequest - (this.globalCount.get() + this.localCount.get())) + " key=" + this.getId() + " currentGlobalCount=" + globalCount + " currentTime=" + currentTime + " " + "nextTimeWindow=" + this.nextTimeWindow + " currentLocalCount=" + localCount + " Tier=" + configuration.getID() + " nextAccessTime=" + this.nextAccessTime); } canAccess = true; // can continue access this.localCount.incrementAndGet(); // Send the current state to others (clustered env) throttleContext.flushCallerContext(this, id); // can complete access } else { // else , if caller has not already prohibit if (this.nextAccessTime == 0) { // and if there is no prohibit time period in configuration long prohibitTime = configuration.getProhibitTimePeriod(); if (prohibitTime == 0) { // prohibit access until unit time period is over this.nextAccessTime = this.firstAccessTime + configuration.getUnitTime(); } else { // if there is a prohibit time period in configuration ,then // set it as prohibit period this.nextAccessTime = currentTime + prohibitTime; } if (log.isDebugEnabled()) { String type = ThrottleConstants.IP_BASE == configuration.getType() ? "IP address" : "domain"; log.debug( "Maximum Number of requests are reached for caller with " + type + " - " + this.id); } // Send the current state to others (clustered env) throttleContext.flushCallerContext(this, id); } else { // else , if the caller has already prohibit and prohibit // time period has already over if (this.nextAccessTime <= currentTime) { if (log.isDebugEnabled()) { log.debug( "CallerContext Checking access if unit time is not over before time window exceed >> " + "Access allowed=" + maxRequest + " available=" + (maxRequest - (this.globalCount.get() + this.localCount.get())) + " key=" + this.getId() + " currentGlobalCount=" + globalCount + " currentTime=" + currentTime + " " + "nextTimeWindow=" + this.nextTimeWindow + " currentLocalCount=" + localCount + " " + "Tier=" + configuration.getID() + " nextAccessTime=" + this.nextAccessTime); } // remove previous caller context if (this.nextTimeWindow != 0) { throttleContext.removeCallerContext(id); } // reset the states so that, this is the first access this.nextAccessTime = 0; canAccess = true; this.globalCount.set(0); // can access the system and this is same as first access this.localCount.set(1); this.firstAccessTime = currentTime; this.nextTimeWindow = currentTime + configuration.getUnitTime(); throttleContext.addAndFlushCallerContext(this, this.id); throttleContext.replicateTimeWindow(this.id); if (log.isDebugEnabled()) { log.debug( "Caller=" + this.getId() + " has reset counters and added for replication when unit " + "time is not over"); } } else { if (log.isDebugEnabled()) { String type = ThrottleConstants.IP_BASE == configuration.getType() ? "IP address" : "domain"; log.debug( "Prohibit period is not yet over for caller with " + type + " - " + this.id); } } } } } return canAccess; }