我试图使用redlock模块,但我面临一个无法通过的错误。
以下是我的红锁文件的外观:

var redis = require('redis');
var Redlock = require('redlock');
var logger = require('./logger.js');

var client;

module.exports.redisConnection = function(port,host){
    client = redis.createClient(port,host);

    client.on('connect',function(){
        logger.info("Redis default connection open to "+host+":"+port);
    });

    client.on('error',function(err){
        logger.info("Redis default connection error "+err);
        logger.info("Redis Path : "+host+":"+port);
    });

    process.on('SIGINT', function() {
        client.quit();
        logger.info("Redis default connection disconnected");
        process.exit(0);
    });
};

var redlock = new Redlock(
    [client],
    {
        driftFactor : 0.01,
        retryCount : 15,
        retryDelay : 200
    }
);

redlock.on('clientError', function(err) {
    logger.info("A Redis Error Has Occurred : "+err);
});

module.exports.lockRessource = function(ressource_id,callback){
    redlock.lock(ressource_id,2000,function(err,lock){
        if(err){
            callback(err,null);
        }
        else{
            callback(null,lock);
        }
    });
};

module.exports.unlockLock = function(lock,callback){

    lock.unlock(function(err){
        if(err){
            callback(true,null);
        }
        else{
            callback(null,true);
        }
    });
};

当我尝试从另一个模块调用lockressource函数时,会得到一个错误:
2016-04-14T16:28:55.020-信息:类型错误:无法读取未定义的属性“set”
应请求(/usr/app/node_modules/redlock/redlock.js:260:18)
at/usr/app/node_modules/redlock/redlock.js:314:12
在array.foreach(本机)
尝试时(/usr/app/node_modules/redlock/redlock.js:313:24)
at/usr/app/node_modules/redlock/redlock.js:318:10
按承诺执行(/usr/app/node-modules/redlock/node-modules/bluebird/js/release/debugability.js:272:9)
在promise._resolvefromexecutor(/usr/app/node_modules/redlock/node_modules/bluebird/js/release/promise.js:474:18)
在新的promise上(/usr/app/node_modules/redlock/node_modules/bluebird/js/release/promise.js:77:14)
在redlock.\u lock(/usr/app/node\u modules/redlock/redlock.js:249:9)
在redlock.lock(/usr/app/node_modules/redlock/redlock.js:111:14)
在object.module.exports.lockressource(/usr/app/redisdata.js:42:10)
你知道这是从哪里来的吗?看来我对那个模块没做什么疯狂的事。
谢谢!

最佳答案

分配客户端后初始化重锁。
你的红锁文件看起来像:

var redis = require('redis');
var Redlock = require('redlock');
var logger = require('./logger.js');

var client;
var redlock;

module.exports.redisConnection = function(port,host){
    client = redis.createClient(port,host);
    redlock = new Redlock(
        [client],
        {
            driftFactor : 0.01,
            retryCount : 15,
            retryDelay : 200
        }
    );

    client.on('connect',function(){
        logger.info("Redis default connection open to "+host+":"+port);
    });

    client.on('error',function(err){
        logger.info("Redis default connection error "+err);
        logger.info("Redis Path : "+host+":"+port);
    });

    redlock.on('clientError', function(err) {
        logger.info("A Redis Error Has Occurred : "+err);
    });

    process.on('SIGINT', function() {
        client.quit();
        logger.info("Redis default connection disconnected");
        process.exit(0);
    });
};

module.exports.lockRessource = function(ressource_id,callback){
    redlock.lock(ressource_id,2000,function(err,lock){
        if(err){
            callback(err,null);
        }
        else{
            callback(null,lock);
        }
    });
};

module.exports.unlockLock = function(lock,callback){

    lock.unlock(function(err){
        if(err){
            callback(true,null);
        }
        else{
            callback(null,true);
        }
    });
};

如果要使用该模块,请先调用redisconnection。
假设上面的文件名是“redlock.js”。
resource = require('./redlock.js')

resource.redisConnection(6379, "127.0.0.1")
resource.lockRessource("your resourceId", function(err, lock) { })

09-17 07:48