c358c180 by astaxie

Merge pull request #511 from francoishill/patch-2

Update sess_file.go
2 parents edb8bac5 adf2a590
...@@ -81,6 +81,7 @@ func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) { ...@@ -81,6 +81,7 @@ func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
81 81
82 // File session provider 82 // File session provider
83 type FileProvider struct { 83 type FileProvider struct {
84 lock sync.RWMutex
84 maxlifetime int64 85 maxlifetime int64
85 savePath string 86 savePath string
86 } 87 }
...@@ -97,6 +98,9 @@ func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error { ...@@ -97,6 +98,9 @@ func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
97 // if file is not exist, create it. 98 // if file is not exist, create it.
98 // the file path is generated from sid string. 99 // the file path is generated from sid string.
99 func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) { 100 func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
101 filepder.lock.Lock()
102 defer filepder.lock.Unlock()
103
100 err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777) 104 err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
101 if err != nil { 105 if err != nil {
102 println(err.Error()) 106 println(err.Error())
...@@ -133,6 +137,9 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) { ...@@ -133,6 +137,9 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
133 // Check file session exist. 137 // Check file session exist.
134 // it checkes the file named from sid exist or not. 138 // it checkes the file named from sid exist or not.
135 func (fp *FileProvider) SessionExist(sid string) bool { 139 func (fp *FileProvider) SessionExist(sid string) bool {
140 filepder.lock.Lock()
141 defer filepder.lock.Unlock()
142
136 _, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid)) 143 _, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
137 if err == nil { 144 if err == nil {
138 return true 145 return true
...@@ -143,12 +150,18 @@ func (fp *FileProvider) SessionExist(sid string) bool { ...@@ -143,12 +150,18 @@ func (fp *FileProvider) SessionExist(sid string) bool {
143 150
144 // Remove all files in this save path 151 // Remove all files in this save path
145 func (fp *FileProvider) SessionDestroy(sid string) error { 152 func (fp *FileProvider) SessionDestroy(sid string) error {
153 filepder.lock.Lock()
154 defer filepder.lock.Unlock()
155
146 os.Remove(path.Join(fp.savePath)) 156 os.Remove(path.Join(fp.savePath))
147 return nil 157 return nil
148 } 158 }
149 159
150 // Recycle files in save path 160 // Recycle files in save path
151 func (fp *FileProvider) SessionGC() { 161 func (fp *FileProvider) SessionGC() {
162 filepder.lock.Lock()
163 defer filepder.lock.Unlock()
164
152 gcmaxlifetime = fp.maxlifetime 165 gcmaxlifetime = fp.maxlifetime
153 filepath.Walk(fp.savePath, gcpath) 166 filepath.Walk(fp.savePath, gcpath)
154 } 167 }
...@@ -170,6 +183,9 @@ func (fp *FileProvider) SessionAll() int { ...@@ -170,6 +183,9 @@ func (fp *FileProvider) SessionAll() int {
170 // Generate new sid for file session. 183 // Generate new sid for file session.
171 // it delete old file and create new file named from new sid. 184 // it delete old file and create new file named from new sid.
172 func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { 185 func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
186 filepder.lock.Lock()
187 defer filepder.lock.Unlock()
188
173 err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777) 189 err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777)
174 if err != nil { 190 if err != nil {
175 println(err.Error()) 191 println(err.Error())
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!