From b6d15f2b602c87f3bdd1e06a84e1d0d5386b38fd Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Thu, 11 Jan 2024 19:05:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=94=A8=E6=88=B7=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/usersession.go | 8 ++++++++ 1 file changed, 8 insertions(+) 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 }