-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sessions.go
111 lines (101 loc) · 2.51 KB
/
Sessions.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
package viot
import (
"time"
"github.com/456vv/vmap/v2"
)
type manageSession struct{
s Sessioner
recent time.Time
}
// Sessions集
type Sessions struct{
Expired time.Duration // 保存session时间长
ss vmap.Map // 集,map[token]*Session
}
//Len 当前Session数量
// int 数量
func (T *Sessions) Len() int {
return T.ss.Len()
}
//ProcessDeadAll 定时来处理过期的Session
// []string 过期的ID名称
func (T *Sessions) ProcessDeadAll() []interface{} {
var expId []interface{}
if T.Expired != 0 {
currTime := time.Now()
T.ss.Range(func(token, mse interface{}) bool{
ms := mse.(*manageSession)
recentTime := ms.recent.Add(T.Expired)
if currTime.After(recentTime) {
//追加了expId一次性删除
expId = append(expId, token)
//执行Defer
go ms.s.Free()
}
return true
})
T.ss.Dels(expId)
}
return expId
}
//triggerDeadSession 由用户来触发,并删除已挂载入的Defer
func (T *Sessions) triggerDeadSession(ms *manageSession) (ok bool) {
if T.Expired != 0 {
currTime := time.Now()
recentTime := ms.recent.Add(T.Expired)
if currTime.After(recentTime) {
go ms.s.Free()
return true
}
}
return
}
//GetSession 使用token读取会话
// token string 标识符
// Sessioner 会话
// bool 错误
func (T *Sessions) GetSession(token string) (Sessioner, bool) {
mse, ok := T.ss.GetHas(token)
if !ok {
return nil, false
}
ms := mse.(*manageSession)
if T.triggerDeadSession(ms) {
T.ss.Del(token)
return nil, false
}
ms.recent = time.Now()
return ms.s, true
}
//SetSession 使用token写入新的会话
// token string 标识符
// s Sessioner 新的会话
func (T *Sessions) SetSession(token string, s Sessioner) Sessioner {
if inf, ok := T.ss.GetHas(token); ok {
ms := inf.(*manageSession)
if ms.s.Token() == s.Token() {
//已经存在,无法再设置
return s
}
go ms.s.Free()
}
if t, can := s.(*Session); can {
//对应这个token,并保存
t.token = token
}
ms := &manageSession{
s:s,
recent:time.Now(),
}
T.ss.Set(token, ms)
return s
}
//DelSession 使用token删除的会话
// token string token标识符
func (T *Sessions) DelSession(token string) {
if mse, ok := T.ss.GetHas(token); ok {
ms := mse.(*manageSession)
go ms.s.Free()
T.ss.Del(token)
}
}