1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| // Load retrieves the session data for the given token from the session store, // and returns a new context.Context containing the session data. If no matching // token is found then this will create a new session. // // Most applications will use the LoadAndSave() middleware and will not need to // use this method. func (s *SessionManager) Load(ctx context.Context, token string) (context.Context, error) {
// A Context carries a deadline, a cancellation signal, and other request-scope values across API boundaries. // Context's methods may be called by multiple goroutines simultaneously.
// contextKey is the key used to set and retrieve the session data from a context.Context. It's automatically generated to ensure uniqueness.
// .(*sessionData) is a type assertion, similar to the 'as' keyword in C# // If the session is already in the context, just return the context and nothing needs to be done. if _, ok := ctx.Value(s.contextKey).(*sessionData); ok { return ctx, nil }
// If the token is empty, simply add a new, empty session data to the context. if token == "" { return s.addSessionDataToContext(ctx, newSessionData(s.Lifetime)), nil }
// Find should return the data for a session token from the store, in byte array format b, found, err := s.Store.Find(token) if err != nil { return nil, err } else if !found { // If no session data with the given token is found in the store, add empty session data to the context. return s.addSessionDataToContext(ctx, newSessionData(s.Lifetime)), nil }
sd := &sessionData{ status: Unmodified, token: token, } // Decode the byte array found from the store if sd.deadline, sd.values, err = s.Codec.Decode(b); err != nil { return nil, err }
// Mark the session data as modified if an idle timeout is being used. This // will force the session data to be re-committed to the session store with // a new expiry time. if s.IdleTimeout > 0 { sd.status = Modified }
return s.addSessionDataToContext(ctx, sd), nil }
|