globalsign / mgo

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

Need Help: MongoDB changeStream ends immediately after opening #315

Closed anshap1719 closed 5 years ago

anshap1719 commented 5 years ago

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

db version v4.0.1
git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

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

go version go1.11 darwin/amd64

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

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

What did you do?

Here's the code I used to watch for changes and push the result to a go channel:

cs, err := r.users.Watch([]bson.M{}, mgo.ChangeStreamOptions{FullDocument: mgo.FullDocument("updateLookup")})
    if err != nil {
        fmt.Println(err)
        raven.CaptureError(fmt.Errorf("unable to watch for changes %s", err), nil)
        return err
    }
for cs.Next(&change) {
        byts, _ := bson.Marshal(change["fullDocument"].(bson.M))
        bson.Unmarshal(byts, &userDoc)
        userDoc.ID = bson.ObjectId(userDoc.ID).Hex()
        fmt.Printf("Change: %v\n", userDoc)
        fmt.Println("Sending value to channel")
        *userChan <- userDoc
        fmt.Println("value sent")
    }

This channel recieves the value and pushes it to client via websocket. That part is working fine. But the for loop doesn't stay open for even a few seconds.

Any way to keep the cursor open until an error occurs or the server closes?

rwynn commented 5 years ago

Hi @anshap1719 not sure if you figured this out already. When Next returns false you can call it again if cs.Timeout() returns true. Here is an example.

There is also MaxAwaitTimeMS on the options if you don't want it to time out so quickly.

Some other things to consider are calling Close on the change stream if it didn't time out. And possibly saving the cs.ResumeToken in case you need to resume a broken stream without losing events.

eminano commented 5 years ago

@rwynn thanks a lot for helping out! @anshap1719 I'll close this issue now.

Cheers, Esther