Can't create secondary mongodb connection
Closed this issue · 5 comments
Tests failing - https://github.com/apexskier/httpauth/blob/mjhall-mongodb/mongoBackend_test.go#L70
The intent of this test is to connect to an existing mongoldb with saved data and pull correct info out of it.
I'm puzzled, where are driverName
and driverInfo
coming from? Did you mean url
and db
?
(side note: Close
is never called on the mgo Session
s)
Ha, that's the code from the sql tests, I forgot to change over. I suppose it should defer a close as well. That's probably why this is broken.
Fixed, though I'm not positive the session's being closed properly. I can't use a defer anywhere more, unless the session is stored in the MongodbAuthBackend struct (maybe?).
Looking at the mgo docs, I believe you'd have better performances storing the mgo.Session
in the MongodbAuthBackend
struct (but you must add Close()
method to it that'd call Close()
on the mgo.Session
) and have connect()
call Copy()
on the stored session.
Also, your added defer session.Close()
in NewMongodbBackend
can error as mgo.Dial
returns a nil
session when there's an error.
And each method calling connect()
should defer
closing the session, e.g.
c, err := b.connect()
if err != nil {
panic(err)
}
defer c.Database.Session.Close()
(as a side note, are you sure this if err != nil { panic(err) }
is an idiomatic Go pattern? shouldn't it be a return err
instead?)
Cool that all makes sense. Yes, the panics should be phased out for sure.