Skip to content

Commit

Permalink
setting up mongo for reconnect policy and lenient first connect
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslan committed Nov 28, 2016
1 parent 53d9a86 commit 35637b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
.idea
*.iml

# VSCODE
.vscode

# Eclipse specific
.jshintrc
.project
Expand Down
80 changes: 37 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ const stdLogger = {
warn: consoleLogger('warn'),
error: consoleLogger('error')
}
const RECONNECT_TIMEOUT = 30000

let standardOptions = {
db: {
native_parser: true
},
server: {
socketOptions: {
keepAlive: 1
},
ssl: false,
authenticationDatabase: '',
auto_reconnect: true,
reconnectInterval: RECONNECT_TIMEOUT
},
replset: {
socketOptions: {
keepAlive: 1
}
},
user: '',
pass: ''
}

function consoleLogger (level) {
return function (file, msg) {
Expand Down Expand Up @@ -41,9 +64,13 @@ function _connect (options, sslOptions) {
log.info('Connect DB: ' + options.dbUri)

mongoose.connection.on('error', dbErr => {
log.warn({ err: dbErr }, 'DB connection error')
log.warn({ err: dbErr.message }, 'DB connection error, retrying in 30 seconds')

This comment has been minimized.

Copy link
@jhsware

jhsware Dec 1, 2016

Message shouldn't be hard coded, use options.reconnectInterval instead

isOk = false
reject(dbErr)
// reject(dbErr)
setTimeout(function () {
mongoose.connect(dbUri, dbOptions)
log.info('Attempting to reconnect')
}, options.reconnectInterval || RECONNECT_TIMEOUT)

This comment has been minimized.

Copy link
@jhsware

jhsware Dec 1, 2016

options.reconnectInterval is set in getMongoOptions. This could be confusing since RECONNECT_TIMEOUT will never be used

})

mongoose.connection.on('connected', () => {
Expand All @@ -65,52 +92,19 @@ function _connect (options, sslOptions) {
}

function getMongoOptions (options) {
var dbOptions = {
db: {
native_parser: true
},
server: {
socketOptions: {
keepAlive: 1
}
},
reconnectTries: options.reconnectTries || 1000,
reconnectInterval: options.reconnectInterval || 30000,
replset: {
socketOptions: {
keepAlive: 1
}
},
user: options.dbUsername,
pass: options.dbPassword
}
var dbOptions = standardOptions
dbOptions.reconnectInterval = options.reconnectInterval || RECONNECT_TIMEOUT
dbOptions.reconnectTries = options.reconnectTries || 0
dbOptions.user = options.dbUsername
dbOptions.pass = options.dbPassword

return dbOptions
}

function getMongoOptionsSsl (options, sslOptions) {
var dbOptions = {
db: {
native_parser: true
},
server: {
socketOptions: {
keepAlive: 1
},
ssl: sslOptions.ssl,
authenticationDatabase: sslOptions.authDatabase,
sslCA: sslOptions.sslCA
},
reconnectTries: options.reconnectTries || 1000,
reconnectInterval: options.reconnectInterval || 30000,
replset: {
socketOptions: {
keepAlive: 1
}
},
user: options.dbUsername,
pass: options.dbPassword
}
var dbOptions = getMongoOptions(options)
dbOptions.ssl = sslOptions.ssl
dbOptions.authenticationDatabase = sslOptions.authDatabase

return dbOptions
}

0 comments on commit 35637b6

Please sign in to comment.