|
@@ -1,6 +1,7 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"time"
|
|
@@ -10,18 +11,26 @@ import (
|
|
|
"github.com/vladimirok5959/golang-server-sessions/session"
|
|
|
)
|
|
|
|
|
|
-func session_clean_start(www_dir string) chan bool {
|
|
|
+func session_clean_start(www_dir string) (chan bool, chan bool) {
|
|
|
ch := make(chan bool)
|
|
|
+ stop := make(chan bool)
|
|
|
go func() {
|
|
|
for {
|
|
|
+ // Destroy old session files on each host
|
|
|
+ // Every one hour
|
|
|
select {
|
|
|
case <-time.After(1 * time.Hour):
|
|
|
files, err := ioutil.ReadDir(www_dir)
|
|
|
if err == nil {
|
|
|
for _, file := range files {
|
|
|
- tmpdir := www_dir + string(os.PathSeparator) + file.Name() + string(os.PathSeparator) + "tmp"
|
|
|
- if utils.IsDirExists(tmpdir) {
|
|
|
- session.Clean(tmpdir)
|
|
|
+ select {
|
|
|
+ case <-stop:
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ tmpdir := www_dir + string(os.PathSeparator) + file.Name() + string(os.PathSeparator) + "tmp"
|
|
|
+ if utils.IsDirExists(tmpdir) {
|
|
|
+ session.Clean(tmpdir)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -31,10 +40,19 @@ func session_clean_start(www_dir string) chan bool {
|
|
|
}
|
|
|
}
|
|
|
}()
|
|
|
- return ch
|
|
|
+ return ch, stop
|
|
|
}
|
|
|
|
|
|
-func session_clean_stop(ch chan bool) {
|
|
|
- ch <- true
|
|
|
- <-ch
|
|
|
+func session_clean_stop(ch, stop chan bool) {
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case stop <- true:
|
|
|
+ case ch <- true:
|
|
|
+ <-ch
|
|
|
+ return
|
|
|
+ case <-time.After(10 * time.Second):
|
|
|
+ fmt.Println("Session cleaner error: force exit by timeout after 10 seconds")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|