This commit is contained in:
aozhiwei 2024-07-13 16:48:57 +08:00
parent 90267f2b1a
commit 81e5f65f46
5 changed files with 89 additions and 9 deletions

View File

@ -18,15 +18,18 @@ type StreamPagination struct {
type Session interface {
GetAccountAddress() string
GetToken() string
GetLoginTime() int32
}
type SessionMgr interface {
GenNonce() string
IsValidNonce(string) bool
AddSession(string, string)
RemoveSssion(string)
AddSession(string, string) Session
RemoveSssionWithToken(string)
RemoveSssionWithAddress(string)
GetSessionByToken(string) Session
GetSessionByAddress(string) Session
}
type App interface {

View File

@ -32,6 +32,7 @@ require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gomodule/redigo v1.8.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect

View File

@ -35,6 +35,8 @@ github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUz
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=

View File

@ -1,14 +1,32 @@
package session
type session struct {
import (
"f5"
)
type session struct {
accountAddress string
token string
loginTime int32
}
func (this *session) GetAccountAddress() string {
return ""
return this.accountAddress
}
func (this *session) GetToken() string {
return this.token
}
func (this *session) GetLoginTime() int32 {
return 0
return this.loginTime
}
func newSession(accountAddress string, token string) *session {
p := new(session)
p.accountAddress = accountAddress
p.token = token
p.loginTime = int32(f5.GetApp().GetRealSeconds())
return p
}

View File

@ -1,13 +1,21 @@
package session
import (
"q5"
"main/common"
"github.com/google/uuid"
"sync"
)
type sessionMgr struct {
lock sync.Mutex
addressHash *q5.ConcurrentMap[string, *session]
sessionHash *q5.ConcurrentMap[string, *session]
}
func (this *sessionMgr) Init() {
this.addressHash = new(q5.ConcurrentMap[string, *session])
this.sessionHash = new(q5.ConcurrentMap[string, *session])
}
func (this *sessionMgr) UnInit() {
@ -15,21 +23,69 @@ func (this *sessionMgr) UnInit() {
}
func (this *sessionMgr) GenNonce() string {
return ""
uuidV4 := uuid.New()
return uuidV4.String()
}
func (this *sessionMgr) IsValidNonce(string) bool {
return true
}
func (this *sessionMgr) AddSession(string, string) {
func (this *sessionMgr) AddSession(accountAddress string, token string) common.Session {
this.lock.Lock()
defer this.lock.Unlock()
{
s := this.GetSessionByToken(token)
if s != nil {
this.internalRemoveSession(s)
}
}
{
s := this.GetSessionByAddress(accountAddress)
if s != nil {
this.internalRemoveSession(s)
}
}
s := newSession(accountAddress, token)
return s
}
func (this *sessionMgr) RemoveSssion(string) {
func (this *sessionMgr) RemoveSssionWithToken(token string) {
this.lock.Lock()
defer this.lock.Unlock()
s := this.GetSessionByToken(token)
if s != nil {
this.internalRemoveSession(s)
}
}
func (this* sessionMgr) GetSessionByToken(string) common.Session {
func (this *sessionMgr) RemoveSssionWithAddress(accountAddress string) {
this.lock.Lock()
defer this.lock.Unlock()
s := this.GetSessionByAddress(accountAddress)
if s != nil {
this.internalRemoveSession(s)
}
}
func (this *sessionMgr) GetSessionByToken(token string) common.Session {
if s, ok := this.sessionHash.Load(token); ok {
return *s
}
return nil
}
func (this *sessionMgr) GetSessionByAddress(accountAddress string) common.Session {
if s, ok := this.addressHash.Load(accountAddress); ok {
return *s
}
return nil
}
func (this *sessionMgr) internalRemoveSession(s common.Session) {
this.sessionHash.Delete(s.GetToken())
this.addressHash.Delete(s.GetAccountAddress())
}