You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.5 KiB
68 lines
1.5 KiB
package session |
|
|
|
import ( |
|
"sonet/pkg/utils/collect" |
|
"sonet/pkg/utils/logger" |
|
) |
|
|
|
type Store interface { |
|
Load(uid string) (channel *Channel, exist bool) |
|
Delete(uid string) |
|
Store(uid string, channel *Channel) |
|
Range(f func(key string, channel *Channel) bool) |
|
OnStore() <-chan string // Store 函数调用 |
|
OnDelete() <-chan *Channel // Delete 函数调用 |
|
} |
|
|
|
func NewMapStore(concurrentLevel int) Store { |
|
return &mapStore{ |
|
m: collect.NewConcurrentMap[string, *Channel](concurrentLevel, func(k string) string { return k }), |
|
storeCh: make(chan string, 128), |
|
deleteCh: make(chan *Channel, 64), |
|
} |
|
} |
|
|
|
type mapStore struct { |
|
m *collect.ConcurrentMap[string, *Channel] |
|
storeCh chan string |
|
deleteCh chan *Channel |
|
} |
|
|
|
func (c *mapStore) Load(uid string) (channel *Channel, exist bool) { |
|
channel, exist = c.m.Load(uid) |
|
return |
|
} |
|
|
|
func (c *mapStore) Delete(uid string) { |
|
ch, ok := c.m.LoadAndDelete(uid) |
|
if !ok { |
|
return |
|
} |
|
|
|
select { |
|
case c.deleteCh <- ch: |
|
default: |
|
logger.Warning("session store delete channel fulled, %s", uid) |
|
} |
|
} |
|
|
|
func (c *mapStore) Store(uid string, channel *Channel) { |
|
c.m.Store(uid, channel) |
|
|
|
c.storeCh <- uid |
|
// logger.Warningf("session store store channel fulled, %s", uid) |
|
} |
|
|
|
func (c *mapStore) Range(f func(key string, channel *Channel) bool) { |
|
c.m.Range(f) |
|
} |
|
|
|
// OnStore 用户上线 |
|
func (c *mapStore) OnStore() <-chan string { |
|
return c.storeCh |
|
} |
|
|
|
// OnDelete 用户离线 |
|
func (c *mapStore) OnDelete() <-chan *Channel { |
|
return c.deleteCh |
|
}
|
|
|