                     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)->

    getName: ->
        @_factory.name
    acquire: (callback, opts = {})->
        @semTake
            priority: opts.priority
            num: 1
            timeOut: opts.timeOut
            onTimeOut: opts.onTimeOut
            onTake: =>
                if @_avalaible.length is 0
                    @_factory.create (err, client)=>
                        return callback err if err
                        @_created.push client
                        @_removeIdle @_created.length - 1, client
                        callback null, client
                        return
                    return
                client = @_avalaible.shift()
                @_removeIdle @_created.indexOf(client), client
                callback null, client
                return
    release: (client)->
        index = @_created.indexOf client
        if ~index
            @_avalaible.push client
            @_idle index, client
            return @semGive()
        return false
    _idle: (index, client)->
        if @_factory.idle > 0
            return if @_factory.min is @_created.length
            @_timers[index] = setTimeout =>
                return if @_factory.min is @_created.length
                @destroy client
                return
            , @_factory.idle
        return
    _removeIdle: (index, client)->
        clearTimeout @_timers[index]
        return
    destroy: (client)->
        index = @_created.indexOf client
        if ~index
            @_created.splice index, 1
            @_factory.destroy client
            @_ensureMinimum()
            return true
        return false
    _superDestroy: (safe, _onDestroy)->
        SemaphorePool.__super__.destroy.call @, safe, _onDestroy

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

        @_avalaible.splice 0, @_avalaible.length
        return
    _ensureMinimum: ->
        self = @
        if self._factory.min > self._created.length
            logger.debug "[#{self._factory.name}] [#{self.id}] _ensureMinimum. #{self._created.length}/#{self._factory.min}"
            self.acquire (err, client)->
                return self.emit 'error', err if err
                self.release client
                self._ensureMinimum()
                return
        return

