Skip to content

Commit e79f629

Browse files
committed
fix missing files
1 parent da514e9 commit e79f629

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

middlewares/session/session.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ type Session struct {
1515
data map[string]any
1616
}
1717

18+
func (s *Session) Data() (data map[string]any) {
19+
data = make(map[string]any, len(s.data))
20+
for k, v := range s.data {
21+
data[k] = v
22+
}
23+
return
24+
}
25+
1826
// Put puts the specified `value` in the session for the given `key`.
1927
func (s *Session) Put(key string, value any) {
2028
if s.state == none {

pkg/wildcard_store.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package pkg
22

33
import (
4-
"fmt"
4+
"errors"
55
"sort"
66
"strings"
77
"sync"
88
)
99

1010
var (
11-
ErrInvalidPattern = fmt.Errorf("invalid pattern")
12-
ErrInvalidSplatPattern = fmt.Errorf("splat patterns must end with *")
13-
ErrItemAlreadyExist = fmt.Errorf("item already exist")
11+
ErrInvalidPattern = errors.New("invalid pattern")
12+
ErrItemAlreadyExist = errors.New("item already exist")
13+
ErrInvalidSplatPattern = errors.New("splat patterns must end with *")
1414
)
1515

1616
// WildcardStore utility to persist and search items using wildcards. Used for channels, topics and events
1717
//
1818
// IMPORTANT: Items should only persist during system startup.
1919
type WildcardStore[T any] struct {
2020
mutex sync.Mutex
21+
all map[string]T // all elements, by key
2122
exactly map[string]T // exactly match (Ex. /room:lobby)
2223
wildcard []*wildcardEntry[T] // wildcard match (Ex. /room:*)
2324
}
@@ -28,8 +29,11 @@ type wildcardEntry[T any] struct {
2829
}
2930

3031
// Match returns the exactly value corresponding to the first occurrence of the keyPattern that matches the given key
31-
func (s *WildcardStore[T]) MatchExactly(key string) (out T) {
32-
if item, exist := s.exactly[key]; exist {
32+
func (s *WildcardStore[T]) Get(key string) (out T) {
33+
if s.all == nil {
34+
return
35+
}
36+
if item, exist := s.all[key]; exist {
3337
out = item
3438
return
3539
}
@@ -39,9 +43,11 @@ func (s *WildcardStore[T]) MatchExactly(key string) (out T) {
3943

4044
// Match returns the value corresponding to the first occurrence of the keyPattern that matches the given key
4145
func (s *WildcardStore[T]) Match(key string) (out T) {
42-
if item, exist := s.exactly[key]; exist {
43-
out = item
44-
return
46+
if s.exactly != nil {
47+
if item, exist := s.exactly[key]; exist {
48+
out = item
49+
return
50+
}
4551
}
4652

4753
for _, entry := range s.wildcard {
@@ -60,8 +66,10 @@ func (s *WildcardStore[T]) Match(key string) (out T) {
6066
// MatchAll returns all existing values that match the given key
6167
func (s *WildcardStore[T]) MatchAll(key string) []T {
6268
var items []T
63-
if item, exist := s.exactly[key]; exist {
64-
items = append(items, item)
69+
if s.exactly != nil {
70+
if item, exist := s.exactly[key]; exist {
71+
items = append(items, item)
72+
}
6573
}
6674

6775
for _, entry := range s.wildcard {
@@ -78,6 +86,18 @@ func (s *WildcardStore[T]) MatchAll(key string) []T {
7886

7987
func (s *WildcardStore[T]) Insert(keyPattern string, value T) error {
8088

89+
if s.exactly == nil {
90+
s.exactly = map[string]T{}
91+
}
92+
93+
if s.all == nil {
94+
s.all = map[string]T{}
95+
}
96+
97+
if _, exist := s.all[keyPattern]; exist {
98+
return ErrItemAlreadyExist
99+
}
100+
81101
keyPattern = strings.TrimSpace(keyPattern)
82102

83103
if keyPattern == "" {
@@ -107,17 +127,11 @@ func (s *WildcardStore[T]) Insert(keyPattern string, value T) error {
107127
})
108128

109129
s.wildcard = wildcard
130+
s.all[keyPattern] = value
110131
return nil
111132
}
112133

113-
if s.exactly == nil {
114-
s.exactly = map[string]T{}
115-
}
116-
117-
if _, exist := s.exactly[keyPattern]; exist {
118-
return ErrItemAlreadyExist
119-
}
120-
134+
s.all[keyPattern] = value
121135
s.exactly[keyPattern] = value
122136

123137
return nil

pubsub/pubsub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func Subscribe(topicPattern string, dispatcher Dispatcher) {
6969
defer p.subscriptionsMutex.Unlock()
7070
var sub *subscription
7171

72-
if sub = p.subscriptions.MatchExactly(topicPattern); sub == nil {
72+
if sub = p.subscriptions.Get(topicPattern); sub == nil {
7373
sub = &subscription{dispatchers: map[Dispatcher]int{}}
7474
if err := p.subscriptions.Insert(topicPattern, sub); err != nil {
7575
slog.Warn(
@@ -95,7 +95,7 @@ func Unsubscribe(topicPattern string, dispatcher Dispatcher) {
9595
var sub *subscription
9696
var exist bool
9797

98-
if sub = p.subscriptions.MatchExactly(topicPattern); sub == nil {
98+
if sub = p.subscriptions.Get(topicPattern); sub == nil {
9999
return
100100
}
101101

0 commit comments

Comments
 (0)