Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router,admin,hermes): new redis options compatibility #443

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions libraries/grpc-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
Admin,
Authentication,
Authorization,
Chat,
Config,
Core,
Expand All @@ -10,8 +12,6 @@ import {
Router,
SMS,
Storage,
Authorization,
Authentication,
} from './modules';
import Crypto from 'crypto';
import { EventBus } from './utilities/EventBus';
Expand Down Expand Up @@ -46,6 +46,7 @@ export default class ConduitGrpcSdk {
private readonly _core?: Core;
private readonly _config?: Config;
private readonly _admin?: Admin;
private _redisManager: RedisManager | null = null;
private _redisDetails?:
| RedisOptions
| { nodes: { host: string; port: number }[]; options: ClusterOptions };
Expand Down Expand Up @@ -398,18 +399,17 @@ export default class ConduitGrpcSdk {
}
return promise
.then(() => {
let redisManager: RedisManager;
if (this._redisDetails!.hasOwnProperty('nodes')) {
redisManager = new RedisManager(this._redisDetails);
this._redisManager = new RedisManager(this._redisDetails);
} else {
const redisHost = this.urlRemap ?? (this._redisDetails as RedisOptions).host;
redisManager = new RedisManager({
this._redisManager = new RedisManager({
host: redisHost,
...this._redisDetails,
});
}
this._eventBus = new EventBus(redisManager);
this._stateManager = new StateManager(redisManager, this.name);
this._eventBus = new EventBus(this._redisManager);
this._stateManager = new StateManager(this._redisManager, this.name);
return this._eventBus;
})
.catch((err: Error) => {
Expand All @@ -418,6 +418,14 @@ export default class ConduitGrpcSdk {
});
}

get redisManager(): RedisManager {
if (this._redisManager) {
return this._redisManager;
} else {
throw new Error('Redis not available');
}
}

/**
* Gets all the registered modules from the config and creates clients for them.
* This will only work on known modules, since the primary usage for the sdk is internal
Expand Down
9 changes: 4 additions & 5 deletions libraries/hermes/src/Socket/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Application, NextFunction, Request, Response } from 'express';
import { createServer, Server as httpServer } from 'http';
import { Server as IOServer, ServerOptions, Socket } from 'socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import IORedis, { Redis } from 'ioredis';
import { Cluster, Redis } from 'ioredis';
import { ConduitRouter } from '../Router';
import { isNil } from 'lodash';
import {
Expand All @@ -20,8 +20,8 @@ export class SocketController extends ConduitRouter {
private io: IOServer;
private readonly options: Partial<ServerOptions>;
private _registeredNamespaces: Map<string, ConduitSocket>;
private readonly pubClient: Redis;
private readonly subClient: Redis;
private readonly pubClient: Redis | Cluster;
private readonly subClient: Redis | Cluster;
private globalMiddlewares: ((
req: Request,
res: Response,
Expand All @@ -32,7 +32,6 @@ export class SocketController extends ConduitRouter {
private readonly port: number,
grpcSdk: ConduitGrpcSdk,
expressApp: Application,
redisDetails: { host: string; port: number },
private readonly metrics?: {
registeredRoutes?: {
name: string;
Expand All @@ -49,7 +48,7 @@ export class SocketController extends ConduitRouter {
},
};
this.io = new IOServer(this.httpServer, this.options);
this.pubClient = new IORedis(redisDetails.port, redisDetails.host);
this.pubClient = grpcSdk.redisManager.getClient();
this.subClient = this.pubClient.duplicate();
this.io.adapter(createAdapter(this.pubClient, this.subClient));
this.httpServer.listen(this.port);
Expand Down
6 changes: 1 addition & 5 deletions libraries/hermes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,12 @@ export class ConduitRoutingController {
this._graphQLRouter = new GraphQLController(this.grpcSdk, this.metrics);
}

initSockets(redisHost: string, redisPort: number) {
initSockets() {
if (this._socketRouter) return;
this._socketRouter = new SocketController(
this.socketPort,
this.grpcSdk,
this.expressApp,
{
host: redisHost,
port: redisPort,
},
this.metrics,
);
}
Expand Down
5 changes: 1 addition & 4 deletions modules/router/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ export default class ConduitDefaultRouter extends ManagedModule<Config> {
this._internalRouter.stopRest();
}
if (config.transports.sockets) {
this._internalRouter.initSockets(
this.grpcSdk.redisDetails.host,
this.grpcSdk.redisDetails.port,
);
this._internalRouter.initSockets();
atLeastOne = true;
} else {
this._internalRouter.stopSockets();
Expand Down
8 changes: 4 additions & 4 deletions modules/router/src/security/handlers/rate-limiter/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import IORedis, { Redis } from 'ioredis';
import { Cluster, Redis } from 'ioredis';

import { RateLimiterRedis } from 'rate-limiter-flexible';
import { ConduitError } from '@conduitplatform/grpc-sdk';
import ConduitGrpcSdk, { ConduitError } from '@conduitplatform/grpc-sdk';

export class RateLimiter {
private _limiter: any;

constructor(redisHost: string, redisPort: number) {
const redisClient: Redis = new IORedis(redisPort, redisHost, {
constructor(private readonly grpcSdk: ConduitGrpcSdk) {
const redisClient: Redis | Cluster = this.grpcSdk.redisManager.getClient({
enableOfflineQueue: false,
});
this._limiter = new RateLimiterRedis({
Expand Down
3 changes: 1 addition & 2 deletions modules/router/src/security/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export default class SecurityModule {

this.router.registerGlobalMiddleware(
'rateLimiter',
new RateLimiter(this.grpcSdk.redisDetails.host, this.grpcSdk.redisDetails.port)
.limiter,
new RateLimiter(this.grpcSdk).limiter,
true,
);
this.router.registerGlobalMiddleware('helmetMiddleware', helmet());
Expand Down
5 changes: 1 addition & 4 deletions packages/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ export default class AdminModule extends IConduitAdmin {
this._router.stopGraphQL();
}
if (ConfigController.getInstance().config.transports.sockets) {
this._router.initSockets(
this.grpcSdk.redisDetails.host,
this.grpcSdk.redisDetails.port,
);
this._router.initSockets();
} else {
this._router.stopSockets();
}
Expand Down