-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathBaseClient.ts
70 lines (62 loc) · 2.18 KB
/
BaseClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import path from 'path';
import protobuf, { Root } from 'protobufjs';
import { credentials, Client } from '@grpc/grpc-js';
import { ERROR_REASONS } from '.';
import { getGRPCService, formatAddress, getAuthInterceptor } from '../utils';
// pathes
const protoPath = path.resolve(__dirname, '../proto/proto/milvus.proto');
const schemaProtoPath = path.resolve(__dirname, '../proto/proto/schema.proto');
// Base Client
export class BaseClient {
// schema proto
schemaProto: Root;
// milvus proto
milvusProto: Root;
// client
grpcClient: Client;
/**
* Connect to milvus grpc client.
*
* @param address milvus address like: 127.0.0.1:19530
* @param ssl ssl connect or not, default is false
* @param username After created user in Milvus, username is required
* @param password After created user in Milvus, password is required
*
*/
constructor(
address: string,
ssl?: boolean,
username?: string,
password?: string
) {
// check if address is set
if (!address) {
throw new Error(ERROR_REASONS.MILVUS_ADDRESS_IS_REQUIRED);
}
// if we need to create auth interceptors
const needAuth = username !== undefined && password !== undefined;
// get Milvus GRPC service
const MilvusService = getGRPCService({
protoPath,
serviceName: 'milvus.proto.milvus.MilvusService', // the name of the Milvus service
});
// create interceptors
const interceptors = needAuth
? getAuthInterceptor(username, password)
: null;
// load proto
this.schemaProto = protobuf.loadSync(schemaProtoPath);
this.milvusProto = protobuf.loadSync(protoPath);
// create grpc client
this.grpcClient = new MilvusService(
formatAddress(address), // format the address
ssl ? credentials.createSsl() : credentials.createInsecure(), // create SSL or insecure credentials
{
interceptors: [interceptors],
// Milvus default max_receive_message_length is 100MB, but Milvus support change max_receive_message_length .
// So SDK should support max_receive_message_length unlimited.
'grpc.max_receive_message_length': -1, // set max_receive_message_length to unlimited
}
);
}
}