package session import ( "mongo.games.com/game/gamesrv/slotspkg/internal/generic/key" "mongo.games.com/game/gamesrv/slotspkg/internal/module/shared" "sync" "sync/atomic" "time" ) type Session struct { sync.RWMutex uid uint64 // binding user id coin int64 data map[string]interface{} // session data store lastConnectTime time.Time connectNum uint64 } func NewSession(uid uint64, coin int64) *Session { return &Session{ uid: uid, coin: coin, data: make(map[string]interface{}), } } func (s *Session) SetLastConnectTime() bool { if time.Now().Sub(s.lastConnectTime) > time.Second { s.connectNum = 1 s.lastConnectTime = time.Now() } else { s.connectNum++ if s.connectNum > 5 { return false } } return true } func (s *Session) GetLastConnectTime() time.Time { return s.lastConnectTime } func (s *Session) UID() uint64 { return atomic.LoadUint64(&s.uid) } func (s *Session) Coin() int64 { return atomic.LoadInt64(&s.coin) } func (s *Session) SetSessionContext(sessionContext *shared.SessionContext) { s.Set(key.SessionContext, sessionContext) } func (s *Session) Set(key string, value interface{}) { s.Lock() defer s.Unlock() if s.data == nil { s.data = make(map[string]interface{}) } s.data[key] = value } func (s *Session) Remove(key string) { s.Lock() defer s.Unlock() delete(s.data, key) } func (s *Session) Value(key string) interface{} { s.RLock() defer s.RUnlock() return s.data[key] } func (s *Session) Bool(key string) bool { s.RLock() defer s.RUnlock() v, ok := s.data[key] if !ok { return false } value, ok := v.(bool) if !ok { return false } return value }