1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 06:00:55 +00:00

Update sess_file.go

Lock required to ensure the File sessions work correct.
This commit is contained in:
Francois 2014-02-27 15:34:38 +02:00
parent edb8bac5bc
commit adf2a590fc

View File

@ -81,6 +81,7 @@ func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
// File session provider // File session provider
type FileProvider struct { type FileProvider struct {
lock sync.RWMutex
maxlifetime int64 maxlifetime int64
savePath string savePath string
} }
@ -97,6 +98,9 @@ func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
// if file is not exist, create it. // if file is not exist, create it.
// the file path is generated from sid string. // the file path is generated from sid string.
func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) { func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
filepder.lock.Lock()
defer filepder.lock.Unlock()
err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777) err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
if err != nil { if err != nil {
println(err.Error()) println(err.Error())
@ -133,6 +137,9 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
// Check file session exist. // Check file session exist.
// it checkes the file named from sid exist or not. // it checkes the file named from sid exist or not.
func (fp *FileProvider) SessionExist(sid string) bool { func (fp *FileProvider) SessionExist(sid string) bool {
filepder.lock.Lock()
defer filepder.lock.Unlock()
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid)) _, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
if err == nil { if err == nil {
return true return true
@ -143,12 +150,18 @@ func (fp *FileProvider) SessionExist(sid string) bool {
// Remove all files in this save path // Remove all files in this save path
func (fp *FileProvider) SessionDestroy(sid string) error { func (fp *FileProvider) SessionDestroy(sid string) error {
filepder.lock.Lock()
defer filepder.lock.Unlock()
os.Remove(path.Join(fp.savePath)) os.Remove(path.Join(fp.savePath))
return nil return nil
} }
// Recycle files in save path // Recycle files in save path
func (fp *FileProvider) SessionGC() { func (fp *FileProvider) SessionGC() {
filepder.lock.Lock()
defer filepder.lock.Unlock()
gcmaxlifetime = fp.maxlifetime gcmaxlifetime = fp.maxlifetime
filepath.Walk(fp.savePath, gcpath) filepath.Walk(fp.savePath, gcpath)
} }
@ -170,6 +183,9 @@ func (fp *FileProvider) SessionAll() int {
// Generate new sid for file session. // Generate new sid for file session.
// it delete old file and create new file named from new sid. // it delete old file and create new file named from new sid.
func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
filepder.lock.Lock()
defer filepder.lock.Unlock()
err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777) err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777)
if err != nil { if err != nil {
println(err.Error()) println(err.Error())