Skip to content

Commit

Permalink
add IsFavorite协程
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwin committed Feb 21, 2023
1 parent 2fd408e commit ab07810
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion biz/handler/core/feed_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions biz/handler/core/login_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions biz/handler/core/register_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions kitex_server/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ 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 @@ -170,7 +169,7 @@ func GetVideosFavoriteCountMap(idSet []int64, respVideosFavoriteCountMap chan ma
}

// 传入videoId切片和当前用户id,批量查询喜欢情况
func GetIsFavoriteMap() (idSet []int64, currentUser int64, respIsFavoriteMap chan map[int64]bool, wg *sync.WaitGroup, errChan chan error) {
func GetIsFavoriteMap(idSet []int64, currentUser int64, respIsFavoriteMap chan map[int64]bool, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

res, err := FavoriteClient.FavoriteRelationsMethod(context.Background(), &favorite.FavoriteRelationsReq{
Expand All @@ -182,8 +181,6 @@ func GetIsFavoriteMap() (idSet []int64, currentUser int64, respIsFavoriteMap cha
errChan <- err
return
}
respIsFavoriteMap <- res.IsFavorites
respIsFavoriteMap <- res.IsFavoriteMap
errChan <- nil
// res, err := FavoriteClient.FavoriteRelationMethod(context.Background(), &favorite.FavoriteRelationReq{})
return
}
11 changes: 6 additions & 5 deletions kitex_server/relationservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"sync"

"strconv"

"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/douyin/biz/model/relation"
relationserver "github.com/ClubWeGo/relationmicro/kitex_gen/relation"
"github.com/prometheus/common/log"
"strconv"
)

// 响应码
Expand Down Expand Up @@ -190,10 +191,10 @@ func VerifyFollowParam(myUid int64, targetUid int64, actionType int32) *string {
return nil
}


// TODO : .GetIsFollowMapByUserIdSet
func GetIsFollowMapByUserIdSet(uid int64, idSet []int64) (isFollowMap map[int64]bool, err error) {
return nil, nil
}

// 协程接口

Expand Down Expand Up @@ -236,7 +237,7 @@ func GetRelationMap(idSet []int64, currentUser int64, respRelationMap chan map[i
}(id)
}
wgRelation.Wait()
for range idSet {
for range idSet { // 检查协程是否出错,出错直接按请求失败处理
err := <-insideErrChan
if err != nil {
respRelationMap <- map[int64]FollowInfoWithId{}
Expand All @@ -251,8 +252,8 @@ func GetRelationMap(idSet []int64, currentUser int64, respRelationMap chan map[i
result[data.Id] = data
}

respRelationMap <- result //没有显式出错,但是没有值
errChan <- errors.New("relation server GetFollowInfoMethod error: 微服务调用成功,但是没有查到值")
respRelationMap <- result // 返回查询结构
errChan <- nil
}

// kitex relationserver 数据传输 user -> kitex 回显 core.User
Expand Down
4 changes: 2 additions & 2 deletions kitex_server/userservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
wgUser.Add(1)
go GetRelationMap(idSet, currentUser, respRelationMap, wgUser, respRelationMapError)

// 批量查询TotalFavourited, FavoriteCount,传入查询的userId切片
// 批量查询TotalFavourited, FavoriteCount,传入查询的userId切片 从favorite服务
respUsersFavoriteCountMap := make(chan map[int64][]int64, 1) // [FavoriteCount FavoritedCount]
defer close(respRelationMap)
defer close(respUsersFavoriteCountMap)
respUsersFavoriteCountMapError := make(chan error, 1)
defer close(respUsersFavoriteCountMapError)
wgUser.Add(1)
Expand Down
16 changes: 11 additions & 5 deletions kitex_server/videoservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i
wgVideo.Add(1)
go GetVideosFavoriteCountMap(idSet, respVideosFavoriteCountMap, wgVideo, respVideosFavoriteCountMapError)

// TODO: @weitao
// 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务

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

IsFavoriteMap := <-respIsFavoriteMap
err = <-respIsFavoriteMapError
if err != nil {
errSlice = append(errSlice, err)
}

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

// 更新数据
Expand All @@ -87,7 +94,7 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i
videoLatestMap[id] = core.Video{ // 视频id对应的Video存储查到的关键字段
FavoriteCount: VideosFavoriteCountMap[id], // TODO:从拿到的MAP数据更新
CommentCount: 0, // TODO:从拿到的MAP数据更新
IsFavorite: false, // TODO:从拿到的MAP数据更新
IsFavorite: IsFavoriteMap[id], // TODO:从拿到的MAP数据更新
}
}
respVideoMap <- videoLatestMap // 返回数据
Expand Down Expand Up @@ -140,7 +147,6 @@ func GetFeed(latestTime int64, currentUserId int64, limit int32) (resultList []*
// 处理协程错误
AuthorMap := <-respLatestAuthorMap
errSlice := <-respLatestAuthorMapError

for _, errItem := range errSlice {
if errItem != nil {
return []*core.Video{}, 0, errItem
Expand Down Expand Up @@ -174,7 +180,7 @@ func GetFeed(latestTime int64, currentUserId int64, limit int32) (resultList []*
Title: video.Title,
}
}
return resultList, *r.NextTime, nil
return resultList, *r.NextTime, nil // 成功
}
return []*core.Video{}, 0, errors.New("向kitex请求feed失败")
}
Expand Down Expand Up @@ -239,7 +245,7 @@ func GetVideosByAuthorId(id int64) (resultList []*core.Video, err error) {
resultList = make([]*core.Video, len(r.VideoList))
for index, video := range r.VideoList {
// TODO:没有查询到的错误处理
author := AuthorMap[video.AuthorId]
author := AuthorMap[video.AuthorId] // 只有作者本人,查map其实无所谓
// TODO:设置机制,慢速同步其他服务的最新数据到user服务的主表,video的主表

resultList[index] = &core.Video{
Expand Down

0 comments on commit ab07810

Please sign in to comment.