Skip to content

Commit bc2a112

Browse files
zhaohai666zh378814
andauthored
Client configuration add namespace (#750)
* clientConfiguration add namespace * update beta version * add namespace check * add namespace * add namespace * update version * Remove empty values from settings * update format and single quotes * update format * update version * remove codecov * Change namespace to required * Change namespace to required * Change namespace to required * test adds default parameters --------- Co-authored-by: zh378814 <wb-zh378814@alibaba-inc.com>
1 parent 5790fc4 commit bc2a112

13 files changed

Lines changed: 47 additions & 63 deletions

File tree

.github/workflows/nodejs_coverage.yml

Lines changed: 0 additions & 46 deletions
This file was deleted.

nodejs/examples/Producer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Producer } from '..';
1919

2020
const producer = new Producer({
2121
endpoints: '127.0.0.1:8081',
22+
namespace: ''
2223
});
2324
await producer.startup();
2425

nodejs/examples/SimpleConsumer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { SimpleConsumer } from '..';
2020
const simpleConsumer = new SimpleConsumer({
2121
consumerGroup: 'nodejs-demo-group',
2222
endpoints: '127.0.0.1:8081',
23+
namespace: '',
2324
subscriptions: new Map().set('TopicTest', 'nodejs-demo'),
2425
});
2526
await simpleConsumer.startup();

nodejs/src/client/BaseClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface BaseClientOptions {
5757
* - example.com:8443
5858
*/
5959
endpoints: string;
60+
namespace: string;
6061
sessionCredentials?: SessionCredentials;
6162
requestTimeout?: number;
6263
logger?: ILogger;
@@ -76,6 +77,7 @@ export abstract class BaseClient {
7677
readonly clientType = ClientType.CLIENT_TYPE_UNSPECIFIED;
7778
readonly sslEnabled: boolean;
7879
readonly #sessionCredentials?: SessionCredentials;
80+
readonly namespace: string;
7981
protected readonly endpoints: Endpoints;
8082
protected readonly isolated = new Map<string, Endpoints>();
8183
protected readonly requestTimeout: number;
@@ -92,6 +94,7 @@ export abstract class BaseClient {
9294
this.logger = options.logger ?? getDefaultLogger();
9395
this.sslEnabled = options.sslEnabled === true;
9496
this.endpoints = new Endpoints(options.endpoints);
97+
this.namespace = options.namespace;
9598
this.#sessionCredentials = options.sessionCredentials;
9699
// https://rocketmq.apache.org/docs/introduction/03limits/
97100
// Default request timeout is 3000ms
@@ -288,6 +291,9 @@ export abstract class BaseClient {
288291
metadata.set('x-mq-language', 'HTTP');
289292
// version of client
290293
metadata.set('x-mq-client-version', UserAgent.INSTANCE.version);
294+
if (this.namespace) {
295+
metadata.set('x-mq-namespace', this.namespace);
296+
}
291297
if (this.#sessionCredentials) {
292298
if (this.#sessionCredentials.securityToken) {
293299
metadata.set('x-mq-session-token', this.#sessionCredentials.securityToken);

nodejs/src/client/Settings.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ import { Endpoints } from '../route/Endpoints';
2020
import { RetryPolicy } from '../retry';
2121

2222
export abstract class Settings {
23+
protected readonly namespace: string;
2324
protected readonly clientId: string;
2425
protected readonly clientType: ClientType;
2526
protected readonly accessPoint: Endpoints;
2627
protected retryPolicy?: RetryPolicy;
2728
protected readonly requestTimeout: number;
2829

29-
constructor(clientId: string, clientType: ClientType, accessPoint: Endpoints, requestTimeout: number,
30-
retryPolicy?: RetryPolicy) {
30+
constructor(namespace: string, clientId: string, clientType: ClientType, accessPoint: Endpoints, requestTimeout: number, retryPolicy?: RetryPolicy) {
3131
this.clientId = clientId;
32+
this.namespace = namespace;
3233
this.clientType = clientType;
3334
this.accessPoint = accessPoint;
3435
this.retryPolicy = retryPolicy;

nodejs/src/consumer/SimpleConsumer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class SimpleConsumer extends Consumer {
6060
}
6161
}
6262
this.#awaitDuration = options.awaitDuration ?? 30000;
63-
this.#simpleSubscriptionSettings = new SimpleSubscriptionSettings(this.clientId, this.endpoints,
63+
this.#simpleSubscriptionSettings = new SimpleSubscriptionSettings(options.namespace, this.clientId, this.endpoints,
6464
this.consumerGroup, this.requestTimeout, this.#awaitDuration, this.#subscriptionExpressions);
6565
}
6666

nodejs/src/consumer/SimpleSubscriptionSettings.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ export class SimpleSubscriptionSettings extends Settings {
3030
readonly group: string;
3131
readonly subscriptionExpressions: Map<string, FilterExpression>;
3232

33-
constructor(clientId: string, accessPoint: Endpoints, group: string,
34-
requestTimeout: number, longPollingTimeout: number, subscriptionExpressions: Map<string, FilterExpression>) {
35-
super(clientId, ClientType.SIMPLE_CONSUMER, accessPoint, requestTimeout);
33+
constructor(namespace: string, clientId: string, accessPoint: Endpoints, group: string, requestTimeout: number, longPollingTimeout: number, subscriptionExpressions: Map<string, FilterExpression>) {
34+
super(namespace, clientId, ClientType.SIMPLE_CONSUMER, accessPoint, requestTimeout);
3635
this.longPollingTimeout = longPollingTimeout;
3736
this.group = group;
3837
this.subscriptionExpressions = subscriptionExpressions;

nodejs/src/message/PublishingMessage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class PublishingMessage extends Message {
6868
* This method should be invoked before each message sending, because the born time is reset before each
6969
* invocation, which means that it should not be invoked ahead of time.
7070
*/
71-
toProtobuf(mq: MessageQueue) {
71+
toProtobuf(namespace: string, mq: MessageQueue) {
7272
const systemProperties = new SystemProperties()
7373
.setKeysList(this.keys)
7474
.setMessageId(this.messageId)
@@ -87,8 +87,10 @@ export class PublishingMessage extends Message {
8787
systemProperties.setMessageGroup(this.messageGroup);
8888
}
8989

90+
const resource = createResource(this.topic);
91+
resource.setResourceNamespace(namespace);
9092
const message = new MessagePB()
91-
.setTopic(createResource(this.topic))
93+
.setTopic(resource)
9294
.setBody(this.body)
9395
.setSystemProperties(systemProperties);
9496
if (this.properties) {

nodejs/src/producer/Producer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class Producer extends BaseClient {
6666
// https://rocketmq.apache.org/docs/introduction/03limits/
6767
// Default max number of message sending retries is 3
6868
const retryPolicy = ExponentialBackoffRetryPolicy.immediatelyRetryPolicy(options.maxAttempts ?? 3);
69-
this.#publishingSettings = new PublishingSettings(this.clientId, this.endpoints, retryPolicy,
69+
this.#publishingSettings = new PublishingSettings(options.namespace, this.clientId, this.endpoints, retryPolicy,
7070
this.requestTimeout, this.topics);
7171
this.#checker = options.checker;
7272
}
@@ -85,7 +85,7 @@ export class Producer extends BaseClient {
8585
const request = new EndTransactionRequest()
8686
.setMessageId(messageId)
8787
.setTransactionId(transactionId)
88-
.setTopic(createResource(message.topic))
88+
.setTopic(createResource(message.topic).setResourceNamespace(this.namespace))
8989
.setResolution(resolution);
9090
const response = await this.rpcClientManager.endTransaction(endpoints, request, this.requestTimeout);
9191
StatusChecker.check(response.getStatus()?.toObject());
@@ -187,7 +187,11 @@ export class Producer extends BaseClient {
187187
#wrapSendMessageRequest(pubMessages: PublishingMessage[], mq: MessageQueue) {
188188
const request = new SendMessageRequest();
189189
for (const pubMessage of pubMessages) {
190-
request.addMessages(pubMessage.toProtobuf(mq));
190+
if (this.namespace) {
191+
request.addMessages(pubMessage.toProtobuf(this.namespace, mq));
192+
} else {
193+
request.addMessages(pubMessage.toProtobuf('', mq));
194+
}
191195
}
192196
return request;
193197
}

nodejs/src/producer/PublishingSettings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ export class PublishingSettings extends Settings {
3535
#maxBodySizeBytes = 4 * 1024 * 1024;
3636
#validateMessageType = true;
3737

38-
constructor(clientId: string, accessPoint: Endpoints, retryPolicy: ExponentialBackoffRetryPolicy,
39-
requestTimeout: number, topics: Set<string>) {
40-
super(clientId, ClientType.PRODUCER, accessPoint, requestTimeout, retryPolicy);
38+
constructor(namespace: string, clientId: string, accessPoint: Endpoints, retryPolicy: ExponentialBackoffRetryPolicy, requestTimeout: number, topics: Set<string>) {
39+
super(namespace, clientId, ClientType.PRODUCER, accessPoint, requestTimeout, retryPolicy);
4140
this.#topics = topics;
4241
}
4342

@@ -54,6 +53,7 @@ export class PublishingSettings extends Settings {
5453
.setValidateMessageType(this.#validateMessageType);
5554
for (const topic of this.#topics) {
5655
publishing.addTopics().setName(topic);
56+
publishing.addTopics().setResourceNamespace(this.namespace);
5757
}
5858
return new SettingsPB()
5959
.setClientType(this.clientType)

0 commit comments

Comments
 (0)