On Fri, Nov 1, 2013 at 8:22 PM, William Kennedy <
bi...@thekennedyclan.net> wrote:
>>
>> No problem. I just wanted to make sure you knew that the documentation
>> actually covers these aspects as well.
>
> The documentation really doesn't explain how to use sessions:
Sorry, but the documentation does answer the original question you
had, pretty well in fact.
Question:
"""
For every request I plan on making a clone of an existing session I
create at startup. I am hoping clone reuses the socket pool instead
of creating a new one like copy does?
I am assuming this is the correct way to use the pool, per web session?
"""
Excerpt of documentation for mgo.Dial (
http://labix.org/v2/mgo#Dial):
"""
This method is generally called just once for a given cluster. Further
sessions to the same cluster are then established using the New or
Copy methods on the obtained session. This will make them share the
underlying cluster, and manage the pool of connections appropriately.
"""
Documentation for Session.New (
http://labix.org/v2/mgo#Session.New):
"""
New creates a new session with the same parameters as the original
session, including consistency, batch size, prefetching, safety mode,
etc. The returned session will use sockets from the pool, so there's a
chance that writes just performed in another session may not yet be
visible.
Login information from the original session will not be copied over
into the new session unless it was provided through the initial URL
for the Dial function.
See the Copy and Clone methods.
"""
Documentation for Session.Copy (
http://labix.org/v2/mgo#Session.Copy):
"""
Copy works just like New, but preserves the exact authentication
information from the original session.
"""
Documentation for Session.Clone (
http://labix.org/v2/mgo#Session.Clone):
"""
Clone works just like Copy, but also reuses the same socket as the
original session, in case it had already reserved one due to its
consistency guarantees. This behavior ensures that writes performed in
the old session are necessarily observed when using the new session,
as long as it was a strong or monotonic session. That said, it also
means that long operations may cause other goroutines using the
original session to wait.
"""
> If I have multiple routines running operations at the same time, like in my web service:
> Should I create a master session and then clone the session for each routine/web request, perform the operations and close the session?
> Is it safe to create one session and use it for all routines/web requests?
Yes, it's safe. If you are using a session in Strong or Monotonic
modes, this will mean all the goroutines will share a single socket,
per the documentation (
http://labix.org/v2/mgo#Session.SetMode).
> You see I am very confused about sessions and when to copy or clone them in a multi routine program like a web service.
Sure, I can see that you are missing a prescribed way to do it. Often
this kind of application will have a master session and then use a
Copy of it for each request and then Close at the end. Feel free to
change that prescription to meet your intention, though.
>> It's expected that you might see additional authentication requests if
>> you're using several sockets rather than a single one.
>
> I was wrong, I saw all the authenticate calls when I was using refresh. I think I see connections being accepts and closed with session copies.
I cannot tell what you're actually doing there or referring to, but if
you're suggesting that copying sessions means not using a connection
pool, that is false.
gustavo @
http://niemeyer.net