Skip to content

Commit

Permalink
how to check connection (#341)
Browse files Browse the repository at this point in the history
* updated getVersio() and checkHealth() to await connectPromise for error handling

* add documentation on how to check the connection
  • Loading branch information
ehooi authored Jul 17, 2024
1 parent 151ba0d commit a2f6d71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ const password = 'your-milvus-password'; // optional password

// connect to milvus
const client = new MilvusClient({ address, username, password });
// wait until connecting finished
await client.connectPromise;
```

### Create a collection
Expand Down
4 changes: 4 additions & 0 deletions milvus/grpc/GrpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export class GRPCClient extends User {
* This method returns a Promise that resolves with a `GetVersionResponse` object.
*/
async getVersion(): Promise<GetVersionResponse> {
// wait until connecting finished
await this.connectPromise;
return await promisify(this.channelPool, 'GetVersion', {}, this.timeout);
}

Expand All @@ -252,6 +254,8 @@ export class GRPCClient extends User {
* This method returns a Promise that resolves with a `CheckHealthResponse` object.
*/
async checkHealth(): Promise<CheckHealthResponse> {
// wait until connecting finished
await this.connectPromise;
return await promisify(this.channelPool, 'CheckHealth', {}, this.timeout);
}
}
8 changes: 8 additions & 0 deletions test/grpc/MilvusClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ describe(`Milvus client`, () => {
expect(Array.isArray(res.reasons)).toBe(true);
});

it(`Should throw when server is unavailable`, async () => {
const m10 = new MilvusClient({ address: IP.replace(':19530', ':19539') });
await expect(m10.connectPromise).rejects.toThrow('UNAVAILABLE');
await expect(m10.getVersion()).rejects.toThrow('UNAVAILABLE');
await expect(m10.checkCompatibility()).rejects.toThrow('UNAVAILABLE');
await expect(m10.checkHealth()).rejects.toThrow('UNAVAILABLE');
});

it(`Expect close connection success`, async () => {
expect(milvusClient.channelPool.size).toBeGreaterThan(0);

Expand Down

0 comments on commit a2f6d71

Please sign in to comment.