Opening a mp4 video from GridFS leads to excessive memory usage after a few concurrent calls. The video in question is 43276570 bytes in size. I mention video because it was the scenario that caused me to notice the problem initially, however I think it's fair to assume the results will be the same regardless of what content you are loading.
It wasn't hard to spot something was going on since I noticed 100MiB+ of memory was being used after opening the video twice in different browser tabs. To get a feel for the gravity of the issue I ran a siege benchmark with 50 concurrent users.
siege -c 50 -r 1 'http://localhost:8080/'
After running this benchmark three times the server memory usage caps out at around 1.9GiB of memory according to the gnome-system-monitor. Afterwards you can take a look at the allocation profile of the example server. It's easy to spot that there is a lot of overhead in the readWireMessage function.
go tool pprof http://localhost:8080/debug/pprof/allocs
(pprof) list readWireMessage
{ driver: { name: "mongo-go-driver", version: "v1.1.1+prerelease" }, os: { type: "linux", architecture: "amd64" }, platform: "go1.13" }
I hardcoded the ID of the content that lead to the initial discovery to keep the example as simple as possible, if you want to test the code for yourself change the ID to whatever content you have available.
The download stream is closed by the defer on https://gist.github.com/RDux/1c9eb8c2a3a75b94f2f9f8d19ff90f9a#file-example-go-L61. I can't share the original video but you can test with one of these: https://download.blender.org/peach/bigbuckbunny_movies/. I tested against:
Change line 62 to
video/ogg
instead ofvideo/mp4
. With the same benchmark against this video the example code sits at 2.0GiB in the system monitor.