mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-15 02:37:27 +08:00
Merge pull request #35 from franela/expire-sessions-onload
After loading saved sessions schedule them to expire
This commit is contained in:
commit
e27ccefc2b
@ -63,9 +63,7 @@ func NewInstance(session *Session) (*Instance, error) {
|
|||||||
|
|
||||||
go instance.Attach()
|
go instance.Attach()
|
||||||
|
|
||||||
rw.Lock()
|
|
||||||
err = saveSessionsToDisk()
|
err = saveSessionsToDisk()
|
||||||
rw.Unlock()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,12 @@ func CreateWSServer() *socketio.Server {
|
|||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CloseSessionAfter(s *Session, d time.Duration) {
|
||||||
|
time.AfterFunc(d, func() {
|
||||||
|
CloseSession(s)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func CloseSession(s *Session) error {
|
func CloseSession(s *Session) error {
|
||||||
s.rw.Lock()
|
s.rw.Lock()
|
||||||
defer s.rw.Unlock()
|
defer s.rw.Unlock()
|
||||||
@ -96,14 +102,17 @@ func NewSession() (*Session, error) {
|
|||||||
sessions[s.Id] = s
|
sessions[s.Id] = s
|
||||||
|
|
||||||
// Schedule cleanup of the session
|
// Schedule cleanup of the session
|
||||||
time.AfterFunc(4*time.Hour, func() {
|
CloseSessionAfter(s, 4*time.Hour)
|
||||||
CloseSession(s)
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := CreateNetwork(s.Id); err != nil {
|
if err := CreateNetwork(s.Id); err != nil {
|
||||||
log.Println("ERROR NETWORKING")
|
log.Println("ERROR NETWORKING")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We store sessions as soon as we create one so we don't delete new sessions on an api restart
|
||||||
|
if err := saveSessionsToDisk(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,12 +135,24 @@ func LoadSessionsFromDisk() error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
decoder := gob.NewDecoder(file)
|
decoder := gob.NewDecoder(file)
|
||||||
err = decoder.Decode(&sessions)
|
err = decoder.Decode(&sessions)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// schedule session expiration
|
||||||
|
for _, s := range sessions {
|
||||||
|
timeLeft := s.ExpiresAt.Sub(time.Now())
|
||||||
|
CloseSessionAfter(s, timeLeft)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file.Close()
|
file.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveSessionsToDisk() error {
|
func saveSessionsToDisk() error {
|
||||||
|
rw.Lock()
|
||||||
|
defer rw.Unlock()
|
||||||
file, err := os.Create("./pwd/sessions.gob")
|
file, err := os.Create("./pwd/sessions.gob")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
encoder := gob.NewEncoder(file)
|
encoder := gob.NewEncoder(file)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user