3

現在、ノード redis クライアントとしてhttps://github.com/mranney/node_redisを使用しています。

client.retry_delayデフォルトは 250ms に設定されています。

redis に接続しようとしましたが、接続が成功したら、redis サーバーを手動で停止して、動作するかどうかを確認client.retry_delayしました。しかし、私はそれが働いているのを見ませんでした。

次のログ メッセージは、 を使用して作成された redisClients のイベントとログに記録さreadyendますcreateClient

[2012-03-30 15:13:05.498] [INFO] Development - Node Application is running on port 8090
[2012-03-30 15:13:08.507] [INFO] Development - Connection Successfully Established to  '127.0.0.1' '6379'
[2012-03-30 15:16:33.886] [FATAL] Development - Connection Terminated to  '127.0.0.1' '6379'

サーバーが稼働状態に戻ったときに、成功メッセージ [ready イベントが発生しませんでした] が再び表示されませんでした。

何か不足していますか?リトライ定数はいつ使用されますか? ノードから障害が発生した後に redis サーバーが起動したかどうかを確認するための回避策はありますか?

4

3 に答える 3

11

これを再現できません。このコードを試して、redis サーバーを停止し、ログ出力を確認していただけますか?

var client = require('redis').createClient();

client.on('connect'     , log('connect'));
client.on('ready'       , log('ready'));
client.on('reconnecting', log('reconnecting'));
client.on('error'       , log('error'));
client.on('end'         , log('end'));

function log(type) {
    return function() {
        console.log(type, arguments);
    }
}
于 2012-03-30T10:34:50.990 に答える
2

回答 @ 2020 年 2 月

const redis = require('redis');
const log = (type, fn) => fn ? () => {
    console.log(`connection ${type}`);
} : console.log(`connection ${type}`);

// Option 1: One connection is enough per application
const client = redis.createClient('6379', "localhost", {
    retry_strategy: (options) => {
        const {error, total_retry_time, attempt} = options;
        if (error && error.code === "ECONNREFUSED") {
            log(error.code); // take actions or throw exception
        }
        if (total_retry_time > 1000 * 15) { //in ms i.e. 15 sec
            log('Retry time exhausted'); // take actions or throw exception
        }
        if (options.attempt > 10) {
            log('10 attempts done'); // take actions or throw exception
        }
        console.log("Attempting connection");
        // reconnect after
        return Math.min(options.attempt * 100, 3000); //in ms
    },
});

client.on('connect', log('connect', true));
client.on('ready', log('ready', true));
client.on('reconnecting', log('reconnecting', true));
client.on('error', log('error', true));
client.on('end', log('end', true));

完全な実行例については、 node-cheat のクローンを作成して実行しますnode connect-retry.js

于 2020-02-18T13:40:33.173 に答える