I have a web process, similar to
func main() {
// mgo
mongoDatabase, err := mgopath.Connect(envMongoPath)
if err != nil {
log.Fatal(err)
}
r := mux.NewRouter()
// ....
r.HandleFunc("/apps/{app:.+}", stuffHandler(mongoDatabase)).Methods("GET")
http.Handle("/", r)
listen := fmt.Sprintf("%s:%s", host, port)
log.Fatal(http.ListenAndServe(listen, nil))
}
while mgopath.Connect looks like
func Connect(mongoPath string) (*mgo.Database, error) {
dbConfig, err := url.Parse(mongoPath)
if err != nil {
return nil, err
}
log.Printf("Connecting to %s", dbConfig.Host)
sess, err := mgo.Dial(dbConfig.Host)
if err != nil {
return nil, err
}
dbName := dbConfig.Path
log.Printf("Using database %s", dbName)
if len(dbName) < 2 {
return nil, errors.New("No database name specified.")
}
dbName = dbConfig.Path[1:len(dbConfig.Path)]
return sess.DB(dbName), err
}
Somewhere down the road:
c := database.C("stuff")
err = c.Find(bson.M{"id": id}).One(&item) // First ~1-2 minutes work as expected, then I receive EOFs
// c.Insert(), c.Update(), c.All()...
The problem is, that after a couple of minutes, all queries to mongodb return the error EOF
. I have to restart the process to get things working again. Experienced both issues on Mac and (more frequently) on Windows. MongoDB runs inside of Docker, which in turn runs inside of boot2docker. VM Port forwarding have been done (that's why queries work for a certain amount of time).
Does mgo require me to dial every time I'm doing a query? Is there some timeout I don't know about?