Skip to content

Commit

Permalink
Add DirectConnection Option in MongoDB SDK (#15968)
Browse files Browse the repository at this point in the history
We currently have a ReplicaSetNoPrimary error caused by
MongoServerSelectionError: connection timed out\n
{"reason"=>{"type"=>"ReplicaSetNoPrimary", "servers"=>{},
"stale"=>"REDACTED", "compatible"=>"REDACTED",
"heartbeatFrequencyMS"=>"REDACTED", "localThresholdMS"=>"REDACTED",
"setName"=>"REDACTED", "maxElectionId"=>nil,
"maxSetVersion"=>"REDACTED", "commonWireVersion"=>"REDACTED",
"logicalSessionTimeoutMinutes"=>nil},
"name"=>"MongoServerSelectionError", "message"=>"connection timed out",
"stack"=>"MongoServerSelectionError: connection timed out\n at
Timeout._onTimeout (/usr/src/server/bundle/alfred/www.js:126365:34)\n at
listOnTimeout (node:internal/timers:559:17)\n at processTimers
(node:internal/timers:502:7)"}

From:
https://stackoverflow.com/questions/59162342/mongodb-connection-error-mongotimeouterror-server-selection-timed-out-after-30,
we can fix it by enabling directConnection flag as true.

Here is the definition for directConnection:
https://mongodb.github.io/node-mongodb-native/4.14/interfaces/MongoClientOptions.html#directConnection
  • Loading branch information
tianzhu007 authored Jun 14, 2023
1 parent a51cd2a commit 58c6203
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data:
"globalDbEnabled": {{ .Values.mongodb.globalDbEnabled }},
"expireAfterSeconds": {{ .Values.mongodb.expireAfterSeconds }},
"createCosmosDBIndexes": {{ .Values.mongodb.createCosmosDBIndexes }},
"directConnection": {{ .Values.mongodb.directConnection }},
"softDeletionRetentionPeriodMs": {{ .Values.mongodb.softDeletionRetentionPeriodMs }},
"offlineWindowMs": {{ .Values.mongodb.offlineWindowMs }},
"softDeletionEnabled": {{ .Values.mongodb.softDeletionEnabled }},
Expand Down
1 change: 1 addition & 0 deletions server/routerlicious/kubernetes/routerlicious/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ mongodb:
globalDbEnabled: false
expireAfterSeconds: -1
createCosmosDBIndexes: false
directConnection: true
softDeletionRetentionPeriodMs: 2592000000
offlineWindowMs: 86400000
softDeletionEnabled: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"globalDbEnabled": false,
"expireAfterSeconds": -1,
"createCosmosDBIndexes": false,
"directConnection": true,
"softDeletionRetentionPeriodMs": 2592000000,
"offlineWindowMs": 86400000,
"softDeletionEnabled": false,
Expand Down
5 changes: 5 additions & 0 deletions server/routerlicious/packages/services/src/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ interface IMongoDBConfig {
globalDbEnabled?: boolean;
connectionPoolMinSize?: number;
connectionPoolMaxSize?: number;
directConnection?: boolean;
facadeLevelRetry?: boolean;
facadeLevelTelemetry?: boolean;
facadeLevelRetryRuleOverride?: any;
Expand All @@ -463,6 +464,7 @@ export class MongoDbFactory implements core.IDbFactory {
private readonly globalDbEndpoint?: string;
private readonly connectionPoolMinSize?: number;
private readonly connectionPoolMaxSize?: number;
private readonly directConnection: boolean;
private readonly retryEnabled: boolean = false;
private readonly telemetryEnabled: boolean = false;
private readonly connectionNotAvailableMode: ConnectionNotAvailableMode = "ruleBehavior";
Expand All @@ -474,6 +476,7 @@ export class MongoDbFactory implements core.IDbFactory {
globalDbEndpoint,
connectionPoolMinSize,
connectionPoolMaxSize,
directConnection,
connectionNotAvailableMode,
} = config;
if (globalDbEnabled) {
Expand All @@ -484,6 +487,7 @@ export class MongoDbFactory implements core.IDbFactory {
this.connectionPoolMinSize = connectionPoolMinSize;
this.connectionPoolMaxSize = connectionPoolMaxSize;
this.connectionNotAvailableMode = connectionNotAvailableMode ?? "ruleBehavior";
this.directConnection = directConnection ?? false;
this.retryEnabled = config.facadeLevelRetry || false;
this.telemetryEnabled = config.facadeLevelTelemetry || false;
this.retryRuleOverride = config.facadeLevelRetryRuleOverride
Expand All @@ -499,6 +503,7 @@ export class MongoDbFactory implements core.IDbFactory {
);
// Need to cast to any before MongoClientOptions due to missing properties in d.ts
const options: MongoClientOptions = {
directConnection: this.directConnection ?? false,
keepAlive: true,
keepAliveInitialDelay: 180000,
socketTimeoutMS: 120000,
Expand Down

0 comments on commit 58c6203

Please sign in to comment.