Documentation ¶
Index ¶
- func ContextWithSession(ctx context.Context, session *Session) context.Context
- type Cache
- type Codec
- type MockCache
- func (c *MockCache) GetSessionUse(session SessionExt)
- func (c *MockCache) GetSessionUseByID(builder SessionBuilder, id, name string) *Session
- func (c *MockCache) MarkOrDestroySessionByID(id string)
- func (c *MockCache) MarkSession(session SessionExt)
- func (c *MockCache) PutSessionUse(session SessionExt)
- func (c *MockCache) SaveSession(session SessionExt) (bool, error)
- func (c *MockCache) UseCountFor(id string) int
- type MockCodec
- type Session
- func (s *Session) Drop(key interface{})
- func (s *Session) Get(key interface{}) interface{}
- func (s *Session) Has(key interface{}) bool
- func (s *Session) ID() string
- func (s *Session) Mark()
- func (s *Session) Name() string
- func (s *Session) Save(writer http.ResponseWriter) error
- func (s *Session) Set(key, value interface{})
- type SessionBuilder
- type SessionExt
- type Store
- type TestHarness
- type ValuesType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache interface { // GetSessionUse should increase some use count on data the // passed session refers to. GetSessionUse(session SessionExt) // GetSessionUseByID should use the passed builder to create a // session object with a use count on the data incremented by // one. GetSessionUseByID(builder SessionBuilder, id, name string) *Session // PutSessionUse should decrement the use count on data and // destroy the data if the count drops to zero and the session // was marked for destruction. PutSessionUse(session SessionExt) // MarkOrDestroySessionByID should mark the data for // destruction. If data's use count is zero, it should be // destroyed. MarkOrDestroySessionByID(id string) // MarkSession marks the session for destruction. Data // shouldn't be destroyed since the existence of the passed // session object means that the data is still used. MarkSession(session SessionExt) // SaveSession should persist the changes made in the session // object to the cache. SaveSession(session SessionExt) (bool, error) }
Cache stores the refcounted session information. This type shouldn't be used by libraries - it's only for Store and for implementations of this interface.
type Codec ¶
type Codec interface { // Decode should return an ID from the passed name and cookie // value. Decode(name, value string) (string, error) // Encode should return a cookie value from the passed name // and ID. Encode(name, id string) (string, error) }
Codec decodes cookie ID from the name and value pair, and encodes cookie value from name and id pair. This type is used by the store.
type MockCache ¶
type MockCache struct {
// contains filtered or unexported fields
}
MockCache is an implementation of Cache interface used for mocking.
func (*MockCache) GetSessionUse ¶
func (c *MockCache) GetSessionUse(session SessionExt)
GetSessionUse is a part of Cache interface.
func (*MockCache) GetSessionUseByID ¶
func (c *MockCache) GetSessionUseByID(builder SessionBuilder, id, name string) *Session
GetSessionUseByID is a part of Cache interface.
func (*MockCache) MarkOrDestroySessionByID ¶
MarkOrDestroySessionByID is a part of Cache interface.
func (*MockCache) MarkSession ¶
func (c *MockCache) MarkSession(session SessionExt)
MarkSession is a part of Cache interface.
func (*MockCache) PutSessionUse ¶
func (c *MockCache) PutSessionUse(session SessionExt)
PutSessionUse is a part of Cache interface.
func (*MockCache) SaveSession ¶
func (c *MockCache) SaveSession(session SessionExt) (bool, error)
SaveSession is a part of Cache interface.
func (*MockCache) UseCountFor ¶
UseCountFor returns a use count of the session with a passed ID, or -1 the session is not in cache.
type MockCodec ¶
type MockCodec struct {
// contains filtered or unexported fields
}
MockCodec is an implementation of Codec used for mocking.
func (*MockCodec) AddIDValueMapping ¶
AddIDValueMapping adds a mapping from ID to cookie value and back. Not adding mapping will result in an error when encoding or decoding.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is a snapshot of the session data stored in the cache.
func SessionFromContext ¶
SessionFromContext returns the last session stored in the context. Returns nil if there is no session.
func (*Session) Get ¶
func (s *Session) Get(key interface{}) interface{}
Get returns a value mapped to the key, or nil.
func (*Session) Save ¶
func (s *Session) Save(writer http.ResponseWriter) error
Save persists the information stored in the session object to the cache associated with the object, and updates the cookie header in the response.
TODO: Split the writing the cookie into a separate function, like WriteCookie.
TODO: Add support for options, so we don't hardcode the max age value here. The support should probably be reflected in the Cache interface itself and in Session.
type SessionBuilder ¶
type SessionBuilder interface { // NewSession creates a new session object with a given name, and // associates it with the passed cache. The created cache has // no ID. NewSession(name string, cache Cache) SessionExt // NewExistingSession created a session object with passed // name, id and values map, and associates it with the passed // cache. NewExistingSession(name, id string, values ValuesType, cache Cache) SessionExt }
SessionBuilder is an interface purely for implementation of the Cache interface, so they can create new session objects.
type SessionExt ¶
type SessionExt interface { // Name returns a name of the session. Name() string // ID returns an ID of the session. ID() string // SetID updates an ID of the session. SetID(id string) // GetValues returns the values map. GetValues() ValuesType // Session returns the underlying session object. Session() *Session }
SessionExt is an interface purely for implementations of the Cache interface, so they can change some data in the session that typical user of the session shouldn't be allowed to do.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store contains cache and codec for session management.
func (*Store) GetSessionUse ¶
GetSessionUse tries to either retrieve the session from either context or request, or create a new one. The returned session has its use count increased, so is safe from being destroyed until PutSessionUse is called.
func (*Store) MarkOrDestroySessionByID ¶
MarkOrDestroySessionByID marks the session with a passed ID for destruction. If the session is not currently in use by any request, the session should be destroyed immediately.
func (*Store) PutSessionUse ¶
PutSessionUse decreases the use count of the session and potentially destroys it, if it was marked.
type TestHarness ¶
type TestHarness struct { // T is just testing.T. T *testing.T // NewCache should return a Cache set in such a way, that the // new IDs for session should be in form of "idN", where N // grows from 1. NewCache func() Cache // UseCountFor should return a use count of a session with a // given ID, or -1 if the ID is not in cache. UseCountFor func(cache Cache, id string) int }
TestHarness contains a setup for running session tests.
func (*TestHarness) RunBasicSessionLifecycleTests ¶
func (h *TestHarness) RunBasicSessionLifecycleTests()
RunBasicSessionLifecycleTests tests creation and destruction of sessions, and the use count changes
func (*TestHarness) RunDeadCookiesTests ¶
func (h *TestHarness) RunDeadCookiesTests()
RunDeadCookiesTests tests the behaviour of the cache for marked and destroyed cookies.
type ValuesType ¶
type ValuesType map[interface{}]interface{}
ValuesType is a type of values container used in the session.