Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- use golang context pkg instead of gorilla/context to fix memory leaks #175

Merged
merged 3 commits into from Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions sessions.go
Expand Up @@ -5,12 +5,11 @@
package sessions

import (
"context"
"encoding/gob"
"fmt"
"net/http"
"time"

"github.com/gorilla/context"
)

// Default flashes key.
Expand Down Expand Up @@ -108,15 +107,16 @@ const registryKey contextKey = 0

// GetRegistry returns a registry instance for the current request.
func GetRegistry(r *http.Request) *Registry {
registry := context.Get(r, registryKey)
var ctx = r.Context()
registry := ctx.Value(registryKey)
if registry != nil {
return registry.(*Registry)
}
newRegistry := &Registry{
request: r,
sessions: make(map[string]sessionInfo),
}
context.Set(r, registryKey, newRegistry)
*r = *r.WithContext(context.WithValue(ctx, registryKey, newRegistry))
return newRegistry
secracon marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
31 changes: 31 additions & 0 deletions sessions_test.go
Expand Up @@ -153,6 +153,37 @@ func TestFlashes(t *testing.T) {
if custom.Type != 42 || custom.Message != "foo" {
t.Errorf("Expected %#v, got %#v", FlashMessage{42, "foo"}, custom)
}

// Round 5 ----------------------------------------------------------------
// Check if a request shallow copy resets the request context data store.

req, _ = http.NewRequest("GET", "http://localhost:8080/", nil)

// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}

// Put a test value into the session data store.
session.Values["test"] = "test-value"

// Create a shallow copy of the request.
req = req.WithContext(req.Context())

// Get the session again.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}

// Check if the previous inserted value still exists.
if session.Values["test"] == nil {
t.Fatalf("Session test value is lost in the request context!")
}

// Check if the previous inserted value has the same value.
if session.Values["test"] != "test-value" {
t.Fatalf("Session test value is changed in the request context!")
}
}

func TestCookieStoreMapPanic(t *testing.T) {
Expand Down