diff --git a/cmd/apimain.go b/cmd/apimain.go index 77791a8..ae66c10 100644 --- a/cmd/apimain.go +++ b/cmd/apimain.go @@ -101,7 +101,7 @@ func main() { } func DatabaseAutoUpdate() { - version := 241 + version := 242 db := global.DB diff --git a/lib/lock/local_test.go b/lib/lock/local_test.go index e2ee037..68c783c 100644 --- a/lib/lock/local_test.go +++ b/lib/lock/local_test.go @@ -51,30 +51,22 @@ func TestLocal_GetLock(t *testing.T) { func TestLocal_Lock(t *testing.T) { l := NewLocal() wg := sync.WaitGroup{} - wg.Add(3) + m := 10 + wg.Add(m) i := 0 - go func() { - l.Lock("key") - fmt.Println("l1", i) - i++ - l.UnLock("key") - wg.Done() - }() - go func() { - l.Lock("key") - fmt.Println("l2", i) - i++ - l.UnLock("key") - wg.Done() - }() - go func() { - l.Lock("key") - fmt.Println("l3", i) - i++ - l.UnLock("key") - wg.Done() - }() + for j := 0; j < m; j++ { + go func() { + l.Lock("key") + //fmt.Println(j, i) + i++ + fmt.Println(j, i) + l.UnLock("key") + wg.Done() + }() + } + wg.Wait() + fmt.Println(i) } func TestSyncMap(t *testing.T) { diff --git a/model/user.go b/model/user.go index 32af6d7..1d83458 100644 --- a/model/user.go +++ b/model/user.go @@ -2,7 +2,7 @@ package model type User struct { IdModel - Username string `json:"username" gorm:"default:'';not null;index,unique"` + Username string `json:"username" gorm:"default:'';not null;uniqueIndex"` Password string `json:"-" gorm:"default:'';not null;"` Nickname string `json:"nickname" gorm:"default:'';not null;"` Avatar string `json:"avatar" gorm:"default:'';not null;"` diff --git a/service/user.go b/service/user.go index d62a4eb..fb77ba6 100644 --- a/service/user.go +++ b/service/user.go @@ -221,20 +221,21 @@ func (us *UserService) RegisterByGoogle(name string, email string) *model.User { // RegisterByOauth 注册 func (us *UserService) RegisterByOauth(thirdType, thirdName, uid string) *model.User { + global.Lock.Lock("registerByOauth") + defer global.Lock.UnLock("registerByOauth") + ut := AllService.OauthService.UserThirdInfo(thirdType, uid) + if ut.Id != 0 { + u := &model.User{} + global.DB.Where("id = ?", ut.UserId).First(u) + return u + } + tx := global.DB.Begin() - ut := &model.UserThird{ + ut = &model.UserThird{ OpenId: uid, ThirdName: thirdName, ThirdType: thirdType, } - //global.DB.Where("open_id = ?", githubId).First(ut) - //这种情况不应该出现,如果出现说明有bug - //if ut.Id != 0 { - // u := &model.User{} - // global.DB.Where("id = ?", ut.UserId).First(u) - // tx.Commit() - // return u - //} username := us.GenerateUsernameByOauth(thirdName) u := &model.User{ @@ -242,6 +243,10 @@ func (us *UserService) RegisterByOauth(thirdType, thirdName, uid string) *model. GroupId: 1, } global.DB.Create(u) + if u.Id == 0 { + tx.Rollback() + return u + } ut.UserId = u.Id global.DB.Create(ut)