-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunch.go
138 lines (113 loc) · 2.56 KB
/
bunch.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
package storage
import (
"bytes"
"context"
"strconv"
"sync"
"time"
"github.com/cornelk/hashmap"
)
const (
numberCyclesForReloadTime = 200
MaxAllowedExtendedTTL = int64(300)
cleanerDelay = 2 * time.Second
)
type Bunch struct {
sync.RWMutex
ctx context.Context
sessions *hashmap.HashMap
}
func newBunch(ctx context.Context) *Bunch {
bunch := &Bunch{
ctx: ctx,
sessions: &hashmap.HashMap{},
}
// run cleaner
go bunch.deleteExpired(ctx)
return bunch
}
// create creates new session. Always success.
func (b *Bunch) create(uuid string, ttl uint32) {
// non-blocking operation
s := time.Now().Unix() + int64(ttl)
b.sessions.Set(uuid, s)
}
func (b *Bunch) extend(uuid string, ttl uint32) bool {
// blocking operation
b.RLock()
defer b.RUnlock()
value, ok := b.sessions.Get(uuid)
if ok && value != nil {
if newSession, find := extendTimeSession(value.(int64), time.Now().Unix(), int64(ttl)); find {
// session is not expired now
b.sessions.Set(uuid, newSession)
return true
}
}
return false
}
func (b *Bunch) destroy(id string) bool {
// blocking operation
b.Lock()
defer b.Unlock()
if _, ok := b.sessions.Get(id); ok {
b.sessions.Del(id)
return true
}
return false
}
func (b *Bunch) list(resCh chan []byte) {
select {
case <-b.ctx.Done():
case resCh <- b.allSession():
}
}
func (b *Bunch) allSession() []byte {
buffer := bytes.NewBuffer([]byte{})
now := time.Now().Unix()
counter := 0
for i := range b.sessions.Iter() {
if i.Value != nil {
if ttl := i.Value.(int64) - now; ttl > 0 { // session is not expired now
buffer.WriteString(`{"id":"` + i.Key.(string) + `","ttl":` + strconv.FormatInt(ttl, 10) + `},`)
}
}
// it does >> 1000 cycles per second so it doesn't make sense get time each times.
counter++
if counter > numberCyclesForReloadTime {
counter = 0
now = time.Now().Unix()
}
}
// send final data
return buffer.Bytes()
}
func (b *Bunch) deleteExpired(ctx context.Context) {
// it's deleting sessions which are already expired.
// it's not real method.
for {
select {
case <-ctx.Done():
// game over
return
case <-time.After(cleanerDelay):
for i := range b.sessions.Iter() {
if i.Value != nil {
if i.Value.(int64) <= time.Now().Unix() {
b.sessions.Del(i.Key)
}
}
}
}
}
}
func extendTimeSession(session, now, ttl int64) (int64, bool) {
if session < now { // session is expired
return 0, false
}
session += ttl
if session-now < MaxAllowedExtendedTTL {
return session, true
}
return now + MaxAllowedExtendedTTL, true
}