-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
237 lines (206 loc) · 5.12 KB
/
session.go
File metadata and controls
237 lines (206 loc) · 5.12 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package apisession
import (
"fmt"
"time"
)
type APISession struct {
Id string `json:"i" msgpack:"i"`
Owner string `json:"o" msgpack:"o"`
//Map of url to API call track
Records map[string]*APICallRecord `json:"r" msgpack:"r"`
//Current time window
Window int64 `json:"w" msgpack:"w"`
//Payload are extra data of session
Payload map[string]any `json:"p" msgpack:"p"`
Created int64 `json:"c" msgpack:"c"` //Created time in milliseconds
Updated int64 `json:"u" msgpack:"u"` //Updated time in milliseconds
}
// Tracks how an api is being called
type APICallRecord struct {
//calls in current window
Count int64 `json:"c" msgpack:"c"`
//Last call in milliseconds
Last int64 `json:"l" msgpack:"l"`
}
func NewAPICallRecord() *APICallRecord {
return &APICallRecord{
Count: 0,
Last: 0,
}
}
func (session *APISession) RecordCall(url string) error {
return fmt.Errorf("not implemented")
}
func NewAPISession(owner string) *APISession {
return &APISession{
Id: GenerateSessionValue(owner),
Owner: owner,
Records: make(map[string]*APICallRecord),
Window: 0,
Payload: nil,
Created: time.Now().UnixMilli(),
Updated: time.Now().UnixMilli(),
}
}
func NewAPISessionWithPayload(owner string, payload map[string]any) *APISession {
return &APISession{
Id: GenerateSessionValue(owner),
Owner: owner,
Records: make(map[string]*APICallRecord),
Window: 0,
Payload: payload,
Created: time.Now().UnixMilli(),
Updated: time.Now().UnixMilli(),
}
}
func (ses *APISession) SetPayload(key string, value any) {
if ses.Payload == nil {
ses.Payload = make(map[string]any)
}
ses.Payload[key] = value
}
func (ses *APISession) GetPayload(key string) any {
if ses.Payload == nil {
return nil
}
return ses.Payload[key]
}
func (ses *APISession) SetWindow(window int64) {
ses.Window = window
for _, record := range ses.Records {
record.Count = 0
}
}
// Returns metadata as string, empty string if not found
func (ses *APISession) GetPayloadString(key string) string {
if ses.Payload == nil {
return ""
}
value := ses.Payload[key]
if value != nil {
strValue, isString := value.(string)
if isString {
return strValue
}
}
return ""
}
// Returns metadata as int64, 0 if not found
func (ses *APISession) GetPayloadInt64(key string) int64 {
if ses.Payload == nil {
return 0
}
value := ses.Payload[key]
if value != nil {
intValue, isInt64 := value.(int64)
if isInt64 {
return intValue
}
}
return 0
}
// Returns metadata as int, 0 if not found
func (ses *APISession) GetPayloadInt(key string) int {
if ses.Payload == nil {
return 0
}
value := ses.Payload[key]
if value != nil {
intValue, isInt := value.(int)
if isInt {
return intValue
}
}
return 0
}
func (ses *APISession) GetCallRecord(url string) *APICallRecord {
var record *APICallRecord
record, exist := ses.Records[url]
if !exist {
record = NewAPICallRecord()
ses.Records[url] = record
}
return record
}
func (ses *APISession) ValidateSession(session string) bool {
return ses.Id == session
}
// GetPayloadMap returns a map from session payload, if not exist, return nil
func GetPayloadMap[K comparable, V any](sess *APISession, key string) map[K]V {
value, exist := sess.Payload[key]
if !exist {
return nil
}
typedValue, ok := value.(map[K]V)
if !ok {
return nil
}
return typedValue
}
// GetOrCreatePayloadMap returns a map from session payload, if not exist, create a new map.
// - If the value of key is not a map, replace it with a new map.
// - If any value of the map is not the correct type, it will be ignored.
//
// Return the map and a boolean indicate if the map is created
func GetOrCreatePayloadMap[K comparable, V any](sess *APISession, key string) (map[K]V, bool) {
value, exist := sess.Payload[key]
if !exist {
newMap := make(map[K]V)
sess.Payload[key] = newMap
return newMap, true
}
//Check if the value is correct map type
typedValue, ok := value.(map[K]V)
if ok {
return typedValue, false
}
//Check if the value is map of any
mapAny, ok := value.(map[K]any)
if ok {
newMap := make(map[K]V)
for k, v := range mapAny {
mapValue, valueOk := v.(V)
if valueOk {
newMap[k] = mapValue
}
}
sess.Payload[key] = newMap
return newMap, false
}
//Casting failed, replace with new map
newMap := make(map[K]V)
sess.Payload[key] = newMap
return newMap, true
}
func GetPayloadSlice[V any](sess *APISession, key string) ([]V, bool) {
value, exist := sess.Payload[key]
if !exist {
return nil, false
}
retValues := []V{}
for _, v := range value.([]any) {
typedValue, ok := v.(V)
if !ok {
return nil, false
}
retValues = append(retValues, typedValue)
}
return retValues, true
}
func GetOrCreatePayloadSlice[V any](sess *APISession, key string) ([]V, bool) {
value, exist := sess.Payload[key]
if !exist {
newSlice := make([]V, 0)
sess.Payload[key] = value
return newSlice, false
}
typedValue, ok := value.([]V)
return typedValue, ok
}
// SetPayloadMap init a map in session payload
func SetPayloadMap[K comparable, V any](sess *APISession, key string, value map[K]V) {
if sess.Payload == nil {
sess.Payload = make(map[string]any)
}
sess.Payload[key] = value
}