From fbf6d920a136e81ec20530abdb5b98f18f7d1756 Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Fri, 6 Dec 2024 15:29:44 -0800 Subject: [PATCH] chore: centralize default ttl and milliseconds conversion Previously the code had lots of ad hoc conversion: - deciding to use the provided ttl vs default - converting to milliseconds by multiplying by 1000 We centralize these calculations for maintainability and to reduce errors. --- .../src/internal/cache-control-client.ts | 4 +- .../src/internal/cache-data-client.ts | 71 ++++++++++++------ .../src/internal/internal-auth-client.ts | 4 +- .../src/internal/ping-client.ts | 4 +- .../src/internal/pubsub-client.ts | 4 +- .../src/internal/storage-control-client.ts | 4 +- .../src/internal/webhook-client.ts | 4 +- .../src/internal/cache-data-client.ts | 72 ++++++++++++------- .../src/internal/pubsub-client.ts | 4 +- packages/core/src/index.ts | 2 + packages/core/src/utils/collection-ttl.ts | 5 +- packages/core/src/utils/index.ts | 1 + packages/core/src/utils/time.ts | 8 +++ 13 files changed, 130 insertions(+), 57 deletions(-) create mode 100644 packages/core/src/utils/time.ts diff --git a/packages/client-sdk-nodejs/src/internal/cache-control-client.ts b/packages/client-sdk-nodejs/src/internal/cache-control-client.ts index c8f2653bf..7a2478073 100644 --- a/packages/client-sdk-nodejs/src/internal/cache-control-client.ts +++ b/packages/client-sdk-nodejs/src/internal/cache-control-client.ts @@ -23,6 +23,7 @@ import { TopicLimits, } from '@gomomento/sdk-core/dist/src/messages/cache-info'; import {RetryInterceptor} from './grpc/retry-interceptor'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export interface ControlClientProps { configuration: Configuration; @@ -32,7 +33,8 @@ export interface ControlClientProps { export class CacheControlClient { private readonly clientWrapper: GrpcClientWrapper; private readonly interceptors: Interceptor[]; - private static readonly REQUEST_TIMEOUT_MS: number = 60 * 1000; + private static readonly REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(60); private readonly logger: MomentoLogger; private readonly cacheServiceErrorMapper: CacheServiceErrorMapper; diff --git a/packages/client-sdk-nodejs/src/internal/cache-data-client.ts b/packages/client-sdk-nodejs/src/internal/cache-data-client.ts index 82d8edb1e..21a5596df 100644 --- a/packages/client-sdk-nodejs/src/internal/cache-data-client.ts +++ b/packages/client-sdk-nodejs/src/internal/cache-data-client.ts @@ -121,6 +121,7 @@ import {common} from '@gomomento/generated-types/dist/common'; import { GetBatchCallOptions, GetCallOptions, + secondsToMilliseconds, SetBatchCallOptions, SetBatchItem, SetCallOptions, @@ -154,7 +155,8 @@ export class CacheDataClient implements IDataClient { private readonly credentialProvider: CredentialProvider; private readonly defaultTtlSeconds: number; private readonly requestTimeoutMs: number; - private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = 5 * 1000; + private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(5); private readonly logger: MomentoLogger; private readonly cacheServiceErrorMapper: CacheServiceErrorMapper; private readonly interceptors: Interceptor[]; @@ -418,6 +420,29 @@ export class CacheDataClient implements IDataClient { } } + /** + * Returns the TTL in milliseconds for a collection. + * If the provided TTL is not set, it defaults to the instance's default TTL. + * @param ttl - The CollectionTttl object containing the TTL value. + * @returns The TTL in milliseconds. + */ + private collectionTtlOrDefaultMilliseconds(ttl: CollectionTtl): number { + return ( + ttl.ttlMilliseconds() ?? secondsToMilliseconds(this.defaultTtlSeconds) + ); + } + + /** + * Returns the TTL in milliseconds. + * If the provided TTL is not set, it defaults to the instance's default TTL. + * @param ttl + * @returns The TTL in milliseconds. + */ + private ttlOrDefaultMilliseconds(ttl?: number): number { + const ttlSeconds = ttl ?? this.defaultTtlSeconds; + return secondsToMilliseconds(ttlSeconds); + } + public async set( cacheName: string, key: string | Uint8Array, @@ -469,7 +494,7 @@ export class CacheDataClient implements IDataClient { const request = new grpcCache._SetRequest({ cache_body: value, cache_key: key, - ttl_milliseconds: ttl * 1000, + ttl_milliseconds: secondsToMilliseconds(ttl), }); const metadata = this.createMetadata(cacheName); return await new Promise((resolve, reject) => { @@ -568,7 +593,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(setName), this.convertArray(elements), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -997,7 +1022,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(key), this.convert(value), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1098,7 +1123,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(key), encodedValue, - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1179,7 +1204,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(key), this.convert(value), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1262,7 +1287,7 @@ export class CacheDataClient implements IDataClient { this.convert(key), this.convert(value), this.convert(equal), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1346,7 +1371,7 @@ export class CacheDataClient implements IDataClient { this.convert(key), this.convert(value), this.convert(notEqual), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1430,7 +1455,7 @@ export class CacheDataClient implements IDataClient { this.convert(key), this.convert(value), this.convert(notEqual), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1515,7 +1540,7 @@ export class CacheDataClient implements IDataClient { this.convert(key), this.convert(value), this.convert(equal), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -1892,7 +1917,7 @@ export class CacheDataClient implements IDataClient { const setRequest = new grpcCache._SetRequest({ cache_key: item[0], cache_body: item[1], - ttl_milliseconds: item[2] * 1000, + ttl_milliseconds: secondsToMilliseconds(item[2]), }); setRequests.push(setRequest); } @@ -1958,7 +1983,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(listName), this.convertArray(values), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateFrontToSize ); @@ -2027,7 +2052,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(listName), this.convertArray(values), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateBackToSize ); @@ -2171,7 +2196,7 @@ export class CacheDataClient implements IDataClient { this.convert(listName), startIndex, endIndex, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -2406,7 +2431,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(listName), this.convert(value), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateFrontToSize ); @@ -2474,7 +2499,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(listName), this.convert(value), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateBackToSize ); @@ -2655,7 +2680,7 @@ export class CacheDataClient implements IDataClient { this.convert(dictionaryName), this.convert(field), this.convert(value), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -2725,7 +2750,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(dictionaryName), dictionaryFieldValuePairs, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -3114,7 +3139,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(field), amount, - (ttl || this.defaultTtlSeconds) * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); }); } @@ -3182,7 +3207,7 @@ export class CacheDataClient implements IDataClient { this.convert(dictionaryName), this.convert(field), amount, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -3255,7 +3280,7 @@ export class CacheDataClient implements IDataClient { this.convert(sortedSetName), this.convert(value), score, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -3327,7 +3352,7 @@ export class CacheDataClient implements IDataClient { cacheName, this.convert(sortedSetName), sortedSetValueScorePairs, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); @@ -3800,7 +3825,7 @@ export class CacheDataClient implements IDataClient { this.convert(sortedSetName), this.convert(value), amount, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); }); diff --git a/packages/client-sdk-nodejs/src/internal/internal-auth-client.ts b/packages/client-sdk-nodejs/src/internal/internal-auth-client.ts index 83e1e57ed..a0b379ff4 100644 --- a/packages/client-sdk-nodejs/src/internal/internal-auth-client.ts +++ b/packages/client-sdk-nodejs/src/internal/internal-auth-client.ts @@ -59,9 +59,11 @@ import { import {RetryInterceptor} from './grpc/retry-interceptor'; import {AuthClientConfigurations} from '../index'; import {AuthClientAllProps} from './auth-client-all-props'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export class InternalAuthClient implements IAuthClient { - private static readonly REQUEST_TIMEOUT_MS: number = 60 * 1000; + private static readonly REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(60); private readonly cacheServiceErrorMapper: CacheServiceErrorMapper; private readonly creds: CredentialProvider; diff --git a/packages/client-sdk-nodejs/src/internal/ping-client.ts b/packages/client-sdk-nodejs/src/internal/ping-client.ts index 9b9292a1b..d29b2af06 100644 --- a/packages/client-sdk-nodejs/src/internal/ping-client.ts +++ b/packages/client-sdk-nodejs/src/internal/ping-client.ts @@ -8,6 +8,7 @@ import {GrpcClientWrapper} from './grpc/grpc-client-wrapper'; import {Configuration} from '../config/configuration'; import {CredentialProvider, MomentoLogger} from '../'; import {RetryInterceptor} from './grpc/retry-interceptor'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export interface PingClientProps { configuration: Configuration; @@ -18,7 +19,8 @@ export interface PingClientProps { export class InternalNodeGrpcPingClient { private readonly clientWrapper: GrpcClientWrapper; private readonly interceptors: Interceptor[]; - private static readonly REQUEST_TIMEOUT_MS: number = 60 * 1000; + private static readonly REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(60); private readonly logger: MomentoLogger; /** diff --git a/packages/client-sdk-nodejs/src/internal/pubsub-client.ts b/packages/client-sdk-nodejs/src/internal/pubsub-client.ts index 8a4d679b0..3975e6394 100644 --- a/packages/client-sdk-nodejs/src/internal/pubsub-client.ts +++ b/packages/client-sdk-nodejs/src/internal/pubsub-client.ts @@ -27,12 +27,14 @@ import {TopicConfiguration} from '../config/topic-configuration'; import {TopicClientAllProps} from './topic-client-all-props'; import {grpcChannelOptionsFromGrpcConfig} from './grpc/grpc-channel-options'; import {RetryInterceptor} from './grpc/retry-interceptor'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export class PubsubClient extends AbstractPubsubClient { private readonly client: grpcPubsub.PubsubClient; protected readonly credentialProvider: CredentialProvider; private readonly unaryRequestTimeoutMs: number; - private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = 5 * 1000; + private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(5); private static readonly DEFAULT_MAX_SESSION_MEMORY_MB: number = 256; private readonly unaryInterceptors: Interceptor[]; private readonly streamingInterceptors: Interceptor[]; diff --git a/packages/client-sdk-nodejs/src/internal/storage-control-client.ts b/packages/client-sdk-nodejs/src/internal/storage-control-client.ts index 908a26486..8486649f3 100644 --- a/packages/client-sdk-nodejs/src/internal/storage-control-client.ts +++ b/packages/client-sdk-nodejs/src/internal/storage-control-client.ts @@ -9,11 +9,13 @@ import {validateStoreName} from '@gomomento/sdk-core/dist/src/internal/utils'; import {CreateStore, DeleteStore} from '@gomomento/sdk-core'; import {RetryInterceptor} from './grpc/retry-interceptor'; import {StorageClientAllProps} from './storage-client-all-props'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export class StorageControlClient { private readonly clientWrapper: grpcControl.ScsControlClient; private readonly interceptors: Interceptor[]; - private static readonly REQUEST_TIMEOUT_MS: number = 60 * 1000; + private static readonly REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(60); private readonly logger: MomentoLogger; private readonly cacheServiceErrorMapper: CacheServiceErrorMapper; diff --git a/packages/client-sdk-nodejs/src/internal/webhook-client.ts b/packages/client-sdk-nodejs/src/internal/webhook-client.ts index b24dd7d24..3a3062ec7 100644 --- a/packages/client-sdk-nodejs/src/internal/webhook-client.ts +++ b/packages/client-sdk-nodejs/src/internal/webhook-client.ts @@ -24,13 +24,15 @@ import { } from '@gomomento/sdk-core/dist/src/internal/utils'; import {RetryInterceptor} from './grpc/retry-interceptor'; import {TopicClientAllProps} from './topic-client-all-props'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export class WebhookClient implements IWebhookClient { private readonly webhookClient: grpcWebhook.WebhookClient; protected readonly credentialProvider: CredentialProvider; private readonly logger: MomentoLogger; private readonly cacheServiceErrorMapper: CacheServiceErrorMapper; - private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = 5 * 1000; + private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(5); private readonly unaryInterceptors: Interceptor[]; /** diff --git a/packages/client-sdk-web/src/internal/cache-data-client.ts b/packages/client-sdk-web/src/internal/cache-data-client.ts index 6e73fb4d9..01cfd4f93 100644 --- a/packages/client-sdk-web/src/internal/cache-data-client.ts +++ b/packages/client-sdk-web/src/internal/cache-data-client.ts @@ -156,6 +156,7 @@ import { NotEqual, } from '@gomomento/generated-types-webtext/dist/common_pb'; import { + secondsToMilliseconds, SetBatchCallOptions, SetBatchItem, SetCallOptions, @@ -226,6 +227,29 @@ export class CacheDataClient< // by both nodejs and web SDKs } + /** + * Returns the TTL in milliseconds for a collection. + * If the provided TTL is not set, it defaults to the instance's default TTL. + * @param ttl - The CollectionTttl object containing the TTL value. + * @returns The TTL in milliseconds. + */ + private collectionTtlOrDefaultMilliseconds(ttl: CollectionTtl): number { + return ( + ttl.ttlMilliseconds() ?? secondsToMilliseconds(this.defaultTtlSeconds) + ); + } + + /** + * Returns the TTL in milliseconds. + * If the provided TTL is not set, it defaults to the instance's default TTL. + * @param ttl + * @returns The TTL in milliseconds. + */ + private ttlOrDefaultMilliseconds(ttl?: number): number { + const ttlSeconds = ttl ?? this.defaultTtlSeconds; + return secondsToMilliseconds(ttlSeconds); + } + public async get( cacheName: string, key: string | Uint8Array @@ -337,7 +361,7 @@ export class CacheDataClient< const request = new _SetRequest(); request.setCacheKey(key); request.setCacheBody(value); - request.setTtlMilliseconds(this.convertSecondsToMilliseconds(ttlSeconds)); + request.setTtlMilliseconds(secondsToMilliseconds(ttlSeconds)); return await new Promise((resolve, reject) => { this.clientWrapper.set( @@ -391,7 +415,7 @@ export class CacheDataClient< cacheName, convertToB64String(key), convertToB64String(field), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace(`'setIfNotExists' request result: ${result.toString()}`); return result; @@ -476,7 +500,7 @@ export class CacheDataClient< cacheName, convertToB64String(key), convertToB64String(field), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace(`'setIfAbsent' request result: ${result.toString()}`); return result; @@ -560,7 +584,7 @@ export class CacheDataClient< cacheName, convertToB64String(key), convertToB64String(field), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace(`'setIfPresent' request result: ${result.toString()}`); return result; @@ -646,7 +670,7 @@ export class CacheDataClient< convertToB64String(key), convertToB64String(field), convertToB64String(equal), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace(`'setIfEqual' request result: ${result.toString()}`); return result; @@ -733,7 +757,7 @@ export class CacheDataClient< convertToB64String(key), convertToB64String(field), convertToB64String(notEqual), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace(`'setIfNotEqual' request result: ${result.toString()}`); return result; @@ -820,7 +844,7 @@ export class CacheDataClient< convertToB64String(key), convertToB64String(field), convertToB64String(notEqual), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace( `'setIfPresentAndNotEqual' request result: ${result.toString()}` @@ -912,7 +936,7 @@ export class CacheDataClient< convertToB64String(key), convertToB64String(field), convertToB64String(equal), - ttl ? ttl * 1000 : this.defaultTtlSeconds * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace( `'setIfAbsentOrEqual' request result: ${result.toString()}` @@ -1088,7 +1112,7 @@ export class CacheDataClient< const setRequest = new _SetRequest(); setRequest.setCacheKey(key); setRequest.setCacheBody(value); - setRequest.setTtlMilliseconds(this.convertSecondsToMilliseconds(ttl)); + setRequest.setTtlMilliseconds(secondsToMilliseconds(ttl)); setRequests.push(setRequest); } const request = new _SetBatchRequest(); @@ -1202,7 +1226,7 @@ export class CacheDataClient< cacheName, convertToB64String(field), amount, - (ttl || this.defaultTtlSeconds) * 1000 + this.ttlOrDefaultMilliseconds(ttl) ); this.logger.trace(`'increment' request result: ${result.toString()}`); return result; @@ -1315,7 +1339,7 @@ export class CacheDataClient< cacheName, convertToB64String(setName), this.convertArrayToB64Strings(elements), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); } @@ -1767,7 +1791,7 @@ export class CacheDataClient< cacheName, convertToB64String(listName), this.convertArrayToB64Strings(values), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateFrontToSize ); @@ -1845,7 +1869,7 @@ export class CacheDataClient< cacheName, convertToB64String(listName), this.convertArrayToB64Strings(values), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateBackToSize ); @@ -2009,7 +2033,7 @@ export class CacheDataClient< convertToB64String(listName), // passing ttl info before start/end because it's guaranteed to be defined so doesn't need // to be nullable - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), startIndex, endIndex @@ -2274,7 +2298,7 @@ export class CacheDataClient< cacheName, convertToB64String(listName), convertToB64String(value), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateFrontToSize ); @@ -2349,7 +2373,7 @@ export class CacheDataClient< cacheName, convertToB64String(listName), convertToB64String(value), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl(), truncateBackToSize ); @@ -2481,7 +2505,7 @@ export class CacheDataClient< convertToB64String(dictionaryName), convertToB64String(field), convertToB64String(value), - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); this.logger.trace( @@ -2560,7 +2584,7 @@ export class CacheDataClient< cacheName, convertToB64String(dictionaryName), dictionaryFieldValuePairs, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); this.logger.trace( @@ -2873,7 +2897,7 @@ export class CacheDataClient< convertToB64String(dictionaryName), convertToB64String(field), amount, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); this.logger.trace( @@ -3399,7 +3423,7 @@ export class CacheDataClient< convertToB64String(sortedSetName), convertToB64String(value), score, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); this.logger.trace( @@ -3480,7 +3504,7 @@ export class CacheDataClient< cacheName, convertToB64String(sortedSetName), sortedSetValueScorePairs, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); this.logger.trace( @@ -3755,7 +3779,7 @@ export class CacheDataClient< convertToB64String(sortedSetName), convertToB64String(value), amount, - ttl.ttlMilliseconds() || this.defaultTtlSeconds * 1000, + this.collectionTtlOrDefaultMilliseconds(ttl), ttl.refreshTtl() ); @@ -4508,10 +4532,6 @@ export class CacheDataClient< }); } - private convertSecondsToMilliseconds(ttlSeconds: number): number { - return ttlSeconds * 1000; - } - private convertArrayToB64Strings(v: string[] | Uint8Array[]): string[] { return v.map(i => convertToB64String(i)); } diff --git a/packages/client-sdk-web/src/internal/pubsub-client.ts b/packages/client-sdk-web/src/internal/pubsub-client.ts index 43f561836..904ef37ed 100644 --- a/packages/client-sdk-web/src/internal/pubsub-client.ts +++ b/packages/client-sdk-web/src/internal/pubsub-client.ts @@ -23,6 +23,7 @@ import { } from '../utils/web-client-utils'; import {ClientMetadataProvider} from './client-metadata-provider'; import {TopicClientAllProps} from './topic-client-all-props'; +import {secondsToMilliseconds} from '@gomomento/sdk-core/dist/src/utils'; export class PubsubClient< REQ extends Request, @@ -31,7 +32,8 @@ export class PubsubClient< private readonly client: pubsub.PubsubClient; private readonly credentialProvider: CredentialProvider; private readonly requestTimeoutMs: number; - private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = 5 * 1000; + private static readonly DEFAULT_REQUEST_TIMEOUT_MS: number = + secondsToMilliseconds(5); private readonly clientMetadataProvider: ClientMetadataProvider; private static readonly RST_STREAM_NO_ERROR_MESSAGE = diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a606f038e..9b988de04 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -104,6 +104,8 @@ import { SetBatchItem, } from './utils'; +export * as utils from './utils'; + import { CredentialProvider, StringMomentoTokenProvider, diff --git a/packages/core/src/utils/collection-ttl.ts b/packages/core/src/utils/collection-ttl.ts index d2df2dfaa..22a5b5812 100644 --- a/packages/core/src/utils/collection-ttl.ts +++ b/packages/core/src/utils/collection-ttl.ts @@ -1,4 +1,5 @@ import {validateTtlSeconds} from '../internal/utils'; +import {secondsToMilliseconds} from './time'; /** Represents the desired behavior for managing the TTL on collection * objects (dictionaries, lists, sets) in your cache. @@ -46,7 +47,9 @@ export class CollectionTtl { * @returns {number | null} */ public ttlMilliseconds(): number | null { - return this._ttlSeconds === null ? null : this._ttlSeconds * 1000; + return this._ttlSeconds === null + ? null + : secondsToMilliseconds(this._ttlSeconds); } /** Whether or not to refresh a collection's TTL when it's modified. diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index aaf4d0294..9b6d5cfcc 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -5,3 +5,4 @@ export * from './expiration'; export * from './itemType'; export * from './webhook-destination'; export * from './set-batch-item'; +export * from './time'; diff --git a/packages/core/src/utils/time.ts b/packages/core/src/utils/time.ts new file mode 100644 index 000000000..ceaafe9e2 --- /dev/null +++ b/packages/core/src/utils/time.ts @@ -0,0 +1,8 @@ +/** + * Converts seconds to milliseconds. + * @param seconds - The number of seconds to convert. + * @returns The equivalent number of milliseconds. + */ +export function secondsToMilliseconds(seconds: number): number { + return seconds * 1000; +}