Wednesday, 3 May 2017

Google App Engine Flex Instance Hours with node.js and mongodb. Creating millions of sessions

I might still have the same problem as in my previous question, but now it's starting to cost me: Nodejs mongostore creating thousands of sessions without activity when deployed to GAE

I have deployed a node.js instance to GAE and i'm using mongodb as a session store.

However, my problem is that my mongodb session store is growing super fast with about 1 million sessions created in 24 hours. All without any user interaction.enter image description here

This has lead to a hefty "development/testing" sum on Google Cloud Platform. enter image description here

What should I be checking in my node.js codebase? What can be the cause of this? A middleware being run and creating sessions? Anything with mongoose?

Any help is appreciated since I can't seem to solve this myself.

Here is my code for server.js:

// Reads .env variables for encrypted data
require('dotenv').config()

var express = require('express')
var mongoose = require('mongoose')
var flash = require('connect-flash')
var passport = require('passport')
var path = require('path')
var app = express()
var bodyParser = require('body-parser')
var port = process.env.PORT || 8080
var db = process.env.MONGO_URI

app.set('views', './views')
app.set('view engine', 'ejs')

app.use(express.static(path.join(__dirname, '/public')))

mongoose.connect(db) // connect to our database

const session = require('express-session')
const MongoStore = require('connect-mongo')(session)

app.use(session({
  secret: 'hakunamatata',
  saveUninitialized: false,
  resave: false,
  store: new MongoStore({
    mongooseConnection: mongoose.connection,
    saveUninitialized: false,
    resave: false,
    cookie: {
      secure: true,
      maxAge: new Date(Date.now() + 3600000)
    },
    clear_interval: 3600000,
    autoRemove: 'interval',
    autoRemoveInterval: 1
  })
}))

app.use(flash())

var initPassport = require('./app/authentication/passport')
initPassport(passport)
app.use(passport.initialize())
app.use(passport.session()) // persistent login sessions
require('./app/routes/routes')(app, passport)

app.listen(port)
console.log('Dev port is: ' + port)



via Simon Nilsson Guldstrand

No comments:

Post a Comment