Skip to content

Commit

Permalink
add relation协程
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwin committed Feb 21, 2023
1 parent b2cc41d commit ab3a3c8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
63 changes: 60 additions & 3 deletions kitex_server/relationservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/douyin/biz/model/relation"
relationserver "github.com/ClubWeGo/relationmicro/kitex_gen/relation"
Expand Down Expand Up @@ -177,7 +179,62 @@ func VerifyFollowParam(myUid int64, targetUid int64, actionType int32) *string {
return nil
}

// TODO : .GetIsFollowSetByUserIdSet
func GetIsFollowSetByUserIdSet(idSet []int64) (isFollowSet []int64, err error) {
return []int64{}, nil
// 协程接口

type FollowInfoWithId struct {
Id int64
followInfo relationserver.FollowInfo
}

// 通过GetFollowInfoMethod批量获取FollowInfo,包装为Map
func GetRelationMap(idSet []int64, currentUser int64, respRelationMap chan map[int64]FollowInfoWithId, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

wgRelation := &sync.WaitGroup{}
insideResultChan := make(chan FollowInfoWithId, len(idSet))
insideErrChan := make(chan error, len(idSet))
for _, id := range idSet {
wgRelation.Add(1)
go func(userid int64) {
defer wgRelation.Done()
r, err := Relationclient.GetFollowInfoMethod(context.Background(), &relationserver.GetFollowInfoReq{
MyUid: &currentUser,
TargetUid: userid,
})
if err != nil {
insideResultChan <- FollowInfoWithId{} // 出错,传回一个空值
insideErrChan <- err
return
}
if r.StatusCode == 1 { // 0 error, 1 success, 2 参数不通过
followInfoWithId := FollowInfoWithId{
Id: userid,
followInfo: *r.FollowInfo,
}
insideResultChan <- followInfoWithId
insideErrChan <- nil
return // 成功
}
insideResultChan <- FollowInfoWithId{} //没有显式出错,但是没有值
insideErrChan <- nil
}(id)
}
wgRelation.Wait()
for range idSet {
err := <-insideErrChan
if err != nil {
respRelationMap <- map[int64]FollowInfoWithId{}
errChan <- err
return // 有协程查数据错误,直接报错返回
}
}
// 封装数据
result := make(map[int64]FollowInfoWithId, len(idSet))
for range idSet {
data := <-insideResultChan
result[data.Id] = data
}

respRelationMap <- result //没有显式出错,但是没有值
errChan <- errors.New("relation server GetFollowInfoMethod error: 微服务调用成功,但是没有查到值")
}
26 changes: 22 additions & 4 deletions kitex_server/userservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package kitex_server
import (
"context"
"errors"
"github.com/prometheus/common/log"
"strconv"
"sync"

"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro"
"github.com/ClubWeGo/videomicro/kitex_gen/videomicro"
"github.com/prometheus/common/log"
)

// 工具函数
Expand Down Expand Up @@ -90,6 +91,17 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
wgUser.Add(1)
go GetVideoCountMap(idSet, respVideoCountMap, wgUser, respVideoCountMapError)

// 批量查询FollowCount, FollowerCount,Is_follow 从relation服务
// Videoclient.GetVideoCountSetByIdUserSetMethod(context.Background(), &videomicro.GetVideoCountSetByIdUserSetReq{})
respRelationMap := make(chan map[int64]FollowInfoWithId, 1)
defer close(respRelationMap)
respRelationMapError := make(chan error, 1)
defer close(respRelationMapError)
wgUser.Add(1)
go GetRelationMap(idSet, currentUser, respRelationMap, wgUser, respRelationMapError)

// TODO : TotalFavourited, FavoriteCount,传入查询的userId切片,查对应这两个字段的切片,(结果需要携带UserId):从favorite服务

// 等待数据
wgUser.Wait()

Expand All @@ -106,6 +118,12 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
if err != nil {
errSlice = append(errSlice, err)
}

RelationMap := <-respRelationMap
err = <-respRelationMapError
if err != nil {
errSlice = append(errSlice, err)
}
// TODO: 其他协程的错误处理

errChan <- errSlice // 错误切片
Expand All @@ -115,9 +133,9 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
AuthorMap[id] = core.User{
ID: user.ID,
Name: user.Name,
FollowCount: 0, // TODO: 从获取的数据中拿
FollowerCount: 0, // TODO: 从获取的数据中拿
IsFollow: false, // TODO: 从获取的数据中拿
FollowCount: RelationMap[id].followInfo.FollowCount, // Relation服务 最新的followCount
FollowerCount: RelationMap[id].followInfo.FollowerCount, // Relation服务 最新的followerCount
IsFollow: RelationMap[id].followInfo.IsFollow, // Relation服务 最新的isFollow
Avatar: user.Avatar,
BackgroundImage: user.BackgroundImage,
Signature: user.Signature,
Expand Down
8 changes: 3 additions & 5 deletions kitex_server/videoservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i

wgVideo := &sync.WaitGroup{} // 本函数子协程的wg

// 批量查询视频的 被喜欢数 ,Favorite 从Favorite服务
// 批量查询 favorite_count, total_favourited 从favorite服务: kitex_server.FavoriteClient.UserFavoriteCountMethod()
// TODO: 结果要 videoId与对应的数据 map
// 批量查询视频的 被喜欢数 ,传入视频id的切片,返回对应的FavoriteCount的切片(需携带对应视频id) 从Favorite服务

// 批量查询 is_follow, 从relation服务; 传入目标userID和currentUser
// 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务

// 批量查询 follow_count, follower_cout 从relation服务
// 批量查询 is_favorite, 传入目标视频id切片和currentUser查is_favorite的切片(结果需要携带视频id,douyin里后续需要转成map):从favorite;

// 等待数据
wgVideo.Wait()
Expand Down

0 comments on commit ab3a3c8

Please sign in to comment.