-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
152 lines (138 loc) · 3.13 KB
/
session.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
func setSession(w http.ResponseWriter, session map[string]string) error {
encoded, err := secureCookie.Encode("session", session)
if err != nil {
return err
}
c := &http.Cookie{
Name: "session",
Value: encoded,
Path: "/",
}
http.SetCookie(w, c)
return nil
}
func getSession(r *http.Request) (map[string]string, error) {
value := make(map[string]string)
c, _ := r.Cookie("session")
if c == nil {
return value, nil
}
err := secureCookie.Decode("session", c.Value, &value)
if err != nil {
return nil, err
}
return value, nil
}
func clearSession(w http.ResponseWriter) {
c := &http.Cookie{
Name: "session",
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(w, c)
}
type AppSession struct {
User string
Session string
}
// sessionCh is a channel to send/recv a session through AppSessionManager.
type sessionCh struct {
ch chan AppSession
created time.Time
}
// AppSessionManager holds sessions for apps until they ask them.
// It helps app login process.
type AppSessionManager struct {
sync.Mutex
chs map[string]*sessionCh
}
// NewAppSessionManager creates a new AppSessionManager.
func NewAppSessionManager() *AppSessionManager {
m := &AppSessionManager{
chs: make(map[string]*sessionCh),
}
go func() {
for {
time.Sleep(time.Minute)
m.clearOldSessions()
}
}()
return m
}
// DebugStatus prints the manager status every 5 seconds.
func (m *AppSessionManager) DebugStatus() {
go func() {
for {
time.Sleep(5 * time.Second)
fmt.Printf("AppSessionManager is having %v sessions.\n", len(m.chs))
}
}()
}
// RecieveSession waits to recieve the session for given key.
// After 5 minutes has passed it will return timeout error.
func (m *AppSessionManager) RecieveSession(key string) (AppSession, error) {
ch := m.recieveSessionCh(key)
select {
case sess := <-ch:
m.deleteSession(key)
return sess, nil
case <-time.After(5 * time.Minute):
return AppSession{}, fmt.Errorf("timeout")
}
}
// recieveSessionCh returns channel for recieving the session for given key.
func (m *AppSessionManager) recieveSessionCh(key string) <-chan AppSession {
m.Lock()
defer m.Unlock()
if m.chs[key] == nil {
m.chs[key] = &sessionCh{
ch: make(chan AppSession),
created: time.Now(),
}
}
return m.chs[key].ch
}
// SendSession saves a session for given key.
func (m *AppSessionManager) SendSession(key string, sess AppSession) {
m.Lock()
defer m.Unlock()
if m.chs[key] == nil {
m.chs[key] = &sessionCh{
ch: make(chan AppSession),
created: time.Now(),
}
}
go func() {
m.chs[key].ch <- sess
}()
}
// deleteSession deletes a session for given key.
func (m *AppSessionManager) deleteSession(key string) {
m.Lock()
defer m.Unlock()
close(m.chs[key].ch)
delete(m.chs, key)
}
// clearOldSessions deletes sessions those are aged over 5 minutes.
func (m *AppSessionManager) clearOldSessions() {
m.Lock()
defer m.Unlock()
now := time.Now()
list := make([]string, 0)
for key, as := range m.chs {
if now.Sub(as.created) > 5*time.Minute {
list = append(list, key)
}
}
for _, key := range list {
delete(m.chs, key)
}
}