session

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2025 License: MIT Imports: 8 Imported by: 0

README

simple session manager and providers for go http server

Documentation

Overview

Package session (session manager) control process session lifetime create, update and destroy

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MilisPerSecDuration

func MilisPerSecDuration() time.Duration

MilisPerSec return time resolution (milliseconds / 1sec) changed for short time in testing

func ProviderNames

func ProviderNames() []string

ProviderNames return slice of strings - registered Provider names

func Register

func Register(name string, provide Provider)

Register makes a session provide available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager controls all sessions with registered storage provider

func NewManager

func NewManager(providerName string, sopts *SessOpts, adopts any) (manager *Manager, err error)

NewManager create new *Manager using SesOpts and aditional any other opts for using in provider

func (*Manager) Exists

func (manager *Manager) Exists(sid string) bool

Exists return true if session with sid exists on server

func (*Manager) GC

func (manager *Manager) GC()

GC remove sessions which exceeded manager.maxLifetime

func (*Manager) RegenerateID

func (manager *Manager) RegenerateID(w http.ResponseWriter, r *http.Request)

RegenerateID vhange sid and preserve all session data

func (*Manager) SessionDestroy

func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) (err error)

SessionDestroy end session and delete session data at the server

func (*Manager) SessionStart

func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session Session, err error)

SessionStart start session for next http response

type Provider

type Provider interface {
	//set additional params for provider ex: sql db connection, filesystem path .. etc.
	SetParams(params any) error
	//create new session using sid value
	Init(sid string) (Session, error)
	//read and return existing session by id or if not exist create new session
	Load(sid string) (Session, error)
	//destroy remove session with sid from storage if exist
	Destroy(sid string) error
	//regenerate id change old sid to newsid and preserve existing session data
	ChangeID(oldsid, newsid string) (err error)
	//Exists return true if session with sid exist
	Exists(sid string) bool
	//gc remove all outdated sessions
	GC(maxlifetime int64)
}

Provider interace implement lifecycle for one session

type SessOpts

type SessOpts struct {
	SidLen      uint8
	CookieName  string
	MaxLifetime int64
	Ssl         bool
}

SessOpts set session options or NewManagerh

type Session

type Session interface {
	//set session value and update last access time
	Set(key, value any) error
	//get session value and update last access time
	Get(key any) (v any, err error)
	//delete session value
	Delete(key any) error
	//get session id
	SessionID() string
}

Session interface implement storage for one session and have maxLifetime and lastAccessTime

Directories

Path Synopsis
storage
files
Package files implements sessions saved into filesystem persistently encoded using gob
Package files implements sessions saved into filesystem persistently encoded using gob
hybrid
Package hybrid is session storage that use RAM and provider modification log (PML) for very fast efecient session operations and filsystem saving using SPML save session data into filesystem lazy save.
Package hybrid is session storage that use RAM and provider modification log (PML) for very fast efecient session operations and filsystem saving using SPML save session data into filesystem lazy save.
memory
Package memory implements sessions saved into memory is gone when stop the server
Package memory implements sessions saved into memory is gone when stop the server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL