-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
108 lines (89 loc) · 2.44 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gosesh
import (
"fmt"
"sync"
"time"
)
// StoreInterface is responsible for fetching and saving sessions by their ID.
type StoreInterface interface {
// Get returns a pointer to a SessionInterface by its id.
// The returned session will have an updated access time set to the current time.
// An error is returned if this store does not contain a session with the specified id.
Get(id string) (SessionInterface, error)
// Add adds a new SessionInterface to the store.
Add(session SessionInterface)
// Remove removes a SessionInterface from the store.
Remove(session SessionInterface)
}
// MemStore is a StoreInterface implementation.
type MemStore struct {
sessions map[string]SessionInterface // Map of sessions (mapped from ID)
mux *sync.RWMutex // mutex to synchronize access to sessions
ticker *time.Ticker
endTicker chan bool
}
// NewMemStore returns a pointer to a MemStore.
func NewMemStore(interval time.Duration) *MemStore {
s := &MemStore{
sessions: make(map[string]SessionInterface),
mux: &sync.RWMutex{},
endTicker: make(chan bool),
}
go s.clearTimeouts(interval)
return s
}
// Get implements Store.Get().
func (s *MemStore) Get(id string) (SessionInterface, error) {
s.mux.RLock()
defer s.mux.RUnlock()
session, sessionExists := s.sessions[id]
if !sessionExists {
return nil, fmt.Errorf("Session ID %s does not exist", id)
}
session.Access()
return session, nil
}
// Add implements Store.Add().
func (s *MemStore) Add(session SessionInterface) {
s.mux.Lock()
defer s.mux.Unlock()
s.sessions[session.ID()] = session
}
// Remove implements Store.Remove().
func (s *MemStore) Remove(session SessionInterface) {
s.mux.Lock()
defer s.mux.Unlock()
delete(s.sessions, session.ID())
}
// Close impliments Store.Close()
func (s *MemStore) Close() {
close(s.endTicker)
}
// clearTimeouts will remove all timed out sessions.
func (s *MemStore) clearTimeouts(interval time.Duration) {
ticker := time.NewTicker(interval)
for {
select {
case <-s.endTicker:
ticker.Stop()
return
case now := <-ticker.C:
ids := make([]string, 0)
s.mux.RLock()
for _, session := range s.sessions {
// Check if session has timed out.
if now.Sub(session.Accessed()) > session.Timeout() {
ids = append(ids, session.ID())
}
}
s.mux.RUnlock()
if len(ids) > 0 {
s.mux.Lock()
for _, id := range ids {
delete(s.sessions, id)
}
s.mux.Unlock()
}
}
}
}