Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reconnectStrategy Not in effect #2274

Open
mayuehit opened this issue Sep 23, 2022 · 3 comments
Open

reconnectStrategy Not in effect #2274

mayuehit opened this issue Sep 23, 2022 · 3 comments
Labels

Comments

@mayuehit
Copy link

mayuehit commented Sep 23, 2022

Environment:

  • Node.js Version: v16.15.1
  • Redis Server Version: unknown
  • Node Redis Version: [email protected]
  • Platform: Ubuntu 20.04.3
 try {
      const client = createClient(
        {
          socket: {
            // deal ENOENT and ECONNREFUSED
            path: this.unixDomainSocketPath, reconnectStrategy: (retries) => {
              return 10000;
            }
          }
        }
      );
      client.on('error',
        (err) => {
          logger.error(`LoopingPut ${err}. Action:${action},ResPut:${resPut},TimeOut:${this.timeOut}`);
        }
      );
      await client.connect();
      await client.sendCommand([action, this.contentType, resPut])
    } catch (e) {
      logger.error(`Catch ${e}. action:${action},resPut:${resPut},timeOut:${this.timeOut}`);
    }

First when I run the code above, when happened error ENOENT,ECONNREFUSED, reconnectStrategy takes effect.It reconnect every ten seconds .But when happened ECONNRESET, the reconnectStrategy not in effect. Errors all over the screen every second,It seems it reconnect every second.

Second I found ENOENT or ECONNREFUSED means connect failed,so it run to reconnectStrategy. ECONNRESET means connect success,but sendCommand error,so it won't go to reconnectStrategy . It seems that it is sendCommand which retry every second.

Then I solve the question , you could use the following code to solve the problem.The point is that you must run client.disconnect() in .catch to prevent sendCommand retrying all the time.

try {
      const client = createClient(
        {
          socket: {
            // deal ENOENT and ECONNREFUSED
            path: this.unixDomainSocketPath, reconnectStrategy: (retries) => {
              return 10000;
            }
          }
        }
      );
      client.on('error',
        (err) => {
          logger.error(`LoopingPut ${err}. Action:${action},ResPut:${resPut},TimeOut:${this.timeOut}`);
        }
      );
      await client.connect();
      client.sendCommand([action, this.contentType, resPut])
        .then(() => {client.disconnect().then().catch();}
        )
    	// deal ECONNRESET you must client.disconnect() to prevent sendCommand retry all the time
        .catch(() => { client.disconnect().then().catch();});
    } catch (e) {
      logger.error(`Catch ${e}. action:${action},resPut:${resPut},timeOut:${this.timeOut}`);
    }

Finally To run the code above,you could resolve the problem of retrying all the time.But I want to retry in my controll,like retry every 10s,so I do this through the following code.

 public async Put(action: string, resPut: string) {
    try {
      const client = createClient(
        {
          socket: {
            // deal ENOENT and ECONNREFUSED
            path: this.unixDomainSocketPath, reconnectStrategy: (retries) => {
              return 10000;
            }
          }
        }
      );
      client.on('error',
        (err) => {
          logger.error(`put error`);
        }
      );
      await client.connect();
      client.sendCommand([action, this.contentType, resPut])
        .then((result) => {
          client.disconnect().then().catch();
        }
        )
        // deal ECONNRESET
        .catch(() => { 
          client.disconnect().then().catch();
          // my custom retry strategy
          setTimeout(() => {
            this.Put(action, resPut);
          }, 10000);
        });
    } catch (e) {
      logger.error(`xxx`);
    }
  }
}
@xtianus79
Copy link

I think I have this same issue

@mayuehit
Copy link
Author

mayuehit commented Oct 5, 2022 via email

@mayuehit
Copy link
Author

mayuehit commented Oct 8, 2022

I think I have this same issue

you could solve this problem like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants