                     return
                 return
 
-            destroy: (client)=>
+            destroy: (client)->
                 return if client._destroying
-                logger.debug "#{@_factory.name} destroy", @id
+                logger.debug "[#{self._factory.name}] [#{self.id}] destroy"
                 client._destroying = true
                 client.end()
                 return
 
-            max: @options.maxConnection
-            min: @options.minConnection
-            idle: @options.idleTimeout * 1000
-
-        @check(next) if typeof next is 'function'
+            max: self.options.maxConnection
+            min: self.options.minConnection
+            idle: self.options.idleTimeout * 1000
+
+        self.check(next) if typeof next is 'function'
         return
 
     check: (next)->

        @_avalaible.splice 0, @_avalaible.length
        return
    _ensureMinimum: ->
        if @_factory.min > @_created.length
            @acquire (err, client)=>
                return @emit 'error', err if err
                @release client
                @_ensureMinimum()
                return
        return


    getName: ->
        @_factory.name
    acquire: (callback, opts = {})->
        self = @
        self.semTake
            priority: opts.priority
            num: 1
            timeOut: opts.timeOut
            onTimeOut: opts.onTimeOut
            onTake: ->
                logger.debug "[#{self._factory.name}] [#{self.id}] acquire", self._avalaible.length
                if self._avalaible.length is 0
                    self._factory.create (err, client)->
                        return callback err if err
                        self._created.push client
                        self._removeIdle client
                        callback null, client
                        return
                    return
                client = self._avalaible.shift()
                self._removeIdle client
                callback null, client
                return
    release: (client)->
        index = @_created.indexOf client
        if ~index
            logger.debug "[#{this._factory.name}] [#{this.id}] release '#{client.id}'. Avalaible #{this._avalaible.length}"
            @_avalaible.push client
            @_idle client
            return @semGive()
        return false
    _idle: (client)->
        self = @
        if @_factory.idle > 0
            self._removeIdle client
            self._timers[client.id] = setTimeout ->
                self.destroy client
                return
            , self._factory.idle
        return
    _removeIdle: (client)->
        clearTimeout @_timers[client.id]
        delete @_timers[client.id]
        return
    destroy: (client, force)->
        index = @_created.indexOf client

        # ensure minimum
        if ~index
            if force or @_factory.min < @_created.length
                logger.debug "[#{this._factory.name}] [#{this.id}] destroying '#{client.id}'. #{this._avalaible.length}/#{this._created.length}"
                @_created.splice index, 1

                # remove it from available since it will be destroyed
                index = @_avalaible.indexOf client
                @_avalaible.splice index, 1 if ~index

                @_factory.destroy client
                logger.debug "[#{this._factory.name}] [#{this.id}] destroyed '#{client.id}'. #{this._avalaible.length}/#{this._created.length}"

                @_ensureMinimum() if force
                return true
            else
                @_idle client
        return false
    _superDestroy: (safe, _onDestroy)->
        SemaphorePool.__super__.destroy.call @, safe, _onDestroy

    destroyAll: (safe, _onDestroy)->
        self = @
        if safe isnt false
            self._superDestroy true, ->
                self._onDestroy()
                _onDestroy() if 'function' is typeof _onDestroy
                return
        else
            self._superDestroy false, ->
                self._onDestroy()
                _onDestroy() if 'function' is typeof _onDestroy
                return
        return
