inconsistencies between gridfs and document behavior

8 views
Skip to first unread message

Darko Luketic

unread,
Dec 9, 2019, 9:48:57 AM12/9/19
to mongodb-go-driver
In the gridfs package you can use
cursor, err := bucket.Find(filter, opts)

when searching for documents there is a context attached, in gridfs there is none.

should we just use

defer cur.Close(context.Background())

for cur.Next(context.Background()) {

}


?

Divjot Arora

unread,
Dec 11, 2019, 10:22:06 AM12/11/19
to mongodb-go-driver
Hi Darko,

The gridfs implementation in the driver does not support contexts because we wanted the upload and download streams to implement the io.Writer and io.Reader interfaces, respectively. Because of this, we copied the os package's timeout mechanism. You can use the Bucket.SetWriteDeadline and Bucket.SetReadDeadline methods before executing an operation on the Bucket to set a timeout. Be sure to clear the deadline using SetReadDeadline(time.Time{}) after the operation has been executed. If it is not cleared, the same deadline will apply to all future operations as well. Once the Find has been executed and you have a Cursor, you can use contexts when calling Cursor.Next and Cursor.Close, as you have done in your example code.
Reply all
Reply to author
Forward
0 new messages