Documentation
¶
Index ¶
- Constants
- Variables
- type Manager
- func (m *Manager) AcquireSession() *Session
- func (m *Manager) BuildSession(name string, driver ...string) (*Session, error)
- func (m *Manager) Close() error
- func (m *Manager) Extend(name string, handler driver.Driver) error
- func (m *Manager) GetSession(r *http.Request, key ...any) (*Session, error)
- func (m *Manager) HasSession(r *http.Request, key ...any) bool
- func (m *Manager) LockSession(id string)
- func (m *Manager) Logger() *slog.Logger
- func (m *Manager) ReleaseSession(session *Session)
- func (m *Manager) UnlockSession(id string)
- type ManagerOptions
- type Session
- func (s *Session) All() map[string]any
- func (s *Session) Exists(key string) bool
- func (s *Session) Flash(key string, value any) *Session
- func (s *Session) Flush() *Session
- func (s *Session) Forget(keys ...string) *Session
- func (s *Session) Get(key string, defaultValue ...any) any
- func (s *Session) GetID() string
- func (s *Session) GetName() string
- func (s *Session) Has(key string) bool
- func (s *Session) Invalidate() error
- func (s *Session) IsDirty() bool
- func (s *Session) IsStarted() bool
- func (s *Session) Keep(keys ...string) *Session
- func (s *Session) Missing(key string) bool
- func (s *Session) Now(key string, value any) *Session
- func (s *Session) Only(keys []string) map[string]any
- func (s *Session) Pull(key string, def ...any) any
- func (s *Session) Put(key string, value any) *Session
- func (s *Session) Reflash() *Session
- func (s *Session) Regenerate(destroy ...bool) error
- func (s *Session) Remove(key string) any
- func (s *Session) Save() error
- func (s *Session) SetID(id string) *Session
- func (s *Session) SetName(name string) *Session
- func (s *Session) Start() bool
Constants ¶
const ( // DefaultLifetime is used when ManagerOptions.Lifetime is not positive. DefaultLifetime = 120 // minutes // DefaultGcInterval is used when ManagerOptions.GcInterval is not positive. DefaultGcInterval = 30 // minutes )
Variables ¶
var ( CtxKey = "session" // session default context key CookieName = "session" // session default cookie name )
var ( ErrSessionNotFound = errors.New("session not found") ErrDriverNotSet = errors.New("driver is not set") ErrDriverNotSupported = errors.New("driver not supported") ErrDriverExists = errors.New("driver already exists") )
Sentinel errors returned by the Manager; test with errors.Is.
var ErrSessionDestroyed = errors.New("session destroyed concurrently")
ErrSessionDestroyed reports that a session which existed when the request started was destroyed concurrently (logout, Regenerate(true) or garbage collection in another request) before Save could persist it. The save is refused: recreating the session would resurrect an invalidated ID.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
Codec securecookie.Codec
Lifetime int
GcInterval int
// contains filtered or unexported fields
}
func NewManager ¶
func NewManager(option *ManagerOptions) (*Manager, error)
NewManager creates a new session manager.
func (*Manager) AcquireSession ¶
AcquireSession takes a session from the pool.
func (*Manager) BuildSession ¶
BuildSession acquires a pooled session bound to the given driver. Release it with ReleaseSession when the request is done.
func (*Manager) Close ¶ added in v1.5.0
Close stops the garbage collection timers and closes all registered drivers. It is idempotent.
func (*Manager) Extend ¶
Extend registers a custom driver under the given name and starts its garbage collection timer. It is safe for concurrent use.
func (*Manager) GetSession ¶
GetSession returns the session stored in the request context, keyed by CtxKey unless a custom key is given.
func (*Manager) HasSession ¶
HasSession reports whether the request context carries a session.
func (*Manager) LockSession ¶ added in v1.3.0
LockSession locks the given session ID so that concurrent saves of the same session are serialized.
func (*Manager) ReleaseSession ¶
ReleaseSession resets the session and returns it to the pool. The session must not be used afterwards.
func (*Manager) UnlockSession ¶ added in v1.3.0
UnlockSession releases the lock for the given session ID.
type ManagerOptions ¶
type ManagerOptions struct {
// Key is the 32 bytes string used to encrypt session data.
Key string
// Lifetime is the session lifetime in minutes. Defaults to DefaultLifetime.
Lifetime int
// GcInterval is the session garbage collection interval in minutes.
// Defaults to DefaultGcInterval.
GcInterval int
// DisableDefaultDriver disables the default file driver if set to true.
DisableDefaultDriver bool
// Logger receives background errors (garbage collection, middleware
// saves). Defaults to slog.Default().
Logger *slog.Logger
}
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session holds the data of a single session for the duration of a request. It is not safe for concurrent use by multiple goroutines.
func (*Session) All ¶
All returns a copy of the session attributes. Mutating the returned map does not affect the session; use Put and Forget to modify it.
func (*Session) Flash ¶
Flash stores a key/value pair that survives until the end of the next request, then is removed automatically.
func (*Session) Get ¶
Get returns the value for key, or defaultValue (or nil) when the key is missing.
func (*Session) Invalidate ¶
Invalidate flushes all attributes and regenerates the session ID, destroying the previously stored session.
func (*Session) Now ¶
Now stores a key/value pair that is removed at the end of the current request.
func (*Session) Regenerate ¶
Regenerate gives the session a new ID. Pass true to also destroy the previously stored session data.
func (*Session) Save ¶
Save persists the session to the driver and marks it as no longer started. Concurrent saves of the same session ID are merged key by key: only the keys written or forgotten during this request are applied on top of the latest stored state.
func (*Session) SetID ¶
SetID sets the session ID. Invalid IDs (wrong length or characters outside [0-9A-Za-z]) are replaced with a newly generated one.