You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by hu...@apache.org on 2021/07/29 03:37:24 UTC

[dubbo-js] branch master updated: fixed: when zookeeper was closed, retry connect

This is an automated email from the ASF dual-hosted git repository.

hufeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-js.git


The following commit(s) were added to refs/heads/master by this push:
     new 4ae9924  fixed: when zookeeper was closed, retry connect
4ae9924 is described below

commit 4ae992449f1a5cf176d5cd17306b6265997ee251
Author: hufeng <fe...@gmail.com>
AuthorDate: Thu Jul 29 11:36:54 2021 +0800

    fixed: when zookeeper was closed, retry connect
---
 .npmrc                                                |  2 +-
 packages/dubbo-registry/__mocks__/zookeeper.ts        |  6 +-----
 .../src/__tests__/registry-zookeeper-test.ts          | 10 +++++++---
 packages/dubbo-registry/src/registry-zookeeper.ts     | 19 ++++++++++++-------
 4 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/.npmrc b/.npmrc
index a45bed8..ae44462 100644
--- a/.npmrc
+++ b/.npmrc
@@ -1 +1 @@
-registry=https://r.cnpmjs.org/
+registry=https://registry.npm.taobao.org
diff --git a/packages/dubbo-registry/__mocks__/zookeeper.ts b/packages/dubbo-registry/__mocks__/zookeeper.ts
index 3706dba..c6dc788 100644
--- a/packages/dubbo-registry/__mocks__/zookeeper.ts
+++ b/packages/dubbo-registry/__mocks__/zookeeper.ts
@@ -55,10 +55,6 @@ export default class ZookeeperMock extends EventEmitter {
     cb(null)
   }
 
-  mockConnectErr() {
-    this.isConnectErr = true
-  }
-
   create(
     path: string,
     data: string | Buffer,
@@ -74,7 +70,7 @@ export default class ZookeeperMock extends EventEmitter {
   }
 
   exists(path: string) {
-    return Promise.reject(new Error(`node was not exists`))
+    return Promise.reject(new Error(`${path} was not exists`))
   }
 
   w_get_children(servicePath: string) {
diff --git a/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts b/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts
index cacdb29..c3ac9e9 100644
--- a/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts
+++ b/packages/dubbo-registry/src/__tests__/registry-zookeeper-test.ts
@@ -35,7 +35,7 @@ describe('test zookeeper registry', () => {
     })
     expect(zk.getProps()).toEqual({
       connect: 'localhost:2181',
-      timeout: 5000,
+      timeout: 40000,
       debug_level: Zookeeper.constants.ZOO_LOG_LEVEL_WARN,
       host_order_deterministic: false,
       zkRootPath: '/dubbo'
@@ -54,7 +54,7 @@ describe('test zookeeper registry', () => {
 
     expect(zk.getProps()).toEqual({
       connect: 'localhost:2181',
-      timeout: 5000,
+      timeout: 40000,
       debug_level: Zookeeper.constants.ZOO_LOG_LEVEL_WARN,
       host_order_deterministic: false,
       zkRootPath: '/test/com.demo.dubbo'
@@ -103,6 +103,7 @@ describe('test zookeeper registry', () => {
       connect: 'localhost:2181'
     })
 
+    await zk.ready()
     await zk.registerServices(services)
 
     zk.close()
@@ -126,8 +127,11 @@ describe('test zookeeper registry', () => {
       onData(data) {
         expect(data).toMatchSnapshot()
       },
-      onError(err) {}
+      onError(err) {
+        expect(err).toMatchSnapshot()
+      }
     })
+    await zk.ready()
     await zk.registerConsumers(services)
     zk.close()
   })
diff --git a/packages/dubbo-registry/src/registry-zookeeper.ts b/packages/dubbo-registry/src/registry-zookeeper.ts
index b5709e6..e700169 100644
--- a/packages/dubbo-registry/src/registry-zookeeper.ts
+++ b/packages/dubbo-registry/src/registry-zookeeper.ts
@@ -32,7 +32,8 @@ const dlog = debug('dubbo:zookeeper~')
 
 export class ZookeeperRegistry
   extends BaseRegistry
-  implements IRegistry<Zookeeper> {
+  implements IRegistry<Zookeeper>
+{
   private readonly props: IZkClientConfig
   private client: Zookeeper
   private timeout: Timeout
@@ -44,7 +45,7 @@ export class ZookeeperRegistry
   constructor(props: IZkClientConfig) {
     super()
     dlog(`init zookeeper with %O`, props)
-    this.checkProps(props)
+    ZookeeperRegistry.checkProps(props)
     this.props = props
 
     this.props.zkRootPath = this.props.zkRootPath || DUBBO_ZK_ROOT_PATH
@@ -56,6 +57,7 @@ export class ZookeeperRegistry
     })
 
     this.timeout = new Timeout({
+      maxTimeout: this.props.timeout || 40 * 1000,
       onTimeout: () => {
         this.reject(
           new Error(`zookeeper connect ${this.props.connect} timeout`)
@@ -67,20 +69,19 @@ export class ZookeeperRegistry
   }
 
   // ~~~~~~~~~~~~~~~~ private ~~~~~~~~~~~~~~~~~~~~~~~~~~
-  private checkProps(props: IZkClientConfig) {
+  private static checkProps(props: IZkClientConfig) {
     if (!props.connect) {
       throw new Error(`Please specify zookeeper connect url`)
     }
   }
 
   private init() {
-    // cache this.client
     if (this.client) {
-      return this.client
+      return
     }
 
     // set default props value
-    this.props.timeout = this.props.timeout || 5000
+    this.props.timeout = this.props.timeout || 40 * 1000
     this.props.debug_level =
       this.props.debug_level || Zookeeper.constants.ZOO_LOG_LEVEL_WARN
     this.props.host_order_deterministic =
@@ -106,6 +107,8 @@ export class ZookeeperRegistry
     this.client.on('close', () => {
       dlog(`zookeeper closed`)
       this.emitErr(new Error(`Zookeeper was closed`))
+      this.close()
+      this.init()
     })
 
     this.client.on('error', (err) => {
@@ -115,7 +118,7 @@ export class ZookeeperRegistry
     })
 
     process.nextTick(() => {
-      this.client.init(this.props)
+      this.client.init({})
     })
   }
 
@@ -241,7 +244,9 @@ export class ZookeeperRegistry
 
   close(): void {
     this.timeout.clearTimeout()
+    this.client?.removeAllListeners()
     this.client?.close()
+    this.client = null
   }
 
   getClient() {