globalsign / mgo

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

Handling of io.EOF error and related documentation #273

Closed smaheshs closed 5 years ago

smaheshs commented 6 years ago

From this discussion here I understand that when a session is being used in a for loop it is safe to do a session.Refresh() at the beginning of the loop.

However it is not clear how to handle the collections created from the session.

Should the collection variables be assigned after every Refresh() like this:

session := // create mgo session
for{
    session.Refresh()

    collection := session.DB("dbname").C("collectionName")
    ....
}

OR can the collection be assigned only once outside the loop as below:

session := // create mgo session
collection := session.DB("dbname").C("collectionName")
for{
    session.Refresh()

    ....
}

It would be helpful if this information is available in the documentation.


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

3.2.21

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

1.9.2

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

1.11

What did you do?

Unfortunately this is a rare issue due to which the right approach cannot be arrived at easily just by experimenting.

Can you reproduce the issue on the latest development branch?

domodwyer commented 5 years ago

Hi @smaheshs

A quick read of the source shows a Collection has a pointer to the Database:

type Collection struct {
    Database *Database
    Name     string // "collection"
    FullName string // "db.collection"
}

The Database contains a pointer to the Session:

type Database struct {
    Session *Session
    Name    string
}

And none of the other information would be invalidated by a session.Refresh().

Hope that helps! Dom