var pool = require('./pool.js');

var noop = function() {
};
var Lock = module.exports.Lock = function(key) {
	this.key = key;
	this.retries = 0;
	this.maxRetries = 20;
	this.retryDelay = 100;
};

Lock.prototype = {
	retry : function(delay, callback) {
		var self = this;
		if (self.retries > self.maxRetries) {
			return callback(null, null);
		}
		setTimeout(function() {
			self.acquire(callback);
		}, delay);
		this.retries++;
	},
	acquire : function(callback) {
		var self = this;
		acquire(this.key, {}, function(err, done) {
			if (err) {
				return callback(err, null);
			}
			if (done === null) {
				return self.retry(self.retryDelay, callback);
			}
			return callback(null, done);
		});
	},
	release : function(callback) {
		release(this.key, callback || noop);
	}
};

var acquire = module.exports.acquire = function(key, options, callback) {
	var options = options || {};
	pool.redis.acquire(function(err, rclient) {
		var timeout = (options.timeout ? parseInt(options.timeout) : 3600);
		var expire = Date.now() + timeout;
		var expired = Date.now() - timeout;
		if(err) {
			return callback(err, null);
		}
		rclient.get(key, function(err, result) {
			if(err) {
				pool.redis.release(rclient);
				return callback(err, null);
			}
			if(result) {
				if(result > expired) {
					pool.redis.release(rclient);
					return callback(null, null);
				}
				rclient.del(key);
			}
			rclient.setnx(key, expire, function(err, ok) {
				pool.redis.release(rclient);
				if(err) {
					return callback(err, null);
				}
				return callback(null, function() {
					release(key, noop);
				});
			});
		});
	});
};

var release = module.exports.release = function(key, callback) {
	pool.redis.acquire(function(err, rclient) {
		if(err) {
			return callback(err, null);
		}
		rclient.del(key, function(err, result) {
			pool.redis.release(rclient);
			if(err) {
				return callback(err, null);
			}
			return callback(null, result);
		});
	});
};

@@ -269,17 +269,16 @@
 is;%0A%09%09if
- 
 (self.re
@@ -530,17 +530,16 @@
  %7B%0A%09%09%09if
- 
 (err) %7B%0A
@@ -580,17 +580,16 @@
 %09%7D%0A%09%09%09if
- 
 (done ==
@@ -1033,46 +1033,8 @@
 ut;%0A
-%09%09var expired = Date.now() - timeout;%0A
 %09%09if
@@ -1198,24 +1198,25 @@
 null);%0A%09%09%09%7D%0A
+%0A
 %09%09%09if(result
@@ -1223,36 +1223,8 @@
 ) %7B%0A
-%09%09%09%09if(result %3E expired) %7B%0A%09
 %09%09%09%09
@@ -1252,17 +1252,16 @@
 lient);%0A
-%09
 %09%09%09%09retu
@@ -1289,41 +1289,14 @@
 l);%0A
-%09%09%09%09%7D%0A%09%09%09%09rclient.del(key);%0A
 %09%09%09%7D%0A
+%0A
 %09%09%09r
@@ -1337,24 +1337,39 @@
 (err, ok) %7B%0A
+%09%09%09%09if(err) %7B%0A%09
 %09%09%09%09pool.red
@@ -1389,34 +1389,277 @@
 lient);%0A%09%09%09%09
-if(err) %7B%0A
+%09return callback(err, null);%0A%09%09%09%09%7D%0A%09%09%09%09if(!ok) %7B%0A%09%09%09%09%09pool.redis.release(rclient);%0A%09%09%09%09%09return callback(null, null);%0A%09%09%09%09%7D%0A%09%09%09%09rclient.expire(key, timeout, function(err, ok) %7B%0A%09%09%09%09%09pool.redis.release(rclient);%0A%09%09%09%09%09if (err) %7B%0A%09%09%09%09%09%09release(key, noop);%0A%09
 %09%09%09%09%09return 
@@ -1679,26 +1679,28 @@
  null);%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09return c
@@ -1727,24 +1727,25 @@
 on() %7B%0A%09%09%09%09%09
+%09
 release(key,
@@ -1748,24 +1748,33 @@
 key, noop);%0A
+%09%09%09%09%09%7D);%0A
 %09%09%09%09%7D);%0A%09%09%09%7D
@@ -1786,25 +1786,24 @@
 %7D);%0A%09%7D);%0A%7D;%0A
-%0A
 var release 

Use expire instead of checking timeout