globalsign / mgo

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

mgo: FindAndModify create too many sockets #397

Open andrewrong opened 3 years ago

andrewrong commented 3 years ago

We use the issue tracker to track bugs with mgo - if you have a usage question, it's best to try Stack Overflow :)

Replace this text with your description, and please answer the questions below before submitting your issue to help us out. Thanks!


What version of MongoDB are you using (mongod --version)?

3.4

What version of Go are you using (go version)?

go version go1.14.7 

What operating system and processor architecture are you using (go env)?

GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.7/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/h3/k95txtxn3wq3nvxx5xxcnj280000gn/T/go-build203709446=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

  1. init 100 mgo.session (mgo session is not use),
  2. Multiple(for example 100) goroutine run command FindAndModify use session that the step created
  3. you find that mgo create many socket connects mongos

I look the FindAndModify's source code

globalsign/mgo/session.go::Apply()::5032

        session = session.Clone()
        defer session.Close()
        session.SetMode(Strong, false)

If the concurrency using FindAndModify is high enough, then many connections will be created; I think it can be modified like this:

    if session.Mode() != Strong {
        session = session.Clone()
        defer session.Close()
        session.SetMode(Strong, false)
    }

Can you reproduce the issue on the latest development branch?

yeah