Note: I am not sure if this is the most accurate title for this post, if not, please advise on a better one.
Currently I am creating a server where I have a couple of handlers (using goji). After receiving a request, I want to interact with a MongoDB database I have (using mgo). My question is:
I am assuming doing this kind of stuff every time I am handling a request is expensive:
uri := os.Getenv("MONGOHQ_URL")
if uri == "" {
panic("no DB connection string provided")
}
session, err := mgo.Dial(uri)
So, would it be better for me to have a global var that I can access from inside the handlers? So I would go with something like this:
var session *mgo.Session
func main() {
session = setupDB()
defer session.Close()
goji.Get("/user", getUser)
goji.Serve()
}
func getUser(c web.C, w http.ResponseWriter, r *http.Request) {
// Use the session var here
}
My question is related to what would be the best practise here? Opening the DB every time a request comes in, or keep it open for the entire duration of the application.