Skip to content

Commit

Permalink
chore: move ping functionality into cache client (#516)
Browse files Browse the repository at this point in the history
* chore: move ping functionality into cache client

* fix: fix test typo

* fix: simplify calculation of ping client endpoint
  • Loading branch information
pgautier404 authored May 18, 2023
1 parent d7b7945 commit 4b2f77d
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 80 deletions.
23 changes: 20 additions & 3 deletions packages/client-sdk-web/src/cache-client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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,
IDataClient,
ICacheClient,
} from '@gomomento/sdk-core/dist/src/internal/clients/index';
IDataClient,
IPingClient,
} from '@gomomento/sdk-core/dist/src/internal/clients';

export interface CacheClientProps {
credentialProvider: CredentialProvider;
Expand All @@ -19,7 +21,8 @@ export class CacheClient extends AbstractCacheClient implements ICacheClient {
constructor(props: CacheClientProps) {
const controlClient: IControlClient = createControlClient(props);
const dataClient: IDataClient = createDataClient(props);
super(controlClient, [dataClient]);
const pingClient: IPingClient = createPingClient(props);
super(controlClient, [dataClient], pingClient);
}
}

Expand Down Expand Up @@ -51,3 +54,17 @@ function createDataClient(props: CacheClientProps): IDataClient {
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(),
},
});
}
8 changes: 5 additions & 3 deletions packages/client-sdk-web/src/internal/ping-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import {Request, UnaryInterceptor, UnaryResponse} from 'grpc-web';
import {_PingRequest} from '@gomomento/generated-types-webtext/dist/cacheping_pb';
import {Configuration} from '../config/configuration';
import {MomentoLogger} from '@gomomento/sdk-core';
import {IPingClient} from '@gomomento/sdk-core/dist/src/internal/clients';

export interface PingClientProps {
endpoint: string;
configuration: Configuration;
}

export class InternalGrpcWebPingClient<
export class PingClient<
REQ extends Request<REQ, RESP>,
RESP extends UnaryResponse<REQ, RESP>
> {
> implements IPingClient
{
private readonly clientWrapper: ping.PingClient;
private readonly unaryInterceptors: UnaryInterceptor<REQ, RESP>[];
private readonly logger: MomentoLogger;
Expand All @@ -31,7 +33,7 @@ export class InternalGrpcWebPingClient<
).createHeadersInterceptor(),
];
this.clientWrapper = new ping.PingClient(
`https://cache.${props.endpoint}:443`,
`https://${props.endpoint}:443`,
null,
{
unaryInterceptors: this.unaryInterceptors,
Expand Down
30 changes: 0 additions & 30 deletions packages/client-sdk-web/src/ping-client.ts

This file was deleted.

36 changes: 30 additions & 6 deletions packages/client-sdk-web/test/integration/ping.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import {PingClient} from '../../src/ping-client';
import {NoopMomentoLoggerFactory} from '@gomomento/sdk-core';
import {
CredentialProvider,
MomentoLoggerFactory,
NoopMomentoLoggerFactory,
} from '@gomomento/sdk-core';
import {CacheClient} from '../../src';
import {PingClient} from '../../src/internal/ping-client';
import {expectWithMessage} from '@gomomento/common-integration-tests';

describe('ping service', () => {
it('ping should work', async () => {
const client = new PingClient({
endpoint: 'cell-alpha-dev.preprod.a.momentohq.com',
const cacheClient = new CacheClient({
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'TEST_AUTH_TOKEN',
}),
});
await cacheClient.ping();
});
it('should fail on bad URL', async () => {
const pingClient = new PingClient({
endpoint: 'bad.url',
configuration: {
getLoggerFactory: () => new NoopMomentoLoggerFactory(),
getLoggerFactory(): MomentoLoggerFactory {
return new NoopMomentoLoggerFactory();
},
},
});
await client.ping();
try {
await pingClient.ping();
// we shouldn't get to the assertion below
expect(true).toBeFalse();
} catch (error) {
expectWithMessage(() => {
expect((error as Error).name).toEqual('RpcError');
}, `expected RpcError but got ${(error as Error).toString()}`);
}
});
});
15 changes: 14 additions & 1 deletion packages/core/src/internal/clients/cache/AbstractCacheClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,39 @@ import {
} from './ICacheClient';
import {IControlClient} from './IControlClient';
import {IDataClient} from './IDataClient';
import {IPingClient} from './IPingClient';

export abstract class AbstractCacheClient implements ICacheClient {
// making these protected until we fully abstract away the nodejs client
protected readonly controlClient: IControlClient;
protected readonly dataClients: IDataClient[];
// TODO: Make pingClient required if and when the nodejs side starts adding
// one as well
protected readonly pingClient?: IPingClient;
protected nextDataClientIndex: number;

protected constructor(
controlClient: IControlClient,
dataClients: IDataClient[]
dataClients: IDataClient[],
pingClient?: IPingClient
) {
this.controlClient = controlClient;
this.dataClients = dataClients;
this.pingClient = pingClient;

// We round-robin the requests through all of our clients. Since javascript
// is single-threaded, we don't have to worry about thread safety on this
// index variable.
this.nextDataClientIndex = 0;
}

/**
* Ping the service to verify it is up and running
*/
public async ping(): Promise<void> {
return await this.pingClient?.ping();
}

/**
* Creates a cache if it does not exist.
*
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/internal/clients/cache/ICacheClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {IControlClient} from './IControlClient';
import {IPingClient} from './IPingClient';
import {
CacheDelete,
CacheGet,
Expand Down Expand Up @@ -67,7 +68,7 @@ export type SortedSetFetchByRankOptions = SortedSetFetchByRankCallOptions;
export type SortedSetFetchByScoreOptions = SortedSetFetchByScoreCallOptions;
export type SortedSetIncrementOptions = CollectionCallOptions;

export interface ICacheClient extends IControlClient {
export interface ICacheClient extends IControlClient, IPingClient {
get(cacheName: string, key: string | Uint8Array): Promise<CacheGet.Response>;
set(
cacheName: string,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/internal/clients/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './AbstractCacheClient';
export * from './ICacheClient';
export * from './IControlClient';
export * from './IDataClient';
export * from './IPingClient';
1 change: 0 additions & 1 deletion packages/core/src/internal/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './ping';
export * from './cache';
export * from './auth';
export * from './pubsub';
33 changes: 0 additions & 33 deletions packages/core/src/internal/clients/ping/AbstractPingClient.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/core/src/internal/clients/ping/index.ts

This file was deleted.

0 comments on commit 4b2f77d

Please sign in to comment.