Advertisement
cha0s

limiter package for Shrub

Mar 12th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Promise = require 'bluebird'
  3. redis = require 'connect-redis/node_modules/redis'
  4. redback = require('redback').use redis.createClient()
  5.  
  6. pkgman = require 'pkgman'
  7.  
  8. limiter = redback.createRateLimit 'limiter'
  9.  
  10. exports.Limiter = class Limiter
  11.    
  12.     constructor: (@req) ->
  13.    
  14.     add: (key) ->
  15.        
  16.         add = Promise.promisify limiter.add, limiter
  17.         Promise.all(add key for key in @_keys key)
  18.    
  19.     addAndCheckThreshold: (key, threshold) ->
  20.    
  21.         unless threshold instanceof ThresholdTime
  22.             return Promise.reject new TypeError "The second parameter to Limiter::addAndCheckThreshold must be a valid threshold!"
  23.            
  24.         @add(key).then => @checkThreshold key, threshold
  25.    
  26.     count: (key, seconds) ->
  27.        
  28.         count = Promise.promisify limiter.count, limiter
  29.         Promise.all(count key, seconds for key in @_keys key).then(
  30.  
  31.             (counts) -> counts.reduce(
  32.                 (l, r) -> if l > r then l else r
  33.                 -Infinity
  34.             )
  35.  
  36.             (error) -> reject error
  37.         )
  38.  
  39.     checkThreshold: (key, threshold) ->
  40.        
  41.         unless threshold instanceof ThresholdTime
  42.             return Promise.reject new TypeError "The second parameter to Limiter::checkThreshold must be a valid threshold!"
  43.            
  44.         @count(key, threshold.calculateSeconds()).then (count) ->
  45.             count > threshold.count()
  46.                
  47.     _keys: (root) ->
  48.        
  49.         auditKeys = {}
  50.         pkgman.invoke 'auditKeys', (path, fn) => auditKeys[path] = fn @req
  51.        
  52.         keys = []
  53.         for path, suffixes of auditKeys
  54.             keys.push "#{root}:#{path}:#{suffix}" for suffix in suffixes
  55.        
  56.         keys
  57.  
  58. exports.threshold = (count) -> new Threshold count
  59.                
  60. class Threshold
  61.    
  62.     constructor: (@_count) ->
  63.    
  64.     every: (amount) -> new ThresholdTime @_count, amount
  65.    
  66. class ThresholdTime
  67.    
  68.     constructor: (@_count, @_amount) ->
  69.        
  70.         @_multiplier = 1
  71.    
  72.     calculateSeconds: -> @_amount * @_multiplier
  73.    
  74.     count: -> @_count
  75.    
  76.     multipliers =
  77.         ms: 1 / 1000
  78.         seconds: 1
  79.         minutes: 60
  80.    
  81.     for key, multiplier of multipliers         
  82.         do (key, multiplier) =>
  83.             @::[key] = ->
  84.                 @_multiplier = multiplier
  85.                 this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement