Skip to content

Commit

Permalink
perf(user): concurrent fetch sentence details
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 7, 2023
1 parent c56455b commit 6db769f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.5.1
github.com/gogf/gf/v2 v2.5.1
golang.org/x/sync v0.3.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
3 changes: 1 addition & 2 deletions internal/logic/hitokoto/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"reflect"

"github.com/hitokoto-osc/reviewer/internal/service"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/hitokoto-osc/reviewer/internal/consts"
"github.com/hitokoto-osc/reviewer/internal/model"
"github.com/hitokoto-osc/reviewer/internal/model/entity"
"github.com/hitokoto-osc/reviewer/internal/service"
)

// convertToSchemaV1 将 Pending/Sentence/Refuse 数据转换为 Schema V1,此操作需要数据库操作查询投票状态
Expand Down
43 changes: 28 additions & 15 deletions internal/logic/user/poll_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"golang.org/x/sync/errgroup"

"github.com/hitokoto-osc/reviewer/internal/consts"
vtime "github.com/hitokoto-osc/reviewer/utility/time"

Expand Down Expand Up @@ -101,23 +103,34 @@ func (s *sUser) GetUserPollLogsWithSentence(
return nil, err
}
userPollLogs := make([]model.UserPollLogWithSentence, len(pollLogs))
// 并发获取句子
errGroup := new(errgroup.Group)
for i, v := range pollLogs {
sentence, err := service.Hitokoto().GetHitokotoV1SchemaByUUID(ctx, v.SentenceUuid)
if err != nil {
return nil, err
}
userPollLogs[i] = model.UserPollLogWithSentence{
UserPollLog: model.UserPollLog{
Point: v.Point,
SentenceUUID: v.SentenceUuid,
Method: consts.PollMethod(v.Type),
Comment: v.Comment,
CreatedAt: (*vtime.Time)(v.CreatedAt),
UpdatedAt: (*vtime.Time)(v.UpdatedAt),
},
Sentence: sentence,
}
index, value := i, v // 解决并发问题
errGroup.Go(func() error {
sentence, e := service.Hitokoto().GetHitokotoV1SchemaByUUID(ctx, value.SentenceUuid)
if e != nil {
return e
}
userPollLogs[index] = model.UserPollLogWithSentence{
UserPollLog: model.UserPollLog{
Point: value.Point,
SentenceUUID: value.SentenceUuid,
Method: consts.PollMethod(value.Type),
Comment: value.Comment,
CreatedAt: (*vtime.Time)(value.CreatedAt),
UpdatedAt: (*vtime.Time)(value.UpdatedAt),
},
Sentence: sentence,
}
return nil
})
}
err = errGroup.Wait()
if err != nil {
return nil, err
}

out.Collection = userPollLogs
return out, nil
}

0 comments on commit 6db769f

Please sign in to comment.