[GODRIVER-1031] mongo.Connect takes a context for no purpose Created: 09/May/19 Updated: 16/Nov/21 Resolved: 09/May/19 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | Connections, Documentation |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Quest Henkart | Assignee: | Kristofer Brandow (Inactive) |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
The mongo.Connect function https://godoc.org/go.mongodb.org/mongo-driver/mongo#Connect takes a context. However it does not seem to do anything with it: https://github.com/mongodb/mongo-go-driver/blob/master/mongo/client.go#L101
By taking context, it leads one to believe that the connect method should be request-scoped as per Golang's official documentation https://golang.org/pkg/context/ . Instead, according to mongo's tutorial https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial it says
You can see how the documentation and the use of context creates a really confusing understanding of where the client.Connect() method is supposed to live and how it's supposed to be handled.
First I realized that using client.Connect() in a request scope will cause an error because there can be multiple requests and client.Connect() returns an error if the client is already connected. Then I looked at the source code and realized context isn't actually being used. Now I connect once with an ad-hoc context, and share that connection for all requests. I believe that is the correct usage. But the documentation and tutorial does not make it clear
Request: Connect should not take a context unless it is supposed to be request scoped and unless it is being utilized in someway. Documentation and tutorials should make it more clear where an app should Connect |
| Comments |
| Comment by Kristofer Brandow (Inactive) [ 09/May/19 ] |
|
Hi qhenkart, While it is true that the context.Context is currently unused in the API having a context.Context provided to a method like Connect is correct usage because the context.Context type is not used just for request scoped, as stated in the documentation for the context package: "Package context defines the Context type, which carries deadlines, cancelation signals, and other request-scoped values across API boundaries and between processes". In this case, if Connect was synchronous and did network I/O then it would be appropriate to have a context.Context for deadlines and cancellation signals. |