-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
57 lines (49 loc) · 916 Bytes
/
process.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
package main
import (
"runtime"
"time"
)
type ServerInfo struct {
Timeout time.Time
Data string
}
func (s *ServerInfo) RefreshTimeout() {
s.Timeout = time.Now().Add(TIMEOUT)
}
func (s *ServerInfo) IsTimeout() bool {
return s.Timeout.Compare(time.Now()) != 1
}
// global session
var (
SESSION_MAP map[string]*ServerInfo = map[string]*ServerInfo{}
TIMEOUT = time.Second * 30
)
func SetServer(key string, data string) {
s := &ServerInfo{
Timeout: time.Now(),
Data: data,
}
s.RefreshTimeout()
SESSION_MAP[key] = s
}
func GetServer(key string) (string, bool) {
a, ok := SESSION_MAP[key]
if ok {
if a.IsTimeout() {
return "", false
}
return a.Data, ok
}
return "", false
}
func ClearServer() {
for {
time.Sleep(time.Second * 10)
for key, val := range SESSION_MAP {
if val.IsTimeout() {
delete(SESSION_MAP, key)
}
}
runtime.GC()
}
}