Skip to content

Commit

Permalink
feat: Add web sdk configurations (#518)
Browse files Browse the repository at this point in the history
* feat: Add web sdk configurations

Move transport-strategy and grpc-configuration out of core since it
contains options that are only useful for the node sdk.

Create web versions of transport strategy and grpc configuration.

Create web version of configurations.

Plumb deadline millis into the web sdk.
  • Loading branch information
nand4011 authored May 19, 2023
1 parent c7b16dd commit 8508af0
Show file tree
Hide file tree
Showing 20 changed files with 537 additions and 261 deletions.
2 changes: 1 addition & 1 deletion packages/client-sdk-nodejs/src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {RetryStrategy} from './retry/retry-strategy';
import {Middleware} from './middleware/middleware';
import {MomentoLoggerFactory} from '../';
import {TransportStrategy} from '../config/transport/transport-strategy';
import {TransportStrategy} from './transport';

export interface ConfigurationProps {
/**
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 @@ -11,7 +11,7 @@ import {
StaticGrpcConfiguration,
StaticTransportStrategy,
TransportStrategy,
} from '../config/transport';
} from './transport';

// 4 minutes. We want to remain comfortably underneath the idle timeout for AWS NLB, which is 350s.
const defaultMaxIdleMillis = 4 * 60 * 1_000;
Expand Down
3 changes: 2 additions & 1 deletion packages/client-sdk-nodejs/src/config/transport/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '@gomomento/sdk-core/dist/src/config/transport';
export * from './grpc-configuration';
export * from './transport-strategy';
130 changes: 129 additions & 1 deletion packages/client-sdk-nodejs/src/config/transport/transport-strategy.ts
Original file line number Diff line number Diff line change
@@ -1 +1,129 @@
export * from '@gomomento/sdk-core/dist/src/config/transport/transport-strategy';
import {GrpcConfiguration, GrpcConfigurationProps} from './grpc-configuration';

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

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

/**
* Copy constructor to update the client-side timeout
* @param {number} clientTimeoutMillis
* @returns {TransportStrategy} a new TransportStrategy with the specified client timeout
*/
withClientTimeoutMillis(clientTimeoutMillis: number): TransportStrategy;

/**
* The maximum duration for which a connection may remain idle before being replaced. This
* setting can be used to force re-connection of a client if it has been idle for too long.
* In environments such as AWS lambda, if the lambda is suspended for too long the connection
* may be closed by the load balancer, resulting in an error on the subsequent request. If
* this setting is set to a duration less than the load balancer timeout, we can ensure that
* the connection will be refreshed to avoid errors.
* @returns {number}
*/
getMaxIdleMillis(): number;

/**
* Copy constructor to update the max idle connection timeout. (See {getMaxIdleMillis}.)
* @param {number} maxIdleMillis
* @returns {TransportStrategy} a new TransportStrategy with the specified max idle connection timeout.
*/
withMaxIdleMillis(maxIdleMillis: number): TransportStrategy;
}

export interface TransportStrategyProps {
/**
* low-level gRPC settings for communication with the Momento server
*/
grpcConfiguration: GrpcConfiguration;
/**
* The maximum duration for which a connection may remain idle before being replaced. This
* setting can be used to force re-connection of a client if it has been idle for too long.
* In environments such as AWS lambda, if the lambda is suspended for too long the connection
* may be closed by the load balancer, resulting in an error on the subsequent request. If
* this setting is set to a duration less than the load balancer timeout, we can ensure that
* the connection will be refreshed to avoid errors.
* @returns {number}
*/
maxIdleMillis: number;
}

export class StaticGrpcConfiguration implements GrpcConfiguration {
private readonly deadlineMillis: number;
private readonly maxSessionMemoryMb: number;
constructor(props: GrpcConfigurationProps) {
this.deadlineMillis = props.deadlineMillis;
this.maxSessionMemoryMb = props.maxSessionMemoryMb;
}

getDeadlineMillis(): number {
return this.deadlineMillis;
}

getMaxSessionMemoryMb(): number {
return this.maxSessionMemoryMb;
}

withDeadlineMillis(deadlineMillis: number): StaticGrpcConfiguration {
return new StaticGrpcConfiguration({
deadlineMillis: deadlineMillis,
maxSessionMemoryMb: this.maxSessionMemoryMb,
});
}

withMaxSessionMemoryMb(maxSessionMemoryMb: number): StaticGrpcConfiguration {
return new StaticGrpcConfiguration({
deadlineMillis: this.deadlineMillis,
maxSessionMemoryMb: maxSessionMemoryMb,
});
}
}

export class StaticTransportStrategy implements TransportStrategy {
private readonly grpcConfig: GrpcConfiguration;
private readonly maxIdleMillis: number;

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

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

withGrpcConfig(grpcConfig: GrpcConfiguration): StaticTransportStrategy {
return new StaticTransportStrategy({
grpcConfiguration: grpcConfig,
maxIdleMillis: this.maxIdleMillis,
});
}

getMaxIdleMillis(): number {
return this.maxIdleMillis;
}

withMaxIdleMillis(maxIdleMillis: number): TransportStrategy {
return new StaticTransportStrategy({
grpcConfiguration: this.grpcConfig,
maxIdleMillis: maxIdleMillis,
});
}

withClientTimeoutMillis(clientTimeout: number): StaticTransportStrategy {
return new StaticTransportStrategy({
grpcConfiguration: this.grpcConfig.withDeadlineMillis(clientTimeout),
maxIdleMillis: this.maxIdleMillis,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
StaticGrpcConfiguration,
StaticTransportStrategy,
} from '../../../../src/config/transport/transport-strategy';
} from '../../../../src';

describe('StaticGrpcConfiguration', () => {
const testDeadlineMillis = 90210;
Expand Down
22 changes: 22 additions & 0 deletions packages/client-sdk-web/src/cache-client-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {CredentialProvider} from '.';
import {Configuration} from './config/configuration';

export interface CacheClientProps {
/**
* Configuration settings for the cache client
*/
configuration: Configuration;
/**
* controls how the client will get authentication information for connecting to the Momento service
*/
credentialProvider: CredentialProvider;
/**
* the default time to live of object inside of cache, in seconds
*/
defaultTtlSeconds: number;
}

/**
* @deprecated use {CacheClientProps} instead
*/
export type SimpleCacheClientProps = CacheClientProps;
36 changes: 4 additions & 32 deletions packages/client-sdk-web/src/cache-client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import {ControlClient} from './internal/control-client';
import {DataClient} from './internal/data-client';
import {PingClient} from './internal/ping-client';
import {
CredentialProvider,
NoopMomentoLoggerFactory,
} from '@gomomento/sdk-core';
import {
AbstractCacheClient,
IControlClient,
ICacheClient,
IDataClient,
IPingClient,
} from '@gomomento/sdk-core/dist/src/internal/clients';

export interface CacheClientProps {
credentialProvider: CredentialProvider;
}
import {CacheClientProps} from './cache-client-props';

export class CacheClient extends AbstractCacheClient implements ICacheClient {
constructor(props: CacheClientProps) {
Expand All @@ -28,43 +21,22 @@ export class CacheClient extends AbstractCacheClient implements ICacheClient {

function createControlClient(props: CacheClientProps): IControlClient {
return new ControlClient({
// TODO
// TODO
// TODO these shouldn't be hard-coded
// TODO
// TODO
configuration: {
getLoggerFactory: () => new NoopMomentoLoggerFactory(),
},
configuration: props.configuration,
credentialProvider: props.credentialProvider,
});
}

function createDataClient(props: CacheClientProps): IDataClient {
return new DataClient({
// TODO
// TODO
// TODO these shouldn't be hard-coded
// TODO
// TODO
configuration: {
getLoggerFactory: () => new NoopMomentoLoggerFactory(),
},
configuration: props.configuration,
credentialProvider: props.credentialProvider,
defaultTtlSeconds: 60,
});
}

function createPingClient(props: CacheClientProps): IPingClient {
return new PingClient({
// TODO
// TODO
// TODO these shouldn't be hard-coded
// TODO
// TODO
endpoint: props.credentialProvider.getCacheEndpoint(),
configuration: {
getLoggerFactory: () => new NoopMomentoLoggerFactory(),
},
configuration: props.configuration,
});
}
73 changes: 53 additions & 20 deletions packages/client-sdk-web/src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {MomentoLoggerFactory} from '@gomomento/sdk-core';
import {TransportStrategy} from './transport';

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

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

/**
Expand All @@ -25,22 +25,55 @@ export interface Configuration {
*/
getLoggerFactory(): MomentoLoggerFactory;

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

// /**
// * Copy constructor for overriding TransportStrategy
// * @param {TransportStrategy} transportStrategy
// * @returns {Configuration} a new Configuration object with the specified TransportStrategy
// */
// withTransportStrategy(transportStrategy: TransportStrategy): Configuration;

// /**
// * Convenience copy constructor that updates the client-side timeout setting in the TransportStrategy
// * @param {number} clientTimeoutMillis
// * @returns {Configuration} a new Configuration object with its TransportStrategy updated to use the specified client timeout
// */
// withClientTimeoutMillis(clientTimeoutMillis: number): Configuration;
/**
* @returns {TransportStrategy} the current configuration options for wire interactions with the Momento service
*/
getTransportStrategy(): TransportStrategy;

/**
* Copy constructor for overriding TransportStrategy
* @param {TransportStrategy} transportStrategy
* @returns {Configuration} a new Configuration object with the specified TransportStrategy
*/
withTransportStrategy(transportStrategy: TransportStrategy): Configuration;

/**
* Convenience copy constructor that updates the client-side timeout setting in the TransportStrategy
* @param {number} clientTimeoutMillis
* @returns {Configuration} a new Configuration object with its TransportStrategy updated to use the specified client timeout
*/
withClientTimeoutMillis(clientTimeoutMillis: number): Configuration;
}

export class CacheConfiguration implements Configuration {
private readonly loggerFactory: MomentoLoggerFactory;
private readonly transportStrategy: TransportStrategy;

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

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

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

withTransportStrategy(transportStrategy: TransportStrategy): Configuration {
return new CacheConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: transportStrategy,
});
}

withClientTimeoutMillis(clientTimeout: number): Configuration {
return new CacheConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy:
this.transportStrategy.withClientTimeoutMillis(clientTimeout),
});
}
}
Loading

0 comments on commit 8508af0

Please sign in to comment.