What context type should be used when closing a cursor? I’ve seen examples where the context used for finding the documents is also used for closing the cursor, e.g.:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cur, err := collection.Find(ctx, filter, findOptions)
if err != nil {
return err
}
defer cur.Close(ctx)
...
But what would happen, if the cursor expires/times out shortly before or during closing? Would the cursor still be closed and the associated resources freed? I also found this comment inside the driver code:
func (c *Cursor) All(ctx context.Context, results interface{}) error {
...
// Defer a call to Close to try to clean up the cursor server-side when all
// documents have not been exhausted. Use context.Background() to ensure Close
// completes even if the context passed to All has errored.
defer c.Close(context.Background())
...
}
So, why would the API even require a context to be passed in? Any help or hints are highly appreciated.