general purpose request context (based on brad's idea)

52 views
Skip to first unread message

Rodrigo Moraes

unread,
Aug 29, 2011, 10:55:22 AM8/29/11
to google-appengine-go
Hey,

I created a package to store global variables used during a request.
Specifically, variables set by 3rd party packages that must be
available somewhere during a request. It is based on a message by Brad
Fitzpatrick to the go-nuts mailing list:

http://groups.google.com/group/golang-nuts/msg/e2d679d303aa5d53

To that idea, it adds the concept of "Namespace", which is a user/
package-defined "bucket" for a given variable. So instead of
map[*http.Request]interface{}, the context stores map[*http.Request]
[Namespace]interface{}. It is explained here:

http://gopkgdoc.appspot.com/pkg/github.com/moraes/gorilla/src/gorilla/context

I'd like to get some feedback about it, and specially would like to
know if you think I'm doing something bad there.

(this is kinda cross-posted from go-nuts; I was still not sure where
it was going when I posted it :)

-- rodrigo

Brad Fitzpatrick

unread,
Aug 29, 2011, 11:52:40 AM8/29/11
to Rodrigo Moraes, google-appengine-go
If we can think of some good names, I wouldn't mind promoting some of this into the http package itself.

In particular, the need to explicitly clear the context seems a little error prone.  The http package could guarantee such cleanup.

Or maybe just a way to register a per-*http.Request cleanup func with the http package:

package http

// RegisterCleanup registers a function to be called after the http request is finished.
// This provides a reliable place for other layers doing per-Request storage to do
// cleanup.
func (r *Request) RegisterCleanup(fn func()) {
   ...
}

That might be sufficient for now?

Rodrigo Moraes

unread,
Aug 29, 2011, 12:36:05 PM8/29/11
to google-appengine-go
On Aug 29, 12:52 pm, Brad Fitzpatrick wrote:
> In particular, the need to explicitly clear the context seems a little error
> prone.  The http package could guarantee such cleanup.

Well, that would make the namespace idea irrelevant, and this is a
good thing; simply use different contexts and each one registers its
own cleanup function. I only added the "namespaces" support to be able
to call Clear() once and drop all contexts.

> // RegisterCleanup registers a function to be called after the http request
> is finished.
> // This provides a reliable place for other layers doing per-Request storage
> to do
> // cleanup.
> func (r *Request) RegisterCleanup(fn func()) {
>    ...
>
> }
>
> That might be sufficient for now?

I think so. :)

-- rodrigo
Reply all
Reply to author
Forward
0 new messages