Re: Mgo and thread safety

504 views
Skip to first unread message

Gustavo Niemeyer

unread,
Nov 16, 2012, 7:28:50 AM11/16/12
to lars....@gmail.com, mgo-...@googlegroups.com
Hi Lars,

On Fri, Nov 16, 2012 at 10:01 AM, Lars Pensjö <lars....@gmail.com> wrote:
> I am investigating the use of mgo for MongoDB, but I can't find information
> about thread-safety (goroutines). Can you point me somewhere to read more?
>
> That is, do you need a unique session for each thread? Will that be thread
> safe?

mgo is fully goroutine-safe, and you can share sessions across as
needed for your application. The only thing to keep in mind is that
when sharing a Strong or Monotonic session, a single underlying socket
is used to the database for all goroutines sharing that session. It
works fine, and that has both advantages and disadvantages depending
on the specific need.


gustavo @ http://niemeyer.net

Jim Robinson

unread,
May 21, 2013, 8:39:32 AM5/21/13
to mgo-...@googlegroups.com
Gustavo,

So if you were designing a library that needed  Strong consistency for
a handful of operations and Eventual consistency for the rest, would
you have your library dealing with two separate Sessions?   One a
copy of the other with the different mode set on it?  Is it lightweight
to copy a Session, or should one keep two Session open at once?

Gustavo Niemeyer

unread,
May 21, 2013, 10:00:15 AM5/21/13
to mgo-...@googlegroups.com
Hey Jim,

On Tue, May 21, 2013 at 9:39 AM, Jim Robinson <jim.ro...@gmail.com> wrote:
> So if you were designing a library that needed Strong consistency for
> a handful of operations and Eventual consistency for the rest, would
> you have your library dealing with two separate Sessions? One a
> copy of the other with the different mode set on it? Is it lightweight
> to copy a Session, or should one keep two Session open at once?

I would use just a single session, and copy it whenever I need a
session with different properties for a handful of operations.

Except for Dial, creating sessions via any other means is very
lightweight as it reuses the same pool of resources.


gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages