FAQ
I use http://code.google.com/p/go.net/context which looks very useful to
pass information around,
currently I pass pointers to databases and loggers with the Context object,
so each one of the http.HandlerFunc’s can access those.

Databases and loggers are injected to the Context in a wrapper function
that invokes an func(ctx context.Context, w http.ResponseWriter, r
*http.Request)

is it a good practice to do so or is there a better way of doing it ?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tahir at Sep 3, 2014 at 12:23 pm
    Hello,

    People with more experience than I do will probably give you the right
    answer.
    But afaic, I did not like the idea of having an object containing state on
    the main application server so what I did is create a context object per
    request that can be passed to all middleware handlers at will.
    To do that, I had to extend the basic http.HandlerFunc to be able to have
    http handler functions in the form of func(w http.ResponseWriter, r
    *http.Request, data map[string]interface{}).
    It is a bit more complicated than that because I had to abstract a few
    things but I should be releasing some code soon.

    All in all I do not have to lock the context object as it resides in its
    own goroutine corresponding to its own request thus I do not worry about
    another user making changes to it.
    I guess it's less worry about potential scaling issues.

    On the other hand, it means that I have to store/retrieve state in redis or
    similar whenever I need something as simple as a usercount.

    My 2 pennies
    On Wednesday, September 3, 2014 12:48:31 PM UTC+1, Gal Ben-Haim wrote:

    I use http://code.google.com/p/go.net/context which looks very useful to
    pass information around,
    currently I pass pointers to databases and loggers with the Context
    object, so each one of the http.HandlerFunc’s can access those.

    Databases and loggers are injected to the Context in a wrapper function
    that invokes an func(ctx context.Context, w http.ResponseWriter, r
    *http.Request)

    is it a good practice to do so or is there a better way of doing it ?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tahir at Sep 3, 2014 at 1:06 pm
    .. and then just realized you could ask me how do I pass my redis
    connection to all the handlers..

    I have created a type that embeds all connection objects..

    type netObject{
        *Redis.Instance
        *Mongo.Instance
        ...
    }

    (actually it embbeds all my middleware object types)
    And I store that object in the mux handler object as one of its field so I
    can have access to it in ServeHTTP. So only this object represents a global
    context for all requests.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tahir at Sep 4, 2014 at 1:21 pm
    Forget what i wrote... I was not clear.
    I think that what you are currently doing is fine.. if we were to speak in
    terms of patterns, this is dependency injection I believe. I think using a
    service locator pattern which just means having a big object composed of
    pointers to your various dB connections and retrieving from it via a method
    can be easier sometimes. That's what I was trying to say in the posts I
    wrote but it wasn't really clear nor concise at all.

    If I take the concrete example of loggers.. it can make the code a bit
    unruly if you inject the specific pointer systematically. I guess you have
    to deal with the type assertions too unless you have defined interfaces etc.

    That could help
    : http://martinfowler.com/articles/injection.html#UsingAServiceLocator

    (me "trying" to be clear..)


    On Wednesday, September 3, 2014 12:48:31 PM UTC+1, Gal Ben-Haim wrote:

    I use http://code.google.com/p/go.net/context which looks very useful to
    pass information around,
    currently I pass pointers to databases and loggers with the Context
    object, so each one of the http.HandlerFunc’s can access those.

    Databases and loggers are injected to the Context in a wrapper function
    that invokes an func(ctx context.Context, w http.ResponseWriter, r
    *http.Request)

    is it a good practice to do so or is there a better way of doing it ?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 3, '14 at 11:48a
activeSep 4, '14 at 1:21p
posts4
users2
websitegolang.org

2 users in discussion

Tahir: 3 posts Gal Ben-Haim: 1 post

People

Translate

site design / logo © 2021 Grokbase