forked from godaddy/asherah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_cache.go
170 lines (129 loc) · 3.54 KB
/
session_cache.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package appencryption
import (
"sync"
"time"
"github.com/godaddy/asherah/go/appencryption/pkg/cache"
"github.com/godaddy/asherah/go/appencryption/pkg/log"
)
type sessionCache interface {
Get(id string) (*Session, error)
Count() int
Close()
}
func incrementSharedSessionUsage(s *Session) {
s.encryption.(*sharedEncryption).incrementUsage()
}
// sharedEncryption is used to track the number of concurrent users to ensure sessions remain
// cached while in use.
type sharedEncryption struct {
Encryption
created time.Time
accessCounter int
mu *sync.Mutex
cond *sync.Cond
}
func (s *sharedEncryption) incrementUsage() {
s.mu.Lock()
defer s.mu.Unlock()
s.accessCounter++
}
func (s *sharedEncryption) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
defer s.cond.Broadcast()
s.accessCounter--
return nil
}
func (s *sharedEncryption) Remove() {
s.mu.Lock()
for s.accessCounter > 0 {
s.cond.Wait()
}
s.Encryption.Close()
s.mu.Unlock()
}
// sessionLoaderFunc retrieves a Session corresponding to the given partition ID.
type sessionLoaderFunc func(id string) (*Session, error)
// sessionInjectEncryption is used to inject e into s and is primarily used for testing.
func sessionInjectEncryption(s *Session, e Encryption) {
log.Debugf("injecting Encryption(%p) into Session(%p)", e, s)
s.encryption = e
}
// newSessionCacheWithCache returns a new SessionCache with the provided cache implementation
// using the provided SessionLoaderFunc and CryptoPolicy.
func newSessionCacheWithCache(loader sessionLoaderFunc, policy *CryptoPolicy, cache cache.Interface[string, *Session]) sessionCache {
return &cacheWrapper{
loader: func(id string) (*Session, error) {
log.Debugf("loading session for id: %s", id)
s, err := loader(id)
if err != nil {
return nil, err
}
_, ok := s.encryption.(*sharedEncryption)
if !ok {
mu := new(sync.Mutex)
orig := s.encryption
wrapped := &sharedEncryption{
Encryption: orig,
mu: mu,
cond: sync.NewCond(mu),
created: time.Now(),
}
sessionInjectEncryption(s, wrapped)
}
return s, nil
},
policy: policy,
cache: cache,
}
}
// cacheWrapper is a wrapper around a cache.Interface[string, *Session] that implements the
// sessionCache interface.
type cacheWrapper struct {
loader sessionLoaderFunc
policy *CryptoPolicy
cache cache.Interface[string, *Session]
mu sync.Mutex
}
func (c *cacheWrapper) Get(id string) (*Session, error) {
c.mu.Lock()
defer c.mu.Unlock()
val, err := c.getOrAdd(id)
if err != nil {
return nil, err
}
incrementSharedSessionUsage(val)
return val, nil
}
func (c *cacheWrapper) getOrAdd(id string) (*Session, error) {
if val, ok := c.cache.Get(id); ok {
return val, nil
}
val, err := c.loader(id)
if err != nil {
return nil, err
}
c.cache.Set(id, val)
return val, nil
}
func (c *cacheWrapper) Count() int {
return c.cache.Len()
}
func (c *cacheWrapper) Close() {
log.Debugf("closing session cache")
c.cache.Close()
}
func newSessionCache(loader sessionLoaderFunc, policy *CryptoPolicy) sessionCache {
cb := cache.New[string, *Session](policy.SessionCacheMaxSize)
cb.WithEvictFunc(func(k string, v *Session) {
go v.encryption.(*sharedEncryption).Remove()
})
if policy.SessionCacheDuration > 0 {
cb.WithExpiry(policy.SessionCacheDuration)
}
if policy.SessionCacheEvictionPolicy == "" {
policy.SessionCacheEvictionPolicy = "slru"
}
cb.WithPolicy(cache.CachePolicy(policy.SessionCacheEvictionPolicy))
return newSessionCacheWithCache(loader, policy, cb.Build())
}