Skip to content

Commit

Permalink
fix: db remove map avoid concurrent login or logout database closed. (#…
Browse files Browse the repository at this point in the history
…508)

* fix: db remove map avoid concurrent login or logout database closed.

Signed-off-by: Gordon <[email protected]>

* feat: CheckResourceLoad when Synccall (#503)

Co-authored-by: Sylar Deng <[email protected]>

* fix: change logout do not recycle server's im token when run in oimws.

Signed-off-by: Gordon <[email protected]>

---------

Signed-off-by: Gordon <[email protected]>
Co-authored-by: SylarD <[email protected]>
Co-authored-by: Sylar Deng <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent 3d11a5c commit 999b7f4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 63 deletions.
15 changes: 0 additions & 15 deletions internal/login/init_login.go

This file was deleted.

15 changes: 0 additions & 15 deletions internal/login/sdk.go

This file was deleted.

6 changes: 3 additions & 3 deletions open_im_sdk/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,16 @@ func syncCall(operationID string, fn any, args ...any) (res string) {
if operationID == "" {
return ""
}
if err = CheckResourceLoad(UserForSDK, ""); err != nil {
return ""
}
fnv := reflect.ValueOf(fn)
if fnv.Kind() != reflect.Func {
err = errs.ErrRecordNotFound
return ""
}
funcPtr := reflect.ValueOf(fn).Pointer()
funcName := runtime.FuncForPC(funcPtr).Name()
if err = CheckResourceLoad(UserForSDK, funcName); err != nil {
return ""
}
fnt := fnv.Type()
numIn := fnt.NumIn()
if len(args)+1 != numIn {
Expand Down
14 changes: 12 additions & 2 deletions open_im_sdk/userRelated.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ const (
Logged
)

const (
LogoutTips = "js sdk socket close"
)

var (
// Client-independent user class
// UserForSDK Client-independent user class
UserForSDK *LoginMgr
)

Expand Down Expand Up @@ -418,6 +422,9 @@ func (u *LoginMgr) UnInitSDK() {

// token error recycle recourse, kicked not recycle
func (u *LoginMgr) logout(ctx context.Context, isTokenValid bool) error {
if ccontext.Info(ctx).OperationID() == LogoutTips {
isTokenValid = true
}
if !isTokenValid {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
Expand All @@ -429,7 +436,10 @@ func (u *LoginMgr) logout(ctx context.Context, isTokenValid bool) error {
}
}
u.Exit()
_ = u.db.Close(u.ctx)
err := u.db.Close(u.ctx)
if err != nil {
log.ZWarn(ctx, "TriggerCmdLogout db recycle resources failed...", err)
}
// user object must be rest when user logout
u.initResources()
log.ZDebug(ctx, "TriggerCmdLogout client success...", "isTokenValid", isTokenValid)
Expand Down
34 changes: 6 additions & 28 deletions pkg/db/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ import (
"gorm.io/gorm/logger"
)

//"github.com/glebarez/sqlite"

var UserDBMap map[string]*DataBase

var UserDBLock sync.RWMutex

func init() {
UserDBMap = make(map[string]*DataBase, 0)
}

type DataBase struct {
loginUserID string
dbDir string
Expand Down Expand Up @@ -77,37 +67,25 @@ func (d *DataBase) InitDB(ctx context.Context, userID string, dataDir string) er
}

func (d *DataBase) Close(ctx context.Context) error {
UserDBLock.Lock()
dbConn, err := d.conn.WithContext(ctx).DB()
if err != nil {
// log.Error("", "get db conn failed ", err.Error())
return err
} else {
if dbConn != nil {
// log.Info("", "close db finished")
err := dbConn.Close()
if err != nil {
// log.Error("", "close db failed ", err.Error())
return err
}
}
}
// log.NewInfo("", "CloseDB ok, delete db map ", d.loginUserID)
delete(UserDBMap, d.loginUserID)
UserDBLock.Unlock()
return nil
}

func NewDataBase(ctx context.Context, loginUserID string, dbDir string, logLevel int) (*DataBase, error) {
UserDBLock.Lock()
defer UserDBLock.Unlock()
dataBase, ok := UserDBMap[loginUserID]
if !ok {
dataBase = &DataBase{loginUserID: loginUserID, dbDir: dbDir}
err := dataBase.initDB(ctx, logLevel)
if err != nil {
return dataBase, utils.Wrap(err, "initDB failed "+dbDir)
}
UserDBMap[loginUserID] = dataBase
//log.Info(operationID, "open db", loginUserID)
dataBase := &DataBase{loginUserID: loginUserID, dbDir: dbDir}
err := dataBase.initDB(ctx, logLevel)
if err != nil {
return dataBase, utils.Wrap(err, "initDB failed "+dbDir)
}
return dataBase, nil
}
Expand Down

0 comments on commit 999b7f4

Please sign in to comment.