Skip to content

Commit

Permalink
add videoCommentCount协程
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwin committed Feb 21, 2023
1 parent b6929f4 commit f6c582f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
11 changes: 10 additions & 1 deletion kitex_server/initmicro.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package kitex_server

import (
"github.com/ClubWeGo/favoritemicro/kitex_gen/favorite/favoriteservice"
"log"

"github.com/ClubWeGo/favoritemicro/kitex_gen/favorite/favoriteservice"

"github.com/ClubWeGo/commentmicro/kitex_gen/comment/commentservice"
"github.com/ClubWeGo/relationmicro/kitex_gen/relation/combineservice"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro/userservice"
"github.com/ClubWeGo/videomicro/kitex_gen/videomicro/videoservice"
Expand All @@ -15,6 +17,7 @@ var Userclient userservice.Client
var Videoclient videoservice.Client
var Relationclient combineservice.Client
var FavoriteClient favoriteservice.Client
var CommentClient commentservice.Client

func Init(r discovery.Resolver) {
uc, err := userservice.NewClient("userservice", client.WithResolver(r))
Expand All @@ -40,4 +43,10 @@ func Init(r discovery.Resolver) {
log.Fatal(err)
}
FavoriteClient = fc

Cc, err := commentservice.NewClient("commentservice", client.WithResolver(r))
if err != nil {
log.Fatal(err)
}
CommentClient = Cc
}
18 changes: 18 additions & 0 deletions kitex_server/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync"

"github.com/ClubWeGo/commentmicro/kitex_gen/comment"
"github.com/ClubWeGo/douyin/pack"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro"
"github.com/ClubWeGo/videomicro/kitex_gen/videomicro"
Expand Down Expand Up @@ -134,6 +135,7 @@ func CountUserFavorite(ctx context.Context, uid int64) (int64, int64, error) {
return res.FavoriteCount, res.FavoritedCount, nil
}

// 协程接口
// 传入userId切片,批量查询user对应的favorite, total_favorited
// map[int64][]int64 [FavoriteCount FavoritedCount]
func GetUsersFavoriteCountMap(idSet []int64, respUsersFavoriteCountMap chan map[int64][]int64, wg *sync.WaitGroup, errChan chan error) {
Expand Down Expand Up @@ -184,3 +186,19 @@ func GetIsFavoriteMap(idSet []int64, currentUser int64, respIsFavoriteMap chan m
respIsFavoriteMap <- res.IsFavoriteMap
errChan <- nil
}

// 传入videoId切片,批量视频的评论数量
func GetCommentCountMap(idSet []int64, respIsFavoriteMap chan map[int64]int64, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

res, err := CommentClient.VideosFavoriteCountMethod(context.Background(), &comment.VideosCommentCountReq{
VideoIdList: idSet,
})
if err != nil {
respIsFavoriteMap <- map[int64]int64{}
errChan <- err
return
}
respIsFavoriteMap <- res.CommentCountMap
errChan <- nil
}
15 changes: 12 additions & 3 deletions kitex_server/videoservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i
wgVideo.Add(1)
go GetVideosFavoriteCountMap(idSet, respVideosFavoriteCountMap, wgVideo, respVideosFavoriteCountMapError)

// TODO: @weitao
// 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务
// 在此处类似上边的写法,写评论数的实现
respCommentCountMap := make(chan map[int64]int64, 1)
defer close(respCommentCountMap)
respCommentCountMapError := make(chan error, 1)
defer close(respCommentCountMapError)
wgVideo.Add(1)
go GetCommentCountMap(idSet, respCommentCountMap, wgVideo, respCommentCountMapError)

// 批量查询 is_favorite, 传入目标视频id切片和currentUser查is_favorite的切片(结果需要携带视频id,douyin里后续需要转成map):从favorite;
respIsFavoriteMap := make(chan map[int64]bool, 1)
Expand All @@ -87,7 +92,11 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i
errSlice = append(errSlice, err)
}

// @weitao 在此处类似上边的写法,把数据从你前面定义的数据chan里面拿到,把错误从对应的错误chan里面appen到errSlice里面
VideosCommentCountMap := <-respCommentCountMap
err = <-respCommentCountMapError
if err != nil {
errSlice = append(errSlice, err)
}

errChan <- errSlice // 记录错误的切片,至少应该返回一个空切片,否则chan会阻塞

Expand All @@ -96,7 +105,7 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i
for _, id := range idSet {
videoLatestMap[id] = core.Video{ // 视频id对应的Video存储查到的关键字段
FavoriteCount: VideosFavoriteCountMap[id], //
CommentCount: 0, // TODO: @weitao, 在此处从你返回的map里面读去对应id的评论数
CommentCount: VideosCommentCountMap[id], //
IsFavorite: IsFavoriteMap[id], //
}
}
Expand Down

0 comments on commit f6c582f

Please sign in to comment.