Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
2 parents 594d267 + 2b02a5e commit 1c83f79
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 45 deletions.
24 changes: 22 additions & 2 deletions packages/client-sdk-nodejs/src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {RetryStrategy} from './retry/retry-strategy';
import {Middleware} from './middleware/middleware';
import {MomentoLoggerFactory} from '../';
import {TransportStrategy} from './transport';
import {MomentoLoggerFactory, TransportStrategy} from '../';
import {ReadConcern} from '@gomomento/sdk-core';
import {CompressionStrategy} from './compression/compression';

Expand Down Expand Up @@ -72,6 +71,17 @@ export interface Configuration {
*/
withTransportStrategy(transportStrategy: TransportStrategy): Configuration;

/**
* Shorthand copy constructor for overriding TransportStrategy.GrpcStrategy.NumClients. This will
* allow you to control the number of TCP connections that the client will open to the server. Usually
* you should stick with the default value from your pre-built configuration, but it can be valuable
* to increase this value in order to ensure more evenly distributed load on Momento servers.
*
* @param {number} numConnections
* @returns {Configuration} a new Configuration object with the updated TransportStrategy
*/
withNumConnections(numConnections: number): Configuration;

/**
* @returns {Middleware[]} the middleware functions that will wrap each request
*/
Expand Down Expand Up @@ -199,6 +209,16 @@ export class CacheConfiguration implements Configuration {
});
}

withNumConnections(numConnections: number): Configuration {
return this.withTransportStrategy(
this.getTransportStrategy().withGrpcConfig(
this.getTransportStrategy()
.getGrpcConfig()
.withNumClients(numConnections)
)
);
}

getMiddlewares(): Middleware[] {
return this.middlewares;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client-sdk-nodejs/src/config/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
StaticGrpcConfiguration,
StaticTransportStrategy,
TransportStrategy,
} from './transport';
} from '..';
import {DefaultMomentoLoggerFactory} from './logging/default-momento-logger';

// 4 minutes. We want to remain comfortably underneath the idle timeout for AWS NLB, which is 350s.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {MomentoLoggerFactory} from '../';
import {TransportStrategy} from './transport';
import {MomentoLoggerFactory, TransportStrategy} from '../';
import {Middleware} from './middleware/middleware';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
StaticGrpcConfiguration,
StaticTransportStrategy,
TransportStrategy,
} from './transport';
} from '../';
import {DefaultMomentoLoggerFactory} from './logging/default-momento-logger';
import {Middleware} from './middleware/middleware';

Expand Down
49 changes: 49 additions & 0 deletions packages/client-sdk-nodejs/src/config/topic-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {MomentoLoggerFactory} from '@gomomento/sdk-core';
import {TopicTransportStrategy} from './transport/topics';

export interface TopicConfigurationProps {
/**
* Configures logging verbosity and format
*/
loggerFactory: MomentoLoggerFactory;

/**
* Configures low-level options for network interactions with the Momento service
*/
transportStrategy: TopicTransportStrategy;

/**
* Configures whether the client should return a Momento Error object or throw an exception when an error occurs.
*/
Expand All @@ -24,6 +30,22 @@ export interface TopicConfiguration {
*/
getLoggerFactory(): MomentoLoggerFactory;

/**
* @returns {TransportStrategy} the current configuration options for wire interactions with the Momento service
*/
getTransportStrategy(): TopicTransportStrategy;

/**
* Shorthand copy constructor for overriding TransportStrategy.GrpcStrategy.NumClients. This will
* allow you to control the number of TCP connections that the client will open to the server. Usually
* you should stick with the default value from your pre-built configuration, but it can be valuable
* to increase this value in order to ensure more evenly distributed load on Momento servers.
*
* @param {number} numConnections
* @returns {Configuration} a new Configuration object with the updated TransportStrategy
*/
withNumConnections(numConnections: number): TopicConfiguration;

/**
* @returns {boolean} Configures whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
Expand All @@ -43,24 +65,51 @@ export interface TopicConfiguration {

export class TopicClientConfiguration implements TopicConfiguration {
private readonly loggerFactory: MomentoLoggerFactory;
private readonly transportStrategy: TopicTransportStrategy;
private readonly throwOnErrors: boolean;

constructor(props: TopicConfigurationProps) {
this.loggerFactory = props.loggerFactory;
this.transportStrategy = props.transportStrategy;
this.throwOnErrors = props.throwOnErrors;
}

getLoggerFactory(): MomentoLoggerFactory {
return this.loggerFactory;
}

getTransportStrategy(): TopicTransportStrategy {
return this.transportStrategy;
}

withTransportStrategy(
transportStrategy: TopicTransportStrategy
): TopicConfiguration {
return new TopicClientConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: transportStrategy,
throwOnErrors: this.throwOnErrors,
});
}

withNumConnections(numConnections: number): TopicConfiguration {
return this.withTransportStrategy(
this.getTransportStrategy().withGrpcConfig(
this.getTransportStrategy()
.getGrpcConfig()
.withNumClients(numConnections)
)
);
}

getThrowOnErrors(): boolean {
return this.throwOnErrors;
}

withThrowOnErrors(throwOnErrors: boolean): TopicConfiguration {
return new TopicClientConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: this.transportStrategy,
throwOnErrors: throwOnErrors,
});
}
Expand Down
9 changes: 9 additions & 0 deletions packages/client-sdk-nodejs/src/config/topic-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
TopicClientConfiguration,
TopicConfiguration,
} from './topic-configuration';
import {
StaticTopicGrpcConfiguration,
StaticTopicTransportStrategy,
} from './transport/topics';

const defaultLoggerFactory: MomentoLoggerFactory =
new DefaultMomentoLoggerFactory();
Expand All @@ -25,6 +29,11 @@ export class Default extends TopicClientConfiguration {
): TopicConfiguration {
return new TopicClientConfiguration({
loggerFactory: loggerFactory,
transportStrategy: new StaticTopicTransportStrategy({
grpcConfiguration: new StaticTopicGrpcConfiguration({
numClients: 1,
}),
}),
throwOnErrors: false,
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface TopicGrpcConfigurationProps {
/**
* The number of internal clients a cache client will create to communicate with Momento. More of them allows
* more concurrent requests, at the cost of more open connections and the latency of setting up each client.
*/
numClients?: number;
}

/**
* Encapsulates gRPC configuration tunables.
* @export
* @interface TopicGrpcConfiguration
*/
export interface TopicGrpcConfiguration {
/**
* @returns {number} the number of internal clients a cache client will create to communicate with Momento. More of
* them will allow for more concurrent requests.
*/
getNumClients(): number;

/**
* Copy constructor for overriding the number of clients to create
* @param {number} numClients the number of internal clients to create
* @returns {GrpcConfiguration} a new GrpcConfiguration with the specified number of clients
*/
withNumClients(numClients: number): TopicGrpcConfiguration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './grpc-configuration';
export * from './transport-strategy';
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
TopicGrpcConfiguration,
TopicGrpcConfigurationProps,
} from './grpc-configuration';

export interface TopicTransportStrategy {
/**
* Configures the low-level gRPC settings for the Momento client's communication
* with the Momento server.
* @returns {TopicGrpcConfiguration}
*/
getGrpcConfig(): TopicGrpcConfiguration;

/**
* Copy constructor for overriding the gRPC configuration
* @param {TopicGrpcConfiguration} grpcConfig
* @returns {TopicTransportStrategy} a new TransportStrategy with the specified gRPC config.
*/
withGrpcConfig(grpcConfig: TopicGrpcConfiguration): TopicTransportStrategy;
}

export interface TopicTransportStrategyProps {
/**
* low-level gRPC settings for communication with the Momento server
*/
grpcConfiguration: TopicGrpcConfiguration;
}

export class StaticTopicGrpcConfiguration implements TopicGrpcConfiguration {
private readonly numClients: number;

constructor(props: TopicGrpcConfigurationProps) {
if (props.numClients !== undefined && props.numClients !== null) {
this.numClients = props.numClients;
} else {
this.numClients = 1;
}
}

getNumClients(): number {
return this.numClients;
}

withNumClients(numClients: number): TopicGrpcConfiguration {
return new StaticTopicGrpcConfiguration({
numClients: numClients,
});
}
}

export class StaticTopicTransportStrategy implements TopicTransportStrategy {
private readonly grpcConfig: TopicGrpcConfiguration;

constructor(props: TopicTransportStrategyProps) {
this.grpcConfig = props.grpcConfiguration;
}

getGrpcConfig(): TopicGrpcConfiguration {
return this.grpcConfig;
}

withGrpcConfig(
grpcConfig: TopicGrpcConfiguration
): StaticTopicTransportStrategy {
return new StaticTopicTransportStrategy({
grpcConfiguration: grpcConfig,
});
}
}
16 changes: 14 additions & 2 deletions packages/client-sdk-nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,24 @@ export {
StaticTransportStrategy,
TransportStrategy,
TransportStrategyProps,
} from './config/transport/transport-strategy';
} from './config/transport/cache/transport-strategy';

export {
GrpcConfiguration,
GrpcConfigurationProps,
} from './config/transport/grpc-configuration';
} from './config/transport/cache/grpc-configuration';

export {
StaticTopicGrpcConfiguration,
StaticTopicTransportStrategy,
TopicTransportStrategy,
TopicTransportStrategyProps,
} from './config/transport/topics/transport-strategy';

export {
TopicGrpcConfiguration,
TopicGrpcConfigurationProps,
} from './config/transport/topics/grpc-configuration';

export {
Middleware,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {GrpcConfiguration} from '../../config/transport';
import {GrpcConfiguration} from '../..';
import {ChannelOptions} from '@grpc/grpc-js';

// The default value for max_send_message_length is 4mb. We need to increase this to 5mb in order to
Expand Down
26 changes: 13 additions & 13 deletions packages/client-sdk-nodejs/src/topic-client.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import {AbstractTopicClient} from '@gomomento/sdk-core/dist/src/internal/clients/pubsub/AbstractTopicClient';
import {MomentoLogger, TopicConfiguration, TopicConfigurations} from '.';
import {TopicConfiguration, TopicConfigurations} from '.';
import {PubsubClient} from './internal/pubsub-client';
import {TopicClientProps} from './topic-client-props';
import {IPubsubClient} from '@gomomento/sdk-core/dist/src/internal/clients';
import {IWebhookClient} from '@gomomento/sdk-core/dist/src/internal/clients/pubsub/IWebhookClient';
import {WebhookClient} from './internal/webhook-client';
import {TopicClientPropsWithConfiguration} from './internal/topic-client-props-with-config';
import {range} from '@gomomento/sdk-core/dist/src/internal/utils';

/**
* Momento Topic Client.
*
* Publish and subscribe to topics.
*/
export class TopicClient extends AbstractTopicClient {
protected readonly logger: MomentoLogger;
protected readonly pubsubClient: IPubsubClient;
protected readonly webhookClient: IWebhookClient;

/**
* Creates an instance of TopicClient.
*/
constructor(props: TopicClientProps) {
super();

const configuration: TopicConfiguration =
props.configuration ?? getDefaultTopicClientConfiguration();
const propsWithConfiguration: TopicClientPropsWithConfiguration = {
...props,
configuration,
};

this.logger = configuration.getLoggerFactory().getLogger(this);
this.logger.debug('Creating Momento TopicClient');
const numClients = configuration
.getTransportStrategy()
.getGrpcConfig()
.getNumClients();

super(
configuration.getLoggerFactory().getLogger(TopicClient.name),
range(numClients).map(_ => new PubsubClient(propsWithConfiguration)),
new WebhookClient(propsWithConfiguration)
);

this.pubsubClient = new PubsubClient(propsWithConfiguration);
this.webhookClient = new WebhookClient(propsWithConfiguration);
this.logger.debug('Instantiated Momento TopicClient');
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {CacheConfiguration} from '../../../src/config/configuration';
import {FixedCountRetryStrategy} from '../../../src/config/retry/fixed-count-retry-strategy';
import {Configurations, DefaultMomentoLoggerFactory} from '../../../src';
import {Middleware} from '../../../src/config/middleware/middleware';
import {
Configurations,
CacheConfiguration,
FixedCountRetryStrategy,
DefaultMomentoLoggerFactory,
Middleware,
StaticGrpcConfiguration,
StaticTransportStrategy,
} from '../../../src/config/transport/transport-strategy';
} from '../../../src';
import {ReadConcern} from '@gomomento/sdk-core';

describe('configuration.ts', () => {
Expand Down
Loading

0 comments on commit 1c83f79

Please sign in to comment.