lib/rateLimiter/lib/rateLimiter.js
function (key, intervalSize, limit) {
        var deferred = Q.defer()
            , memcached = Shared.memcached();
        if (!memcached) {
            deferred.reject();
        }
        else {
            //Get current rate for ip
            memcached.get(key, function (err, value) {
                if (err) {
                    //Allow access as failover
                    Logger.warn("Rate limit set but memcached error, allow access without limit", err);
                    deferred.reject();
                }
                else if (_.isUndefined(value)) {
                    memcached.set(key, 1, 60 * intervalSize, function (err) {
                        if (err) {
                            Logger.warn("Rate limit set but memcached error, allow access without limit", err);
                        }
                    });
                    deferred.resolve(1);
                }
                else {
                    //Do not increase rate counter if exceeded anyway
                    if (value < limit) {
                        // Increase rate counter by 1
                        memcached.incr(key, 1, function (err) {
                            if (err) {
                                Logger.warn("Rate limit set but memcached error, allow access without limit", err);
                            }
                        });
                    }
                    deferred.resolve(value + 1);
                }
            });
        }
        return deferred.promise;
    }
function (key, intervalSize, limit) {
        var deferred = Q.defer()
            , memcached = Shared.memcached();
        if (!memcached) {
            deferred.reject();
        }
        else {
            //Get current rate for ip
            memcached.get(key, function (err, value) {
                if (err) {
                    //Allow access as failover
                    Logger.warn("Rate limit set but memcached error, allow access without limit", err);
                    deferred.reject();
                }
                else if (_.isUndefined(value)) {
                    memcached.set(key, 1, 60 * intervalSize, function (err) {
                        if (err) {
                            Logger.warn("Rate limit set but memcached error, allow access without limit", err);
                        }
                    });
                    deferred.resolve(1);
                }
                else {
                    //Do not increase rate counter if exceeded anyway
                    if (value < limit) {
                        // Increase rate counter by 1
                        memcached.incr(key, 1, function (err) {
                            if (err) {
                                Logger.warn("Rate limit set but memcached error, allow access without limit", err);
                            }
                        });
                    }
                    deferred.resolve(value + 1);
                }
            });
        }
        return deferred.promise;
    }
function ( key , intervalSize , limit ) { var deferred = Q . defer ( ) , memcached = Shared . memcached ( ) ; if ( ! memcached ) { deferred . reject ( ) ; } else { //Get current rate for ip memcached . get ( key , function ( err , value ) { if ( err ) { //Allow access as failover Logger . warn ( "Rate limit set but memcached error, allow access without limit" , err ) ; deferred . reject ( ) ; } else if ( _ . isUndefined ( value ) ) { memcached . set ( key , 1 , 60 * intervalSize , function ( err ) { if ( err ) { Logger . warn ( "Rate limit set but memcached error, allow access without limit" , err ) ; } } ) ; deferred . resolve ( 1 ) ; } else { //Do not increase rate counter if exceeded anyway if ( value < limit ) { // Increase rate counter by 1 memcached . incr ( key , 1 , function ( err ) { if ( err ) { Logger . warn ( "Rate limit set but memcached error, allow access without limit" , err ) ; } } ) ; } deferred . resolve ( value + 1 ) ; } } ) ; } return deferred . promise ; }
Validate and increase current rate limit in memcache
Validate and increase current rate limit in memcache