You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,20 +89,24 @@ func main() {
89
89
90
90
What we have now is a singleton `mongoConn` that can be used directly which is not a good idea. Why create it at all? Why cannot we establish a connection every time the client calls our endpoints?
91
91
92
-
Because `mgo.DialWithInfo(...)` can take several seconds before the connection to the MongoDB Atlas is ready. There is a couple of necessary steps like sending and accepting certificates, authorization etc. that needs to be done, before your service can proceed with the next step.
92
+
Because `mgo.DialWithInfo(...)` can take several seconds before the connection to the MongoDB Atlas is ready. There is a couple of necessary steps like sending and accepting certificates, authorization etc. that needs to be done, before your service can proceed to the next step.
93
93
94
94
And of course you cannot use the singleton `mongoConn` in all your endpoints for an obvious reason (due to the side effects by using of a common connection in concurrent HTTP sessions).
95
95
96
-
So, we use a copy of the singleton `mongoConn` which works perfect:
96
+
So, we use a copy of the singleton `mongoConn` which works quick and safe:
97
97
98
98
```
99
99
session := mongoConn.Copy() // "session" can be used safely
100
100
defer session.Close()
101
101
```
102
102
103
-
Let us implement `/save` and `/read` now. We store the data in a sort of generic way: everything what the client sends us, we are going to save into the Mongo database as a byte array.
103
+
Let us implement `/save` and `/read` now. We store the data in a sort of generic way: everything what the client app sends us we are going to save into the Mongo database as a byte array.
_Normally you would not want to `panic` in case of an error, but return an HTTP error code in the response. However, we want to keep it simple for now._
138
142
139
-
Do not forget to close a copy of your session. MongoDB Atlas considers sessions as a resource and like every database server has a limit of opened connections.
143
+
Do not forget to close a copy of your session. MongoDB Atlas considers sessions as a resource and like every database server has a limit for the amount of opened connections.
0 commit comments