排行榜长度修改、性能优化
All checks were successful
/ build-services (app/auth_service/Dockerfile, auth, auth) (push) Successful in 38s
/ build-services (app/ecpm_service/Dockerfile, ecpm, ecpm) (push) Successful in 36s
/ build-services (app/ranking_service/Dockerfile, ranking, ranking) (push) Successful in 42s
/ build-services (app/user_service/Dockerfile, user, user) (push) Successful in 38s
/ start-services (push) Successful in 5s

This commit is contained in:
xiabin 2025-02-18 09:56:10 +08:00
parent f37792b13f
commit a7ca717cf3
10 changed files with 355 additions and 65 deletions

View File

@ -2,11 +2,14 @@ package logic
import (
"context"
"errors"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/internal/gen/dao/model"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/internal/logic/rankings"
"strconv"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/internal/svc"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/ranking"
"github.com/redis/go-redis/v9"
"strconv"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
@ -28,10 +31,59 @@ func NewAddUserGameScoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
func (l *AddUserGameScoreLogic) AddUserGameScore(in *ranking.AddUserGameScoreRequest) (*ranking.BaseResult, error) {
cacheKey := rankings.GetRankingsCacheKey(in.AppId, in.Type)
_, err := l.svcCtx.RedisRanking.IncrScore(l.ctx, cacheKey, strconv.Itoa(int(in.UserId)), float64(in.Score))
//todo 需要寻找新的方案
fScore, err := l.svcCtx.RedisRanking.GetScore(l.ctx, cacheKey, strconv.Itoa(int(in.UserId)))
if err != nil && !errors.Is(err, redis.Nil) {
return nil, err
}
//转为uint64
score := uint32(uint64(fScore) >> 32)
score += in.Score
gs := l.svcCtx.Query.GameScore
if errors.Is(err, redis.Nil) {
err = gs.WithContext(l.ctx).Create(&model.GameScore{
AppAccount: in.AppId,
UserID: in.UserId,
T: in.Type,
Score: score, // 初始值会被 ON CONFLICT 中的表达式覆盖
})
} else {
_, err = gs.WithContext(l.ctx).Where(
gs.AppAccount.Eq(in.AppId),
gs.T.Eq(in.Type),
gs.UserID.Eq(in.UserId),
).Update(gs.Score, score)
}
if err != nil {
return nil, err
}
l.svcCtx.RedisRanking.SetList(l.ctx, cacheKey, redis.Z{
Score: float64(uint64(score)<<32 + uint64(time.Now().Unix())),
Member: in.UserId,
})
//err = l.svcCtx.Query.Transaction(func(tx *query.Query) error {
// gs := tx.GameScore
//
// // 使用冲突处理子句
// return gs.WithContext(l.ctx).Clauses(clause.OnConflict{
// Columns: []clause.Column{
// {Name: "app_account"}, // 确保这些字段有唯一约束
// {Name: "user_id"},
// {Name: "t"},
// },
// DoUpdates: clause.Assignments(map[string]interface{}{
// "score": gorm.Expr("game_score.score + ?", in.Score),
// }),
// }).Create(&model.GameScore{
// AppAccount: in.AppId,
// UserID: in.UserId,
// T: in.Type,
// Score: in.Score, // 初始值会被 ON CONFLICT 中的表达式覆盖
// })
//})
return &ranking.BaseResult{}, nil
}

View File

@ -32,7 +32,7 @@ func (l *GetRankingListLogic) GetRankingList(in *ranking.GetRankingListRequest)
resp = new(ranking.GetRankingListResponse)
cacheKey := rankings.GetRankingsCacheKey(in.AppId, in.Type)
cacheData, err := l.svcCtx.RedisRanking.GetList(l.ctx, cacheKey, 0, 99)
cacheData, err := l.svcCtx.RedisRanking.GetList(l.ctx, cacheKey, 0, 98)
if err != nil {
return nil, err
}
@ -42,24 +42,29 @@ func (l *GetRankingListLogic) GetRankingList(in *ranking.GetRankingListRequest)
resp.RankingData = make([]*ranking.RankingList, 0, len(cacheData))
var userRank *ranking.RankingList
for i, datum := range cacheData {
userId, err := strconv.Atoi(datum.Member.(string))
userIds := make([]uint64, 0, len(cacheData))
for _, datum := range cacheData {
userId, err := strconv.ParseUint(datum.Member.(string), 10, 64)
if err != nil {
continue
}
//查询用户数据FindOne带缓存
userModel, err := l.svcCtx.UserServiceClient.GetUserById(l.ctx, &user.UserId{UserId: uint64(userId)})
if err != nil {
return nil, err
}
userIds = append(userIds, userId)
}
res, err := l.svcCtx.UserServiceClient.GetUserIds(l.ctx, &user.UserIds{UserId: userIds})
if err != nil {
return nil, err
}
for i, datum := range res.UserList {
data := &ranking.RankingList{
Nickname: userModel.Nickname,
Avatar: userModel.Avatar,
Score: uint32(uint64(datum.Score) >> 32),
Nickname: datum.Nickname,
Avatar: datum.Avatar,
Score: uint32(uint64(cacheData[i].Score) >> 32),
Rank: uint32(i) + 1,
Self: userModel.ID == in.UserId,
Self: datum.ID == in.UserId,
}
if userModel.ID == in.UserId {
if datum.ID == in.UserId {
flag = true
userRank = data
}
@ -71,7 +76,7 @@ func (l *GetRankingListLogic) GetRankingList(in *ranking.GetRankingListRequest)
if err != nil {
//如果没有找到,则创建一个空的数据
if errors.Is(err, redis.Nil) {
resp.RankingData = append(resp.RankingData, new(ranking.RankingList))
resp.RankingData = append(resp.RankingData, &ranking.RankingList{Self: true})
return resp, nil
}
return nil, err

View File

@ -424,7 +424,7 @@ type AddUserGameScoreRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Type uint32 `protobuf:"varint,1,opt,name=type,proto3" json:"type,omitempty"`
AppId uint32 `protobuf:"varint,2,opt,name=appId,proto3" json:"appId,omitempty"`
UserId uint32 `protobuf:"varint,3,opt,name=userId,proto3" json:"userId,omitempty"`
UserId uint64 `protobuf:"varint,3,opt,name=userId,proto3" json:"userId,omitempty"`
Score uint32 `protobuf:"varint,4,opt,name=score,proto3" json:"score,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
@ -474,7 +474,7 @@ func (x *AddUserGameScoreRequest) GetAppId() uint32 {
return 0
}
func (x *AddUserGameScoreRequest) GetUserId() uint32 {
func (x *AddUserGameScoreRequest) GetUserId() uint64 {
if x != nil {
return x.UserId
}
@ -647,7 +647,7 @@ var file_ranking_service_proto_rawDesc = string([]byte{
0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x74,
0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01,
0x28, 0x0d, 0x52, 0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65,
0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49,
0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49,
0x64, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d,
0x52, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x22, 0x40, 0x0a, 0x14, 0x52, 0x65, 0x6d, 0x6f, 0x76,
0x65, 0x52, 0x61, 0x6e, 0x6b, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,

View File

@ -48,7 +48,7 @@ message BaseResult {
message AddUserGameScoreRequest{
uint32 type = 1;
uint32 appId = 2;
uint32 userId = 3;
uint64 userId = 3;
uint32 score = 4;
}

View File

@ -0,0 +1,63 @@
package logic
import (
"context"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/user_service/internal/svc"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/user_service/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserIdsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetUserIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserIdsLogic {
return &GetUserIdsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// GetUserIds 通过 ID 获取用户
func (l *GetUserIdsLogic) GetUserIds(in *user.UserIds) (res *user.UserList, err error) {
if len(in.UserId) == 0 {
return nil, nil
}
userQuery := l.svcCtx.Query.User
users, err := userQuery.WithContext(l.ctx).Where(userQuery.ID.In(in.UserId...)).Find()
if err != nil {
return nil, err
}
var uMap = make(map[uint64]int)
for i := range users {
uMap[users[i].ID] = i
}
list := make([]*user.User, 0, len(users))
for _, id := range in.UserId {
i, ok := uMap[id]
var userModel *user.User
if ok {
userModel = &user.User{
ID: users[i].ID,
Nickname: users[i].Nickname,
Avatar: users[i].Avatar,
IsNew: users[i].IsNew == 1,
}
} else {
userModel = &user.User{ID: id}
}
list = append(list, userModel)
}
res = &user.UserList{
UserList: list,
}
return
}

View File

@ -57,3 +57,9 @@ func (s *UserServiceServer) GetUserById(ctx context.Context, in *user.UserId) (*
l := logic.NewGetUserByIdLogic(ctx, s.svcCtx)
return l.GetUserById(in)
}
// GetUserIds 通过 ID 获取用户
func (s *UserServiceServer) GetUserIds(ctx context.Context, in *user.UserIds) (*user.UserList, error) {
l := logic.NewGetUserIdsLogic(ctx, s.svcCtx)
return l.GetUserIds(in)
}

View File

@ -413,6 +413,94 @@ func (x *User) GetIsNew() bool {
return false
}
type UserIds struct {
state protoimpl.MessageState `protogen:"open.v1"`
UserId []uint64 `protobuf:"varint,1,rep,packed,name=UserId,proto3" json:"UserId,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *UserIds) Reset() {
*x = UserIds{}
mi := &file_user_service_proto_msgTypes[8]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *UserIds) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UserIds) ProtoMessage() {}
func (x *UserIds) ProtoReflect() protoreflect.Message {
mi := &file_user_service_proto_msgTypes[8]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UserIds.ProtoReflect.Descriptor instead.
func (*UserIds) Descriptor() ([]byte, []int) {
return file_user_service_proto_rawDescGZIP(), []int{8}
}
func (x *UserIds) GetUserId() []uint64 {
if x != nil {
return x.UserId
}
return nil
}
type UserList struct {
state protoimpl.MessageState `protogen:"open.v1"`
UserList []*User `protobuf:"bytes,1,rep,name=UserList,proto3" json:"UserList,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *UserList) Reset() {
*x = UserList{}
mi := &file_user_service_proto_msgTypes[9]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *UserList) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UserList) ProtoMessage() {}
func (x *UserList) ProtoReflect() protoreflect.Message {
mi := &file_user_service_proto_msgTypes[9]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UserList.ProtoReflect.Descriptor instead.
func (*UserList) Descriptor() ([]byte, []int) {
return file_user_service_proto_rawDescGZIP(), []int{9}
}
func (x *UserList) GetUserList() []*User {
if x != nil {
return x.UserList
}
return nil
}
var File_user_service_proto protoreflect.FileDescriptor
var file_user_service_proto_rawDesc = string([]byte{
@ -444,32 +532,42 @@ var file_user_service_proto_rawDesc = string([]byte{
0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16,
0x0a, 0x06, 0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06,
0x41, 0x76, 0x61, 0x74, 0x61, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x18,
0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x32, 0xff, 0x02, 0x0a,
0x0c, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x35, 0x0a,
0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72,
0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x75,
0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x46, 0x0a, 0x07, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x12,
0x1c, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x53,
0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e,
0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x53, 0x65, 0x74,
0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x43, 0x0a, 0x0a,
0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1f, 0x2e, 0x75, 0x73, 0x65,
0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x75, 0x73,
0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x22, 0x21, 0x0a, 0x07,
0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49,
0x64, 0x18, 0x01, 0x20, 0x03, 0x28, 0x04, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22,
0x3a, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x2e, 0x0a, 0x08, 0x55,
0x73, 0x65, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x12, 0x2e,
0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65,
0x72, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x32, 0xbc, 0x03, 0x0a, 0x0c,
0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x35, 0x0a, 0x04,
0x50, 0x69, 0x6e, 0x67, 0x12, 0x15, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x75, 0x73,
0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x12, 0x46, 0x0a, 0x07, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1c,
0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x53, 0x65,
0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x75,
0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x53, 0x65, 0x74, 0x55,
0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x43, 0x0a, 0x0a, 0x43,
0x72, 0x65, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1f, 0x2e, 0x75, 0x73, 0x65, 0x72,
0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x55,
0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x75, 0x73, 0x65,
0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64,
0x12, 0x3c, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x55,
0x73, 0x65, 0x72, 0x12, 0x13, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x14, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f,
0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x34,
0x0a, 0x08, 0x46, 0x69, 0x6e, 0x64, 0x42, 0x79, 0x49, 0x64, 0x12, 0x14, 0x2e, 0x75, 0x73, 0x65,
0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64,
0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e,
0x55, 0x73, 0x65, 0x72, 0x12, 0x37, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x42,
0x79, 0x49, 0x64, 0x12, 0x14, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72,
0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x12, 0x3b, 0x0a,
0x0a, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x15, 0x2e, 0x75, 0x73,
0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49,
0x64, 0x12, 0x3c, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x45, 0x6d, 0x70, 0x74, 0x79,
0x55, 0x73, 0x65, 0x72, 0x12, 0x13, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x14, 0x2e, 0x75, 0x73, 0x65, 0x72,
0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12,
0x34, 0x0a, 0x08, 0x46, 0x69, 0x6e, 0x64, 0x42, 0x79, 0x49, 0x64, 0x12, 0x14, 0x2e, 0x75, 0x73,
0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49,
0x64, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
0x2e, 0x55, 0x73, 0x65, 0x72, 0x12, 0x37, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72,
0x42, 0x79, 0x49, 0x64, 0x12, 0x14, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65,
0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x42, 0x08,
0x5a, 0x06, 0x2e, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x64, 0x73, 0x1a, 0x16, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63,
0x65, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f,
0x75, 0x73, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
})
var (
@ -484,7 +582,7 @@ func file_user_service_proto_rawDescGZIP() []byte {
return file_user_service_proto_rawDescData
}
var file_user_service_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_user_service_proto_msgTypes = make([]protoimpl.MessageInfo, 10)
var file_user_service_proto_goTypes = []any{
(*Request)(nil), // 0: user_service.Request
(*Response)(nil), // 1: user_service.Response
@ -494,25 +592,30 @@ var file_user_service_proto_goTypes = []any{
(*UserId)(nil), // 5: user_service.UserId
(*Empty)(nil), // 6: user_service.Empty
(*User)(nil), // 7: user_service.User
(*UserIds)(nil), // 8: user_service.UserIds
(*UserList)(nil), // 9: user_service.UserList
}
var file_user_service_proto_depIdxs = []int32{
0, // 0: user_service.user_service.Ping:input_type -> user_service.Request
2, // 1: user_service.user_service.SetUser:input_type -> user_service.SetUserRequest
4, // 2: user_service.user_service.CreateUser:input_type -> user_service.CreateUserRequest
6, // 3: user_service.user_service.CreateEmptyUser:input_type -> user_service.Empty
5, // 4: user_service.user_service.FindById:input_type -> user_service.UserId
5, // 5: user_service.user_service.GetUserById:input_type -> user_service.UserId
1, // 6: user_service.user_service.Ping:output_type -> user_service.Response
3, // 7: user_service.user_service.SetUser:output_type -> user_service.SetUserResponse
5, // 8: user_service.user_service.CreateUser:output_type -> user_service.UserId
5, // 9: user_service.user_service.CreateEmptyUser:output_type -> user_service.UserId
7, // 10: user_service.user_service.FindById:output_type -> user_service.User
7, // 11: user_service.user_service.GetUserById:output_type -> user_service.User
6, // [6:12] is the sub-list for method output_type
0, // [0:6] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
7, // 0: user_service.UserList.UserList:type_name -> user_service.User
0, // 1: user_service.user_service.Ping:input_type -> user_service.Request
2, // 2: user_service.user_service.SetUser:input_type -> user_service.SetUserRequest
4, // 3: user_service.user_service.CreateUser:input_type -> user_service.CreateUserRequest
6, // 4: user_service.user_service.CreateEmptyUser:input_type -> user_service.Empty
5, // 5: user_service.user_service.FindById:input_type -> user_service.UserId
5, // 6: user_service.user_service.GetUserById:input_type -> user_service.UserId
8, // 7: user_service.user_service.GetUserIds:input_type -> user_service.UserIds
1, // 8: user_service.user_service.Ping:output_type -> user_service.Response
3, // 9: user_service.user_service.SetUser:output_type -> user_service.SetUserResponse
5, // 10: user_service.user_service.CreateUser:output_type -> user_service.UserId
5, // 11: user_service.user_service.CreateEmptyUser:output_type -> user_service.UserId
7, // 12: user_service.user_service.FindById:output_type -> user_service.User
7, // 13: user_service.user_service.GetUserById:output_type -> user_service.User
9, // 14: user_service.user_service.GetUserIds:output_type -> user_service.UserList
8, // [8:15] is the sub-list for method output_type
1, // [1:8] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_user_service_proto_init() }
@ -526,7 +629,7 @@ func file_user_service_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_user_service_proto_rawDesc), len(file_user_service_proto_rawDesc)),
NumEnums: 0,
NumMessages: 8,
NumMessages: 10,
NumExtensions: 0,
NumServices: 1,
},

View File

@ -25,6 +25,7 @@ const (
UserService_CreateEmptyUser_FullMethodName = "/user_service.user_service/CreateEmptyUser"
UserService_FindById_FullMethodName = "/user_service.user_service/FindById"
UserService_GetUserById_FullMethodName = "/user_service.user_service/GetUserById"
UserService_GetUserIds_FullMethodName = "/user_service.user_service/GetUserIds"
)
// UserServiceClient is the client API for UserService service.
@ -42,6 +43,8 @@ type UserServiceClient interface {
FindById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error)
// GetUserById 通过 ID 获取用户
GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error)
// GetUserIds 通过 ID 获取用户
GetUserIds(ctx context.Context, in *UserIds, opts ...grpc.CallOption) (*UserList, error)
}
type userServiceClient struct {
@ -112,6 +115,16 @@ func (c *userServiceClient) GetUserById(ctx context.Context, in *UserId, opts ..
return out, nil
}
func (c *userServiceClient) GetUserIds(ctx context.Context, in *UserIds, opts ...grpc.CallOption) (*UserList, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(UserList)
err := c.cc.Invoke(ctx, UserService_GetUserIds_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// UserServiceServer is the server API for UserService service.
// All implementations must embed UnimplementedUserServiceServer
// for forward compatibility.
@ -127,6 +140,8 @@ type UserServiceServer interface {
FindById(context.Context, *UserId) (*User, error)
// GetUserById 通过 ID 获取用户
GetUserById(context.Context, *UserId) (*User, error)
// GetUserIds 通过 ID 获取用户
GetUserIds(context.Context, *UserIds) (*UserList, error)
mustEmbedUnimplementedUserServiceServer()
}
@ -155,6 +170,9 @@ func (UnimplementedUserServiceServer) FindById(context.Context, *UserId) (*User,
func (UnimplementedUserServiceServer) GetUserById(context.Context, *UserId) (*User, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetUserById not implemented")
}
func (UnimplementedUserServiceServer) GetUserIds(context.Context, *UserIds) (*UserList, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetUserIds not implemented")
}
func (UnimplementedUserServiceServer) mustEmbedUnimplementedUserServiceServer() {}
func (UnimplementedUserServiceServer) testEmbeddedByValue() {}
@ -284,6 +302,24 @@ func _UserService_GetUserById_Handler(srv interface{}, ctx context.Context, dec
return interceptor(ctx, in, info, handler)
}
func _UserService_GetUserIds_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UserIds)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(UserServiceServer).GetUserIds(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: UserService_GetUserIds_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(UserServiceServer).GetUserIds(ctx, req.(*UserIds))
}
return interceptor(ctx, in, info, handler)
}
// UserService_ServiceDesc is the grpc.ServiceDesc for UserService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
@ -315,6 +351,10 @@ var UserService_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetUserById",
Handler: _UserService_GetUserById_Handler,
},
{
MethodName: "GetUserIds",
Handler: _UserService_GetUserIds_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "user_service.proto",

View File

@ -39,6 +39,14 @@ message User {
bool IsNew = 4;
}
message UserIds {
repeated uint64 UserId = 1;
}
message UserList {
repeated User UserList = 1;
}
service user_service {
rpc Ping(Request) returns(Response);
@ -56,4 +64,7 @@ service user_service {
//GetUserById ID
rpc GetUserById(UserId) returns(User);
//GetUserIds ID
rpc GetUserIds(UserIds) returns(UserList);
}

View File

@ -22,6 +22,8 @@ type (
SetUserResponse = user.SetUserResponse
User = user.User
UserId = user.UserId
UserIds = user.UserIds
UserList = user.UserList
UserService interface {
Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
@ -35,6 +37,8 @@ type (
FindById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error)
// GetUserById 通过 ID 获取用户
GetUserById(ctx context.Context, in *UserId, opts ...grpc.CallOption) (*User, error)
// GetUserIds 通过 ID 获取用户
GetUserIds(ctx context.Context, in *UserIds, opts ...grpc.CallOption) (*UserList, error)
}
defaultUserService struct {
@ -82,3 +86,9 @@ func (m *defaultUserService) GetUserById(ctx context.Context, in *UserId, opts .
client := user.NewUserServiceClient(m.cli.Conn())
return client.GetUserById(ctx, in, opts...)
}
// GetUserIds 通过 ID 获取用户
func (m *defaultUserService) GetUserIds(ctx context.Context, in *UserIds, opts ...grpc.CallOption) (*UserList, error) {
client := user.NewUserServiceClient(m.cli.Conn())
return client.GetUserIds(ctx, in, opts...)
}