globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

Abnormal query time #174

Closed zenghur closed 6 years ago

zenghur commented 6 years ago

Recently, there was a weired behavior in our prod environment. A single find one query cost to much time. I already built index on this field. I use this snippet of code to reproduce the same behavior.

var wg sync.WaitGroup

for i := 0; i < 100; i++ {
    wg.Add(1)
    go func() {
        session := globalSession.Copy()
        // do find one query here
        session.Close()
        wg.Done()
    }()
}

wg.Wait()

Then, I find there are high probabilities that socketLogin() would auth even though globalSession has been authenticated. This action costs too much time. Why this copy session cant remember the existing auth info?

domodwyer commented 6 years ago

Hi @zenghur

Do you have more information? Like what authentication mechanism you're using, mongo version, etc.

Even though the "global session" (really just the first connection) has been authenticated, subsequent connections still need to authenticate because they're new connections mongo doesn't know about. Your tight loop is creating more work than a single connection can perform, so the driver is opening more connections, thus requiring the authentication to be completed for each.

Dom