Skip to content

Commit eefe139

Browse files
committed
Use cache with TTL for memory attribute session store
1 parent da25c8a commit eefe139

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/crewjam/saml v0.4.14
1010
github.com/golang-jwt/jwt/v4 v4.5.0
1111
github.com/jackc/pgx/v5 v5.6.0
12+
github.com/karlseguin/ccache/v3 v3.0.5
1213
github.com/oklog/run v1.1.0
1314
github.com/russellhaering/goxmldsig v1.4.0
1415
github.com/spf13/cobra v1.8.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFr
3434
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
3535
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
3636
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
37+
github.com/karlseguin/ccache/v3 v3.0.5 h1:hFX25+fxzNjsRlREYsoGNa2LoVEw5mPF8wkWq/UnevQ=
38+
github.com/karlseguin/ccache/v3 v3.0.5/go.mod h1:qxC372+Qn+IBj8Pe3KvGjHPj0sWwEF7AeZVhsNPZ6uY=
3739
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3840
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
3941
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=

pkg/sp/memorystore.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,49 @@ package sp
33
import (
44
"fmt"
55
"log/slog"
6-
"sync"
6+
"time"
77

88
"github.com/crewjam/saml/samlsp"
9+
"github.com/karlseguin/ccache/v3"
910
)
1011

1112
type MemoryAttributeStore struct {
12-
store map[string]samlsp.Attributes
13-
mu sync.RWMutex
13+
ttl time.Duration
14+
store *ccache.Cache[samlsp.Attributes]
1415
}
1516

16-
func NewMemoryAttributeStore() (*MemoryAttributeStore, error) {
17+
func NewMemoryAttributeStore(ttl time.Duration) (*MemoryAttributeStore, error) {
1718
return &MemoryAttributeStore{
18-
store: make(map[string]samlsp.Attributes),
19+
store: ccache.New(ccache.Configure[samlsp.Attributes]()),
1920
}, nil
2021
}
2122

2223
func (s *MemoryAttributeStore) Get(id string) (samlsp.Attributes, error) {
23-
s.mu.RLock()
24-
defer s.mu.RUnlock()
24+
if item := s.store.Get(id); item != nil {
25+
slog.Debug("getting attributes from store", "id", id, "attrs", item.Value())
2526

26-
if attrs, found := s.store[id]; found {
27-
slog.Debug("getting attributes from store", "id", id, "attrs", attrs)
28-
29-
return attrs, nil
27+
return item.Value(), nil
3028
}
3129

3230
return nil, fmt.Errorf("not found")
3331
}
3432

3533
func (s *MemoryAttributeStore) Set(id string, attrs samlsp.Attributes) {
36-
s.mu.Lock()
37-
defer s.mu.Unlock()
38-
3934
if s.store == nil {
40-
s.store = make(map[string]samlsp.Attributes)
35+
s.store = ccache.New(ccache.Configure[samlsp.Attributes]())
4136
}
4237

4338
slog.Debug("setting attributes in store", "id", id, "attrs", attrs)
4439

45-
s.store[id] = attrs
40+
s.store.Set(id, attrs, s.ttl)
4641
}
4742

4843
func (s *MemoryAttributeStore) Delete(id string) {
49-
s.mu.Lock()
50-
defer s.mu.Unlock()
51-
5244
if s.store == nil {
5345
return
5446
}
5547

5648
slog.Debug("deleting attributes in store", "id", id)
5749

58-
delete(s.store, id)
50+
s.store.Delete(id)
5951
}

pkg/sp/sp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func NewServiceProvider(cert, key string, root *url.URL, options ...ServiceProvi
6565
return nil, fmt.Errorf("metadata was not set")
6666
}
6767

68-
// set default store
68+
// set default store with a 1-hour TTL
6969
if serviceProvider.store == nil {
70-
serviceProvider.store, _ = NewMemoryAttributeStore()
70+
serviceProvider.store, _ = NewMemoryAttributeStore(time.Hour * 1)
7171
}
7272

7373
// samlsp options

0 commit comments

Comments
 (0)