     # by default client tries to connect forever, fail after half a sec so worker can continue
     # will try again on next job
     unless @_redis?
-      @_redis = redis.createClient @port, @host, { retry_max_delay:100, connect_timeout: 500, max_attempts: 10 }
+      @_redis = memoized_redis_client @host, @port
       @_redis.once 'error', (err) =>
         @log "RESERVE: Error connecting to REDIS: #{@host}:#{@port}.", err
         throw err

  _set_expiration: (cb) ->
    return setImmediate cb unless @_reserve_key?  # nothing reserved here
    @_init_redis()
    
    # set expiration to lock_ttl
    @_redis.expire @_reserve_key, @lock_ttl, (err, state) =>
      @log "RESERVE: Failed to set expiration for #{@_reserve_key} after reservation", err if err?

redis = require 'redis'
async = require 'async'
debug = require('debug') 'redis-reservation'

create_redis_client = (host, port) ->
  redis.createClient port, host, { retry_max_delay:100, connect_timeout: 500, max_attempts: 10 }
memoized_redis_client = _.memoize create_redis_client, (host, port) -> "#{host}:#{port}"

module.exports = class ReserveResource
  constructor: (@by, @host, @port, @heartbeat_interval, @lock_ttl, @log=console.log) ->
