diff --git a/comm/usersession.go b/comm/usersession.go index bc6d06baa..11d91aa62 100644 --- a/comm/usersession.go +++ b/comm/usersession.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "go_dreamfactory/pb" + "sync" "go_dreamfactory/lego/sys/log" @@ -31,6 +32,7 @@ type UserSession struct { UserId string service IService msgqueue []*pb.UserMessage + lock sync.RWMutex mate map[string]interface{} } @@ -117,12 +119,16 @@ func (this *UserSession) UnBind() (err error) { //写入元数据 func (this *UserSession) SetMate(name string, value interface{}) { + this.lock.Lock() this.mate[name] = value + this.lock.Unlock() } //写入元数据 func (this *UserSession) GetMate(name string) (ok bool, value interface{}) { + this.lock.RLock() value, ok = this.mate[name] + this.lock.RUnlock() return } @@ -189,9 +195,11 @@ func (this *UserSession) SyncPush() (err error) { func (this *UserSession) Clone() (session IUserSession) { session = this.service.GetUserSession() session.SetSession(this.IP, this.SessionId, this.ServiceTag, this.GatewayServiceId, this.UserId) + this.lock.RLock() for k, v := range this.mate { session.SetMate(k, v) } + this.lock.RUnlock() return }