public boolean checkCondition()

in wilma-extras/bulkhead/src/main/java/com/epam/wilma/extras/bulkhead/BulkHeadChecker.java [54:99]


    public boolean checkCondition(final WilmaHttpRequest request, final ParameterList parameterList) {
        //determine parameters
        String parameterHashCode = parameterList.get(BULKHEAD_PARAMETER_ID);
        String parameterSpeed = parameterList.get(BULKHEAD_PARAMETER_SPEED);
        if ((parameterHashCode == null) || (parameterHashCode.length() == 0)) {
            parameterHashCode = "ALLCASE_BULKHEAD"; // this is the default head id
        }
        double speedLimit;
        try {
            speedLimit = Double.valueOf(parameterSpeed);
        } catch (NumberFormatException e) {
            speedLimit = DEFAULT_SPEED_LIMIT;
        }

        // we need to know the actual time
        long now = System.currentTimeMillis();
        boolean skipRequest = false; //default return value is false that means the bulk head is not used

        //we have parameters set, we need to do things in synch
        synchronized (GUARD) {
            // first identify if we have measurement already or not
            BulkHeadMapInformation info = BULK_HEAD_MAP.get(parameterHashCode);
            if (info == null) {
                // we don't have, so this is the first measurement, we cannot measure the speed yet, just store the time
                BULK_HEAD_MAP.put(parameterHashCode, new BulkHeadMapInformation(now));
            } else {
                // we have time information, so we can measure the speed, and update the info
                long lastTime = info.getLastTime();
                if (lastTime != now) {
                    //this is what we expect, let's calculate the load
                    double speed = ONE_SEC / (now - lastTime);
                    info.setLastSpeed(speed);
                    skipRequest = speed > speedLimit;
                    if (!skipRequest) {
                        //if we don't skip the request, we update the reference time
                        info.setLastTime(now);
                    }
                } else {
                    //actually we are in trouble, as this should not happen, as this means infinite high load :)
                    skipRequest = true; //we need to skip it in case of infinite high load - just joking
                }
                BULK_HEAD_MAP.put(parameterHashCode, info);
            }
        }
        return skipRequest;
    }