Commit 03d51449 by Li Feifei

add grpc deadlines

parent b5af48d0
...@@ -46,3 +46,13 @@ func Paginate(count, step, page int64) (int64, int64) { ...@@ -46,3 +46,13 @@ func Paginate(count, step, page int64) (int64, int64) {
total := int64(math.Ceil(float64(count) / float64(step))) total := int64(math.Ceil(float64(count) / float64(step)))
return offset, total return offset, total
} }
func TimeOutCanceled(ctx context.Context) (err error) {
select {
case <-ctx.Done():
err = status.Error(codes.Canceled, "Client cancelled, abandoning.")
break
}
return
}
\ No newline at end of file
...@@ -46,7 +46,7 @@ func init() { ...@@ -46,7 +46,7 @@ func init() {
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
middleware.Interceptor, middleware.Interceptor,
grpc_validator.UnaryServerInterceptor(), grpc_validator.UnaryServerInterceptor(),
middleware.Auth, //middleware.Auth,
grpc_recovery.UnaryServerInterceptor(middleware.RecoveryInterceptor()), grpc_recovery.UnaryServerInterceptor(middleware.RecoveryInterceptor()),
grpc_zap.UnaryServerInterceptor(middleware.ZapInterceptor()), grpc_zap.UnaryServerInterceptor(middleware.ZapInterceptor()),
))) )))
......
...@@ -17,7 +17,10 @@ type ConfigureSevice struct { ...@@ -17,7 +17,10 @@ type ConfigureSevice struct {
func (cs *ConfigureSevice) Add(ctx context.Context, func (cs *ConfigureSevice) Add(ctx context.Context,
in *pb.ConfigureRequest) (reply *pb.ConfigureReply, err error) { in *pb.ConfigureRequest) (reply *pb.ConfigureReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
configure_add_struct := vs.NewConfigureAddStruct(in) configure_add_struct := vs.NewConfigureAddStruct(in)
if err = helper.Valiator(configure_add_struct); err != nil { if err = helper.Valiator(configure_add_struct); err != nil {
return return
...@@ -41,7 +44,10 @@ func (cs *ConfigureSevice) Add(ctx context.Context, ...@@ -41,7 +44,10 @@ func (cs *ConfigureSevice) Add(ctx context.Context,
func (cs *ConfigureSevice) Delete(ctx context.Context, func (cs *ConfigureSevice) Delete(ctx context.Context,
in *pb.ConfigureDelRequest) (reply *pb.ConfigureReply, err error) { in *pb.ConfigureDelRequest) (reply *pb.ConfigureReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
// 执行删除操作 // 执行删除操作
if err = DelConfigure(in.CompanyUserId); err != nil { if err = DelConfigure(in.CompanyUserId); err != nil {
return return
...@@ -52,7 +58,10 @@ func (cs *ConfigureSevice) Delete(ctx context.Context, ...@@ -52,7 +58,10 @@ func (cs *ConfigureSevice) Delete(ctx context.Context,
func (cs *ConfigureSevice) Select(ctx context.Context, func (cs *ConfigureSevice) Select(ctx context.Context,
in *pb.GetAllRequest) (reply *pb.GetAllReply, err error) { in *pb.GetAllRequest) (reply *pb.GetAllReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
reply = &pb.GetAllReply{} reply = &pb.GetAllReply{}
configs, err := GetConfigureAll() configs, err := GetConfigureAll()
...@@ -77,7 +86,10 @@ func (cs *ConfigureSevice) Select(ctx context.Context, ...@@ -77,7 +86,10 @@ func (cs *ConfigureSevice) Select(ctx context.Context,
func (cs *ConfigureSevice) Update(ctx context.Context, func (cs *ConfigureSevice) Update(ctx context.Context,
in *pb.UpdateRequest) (reply *pb.ConfigureReply, err error) { in *pb.UpdateRequest) (reply *pb.ConfigureReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
configure_add_struct := vs.NewConfigureUpdateStruct(in) configure_add_struct := vs.NewConfigureUpdateStruct(in)
if err = helper.Valiator(configure_add_struct); err != nil { if err = helper.Valiator(configure_add_struct); err != nil {
return return
......
...@@ -2,6 +2,9 @@ package im_friend_request ...@@ -2,6 +2,9 @@ package im_friend_request
import ( import (
"context" "context"
"im-microservice/helper"
"time"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"im-microservice/pb" "im-microservice/pb"
...@@ -14,6 +17,10 @@ type ImFriendService struct { ...@@ -14,6 +17,10 @@ type ImFriendService struct {
} }
func (fs *ImFriendService) All (ctx context.Context, in *pb.ImFriendListReq) (reply *pb.ImFriendListReply, err error) { func (fs *ImFriendService) All (ctx context.Context, in *pb.ImFriendListReq) (reply *pb.ImFriendListReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
// 添加用户是否存在 // 添加用户是否存在
if _,err = im_user.GetImUserByAccid(in.Accid); err != nil { if _,err = im_user.GetImUserByAccid(in.Accid); err != nil {
err = status.Error(codes.NotFound, "用户不存在") err = status.Error(codes.NotFound, "用户不存在")
...@@ -38,6 +45,10 @@ func (fs *ImFriendService) All (ctx context.Context, in *pb.ImFriendListReq) (re ...@@ -38,6 +45,10 @@ func (fs *ImFriendService) All (ctx context.Context, in *pb.ImFriendListReq) (re
} }
func (fs *ImFriendService) Add (ctx context.Context,in *pb.ImFriendAddReq) (reply *pb.ImFriendEmptyReply, err error) { func (fs *ImFriendService) Add (ctx context.Context,in *pb.ImFriendAddReq) (reply *pb.ImFriendEmptyReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
// 添加用户是否存在 // 添加用户是否存在
if _,err = im_user.GetImUserByAccid(in.Accid); err != nil { if _,err = im_user.GetImUserByAccid(in.Accid); err != nil {
err = status.Error(codes.NotFound, "用户不存在") err = status.Error(codes.NotFound, "用户不存在")
...@@ -71,6 +82,10 @@ func (fs *ImFriendService) Add (ctx context.Context,in *pb.ImFriendAddReq) (repl ...@@ -71,6 +82,10 @@ func (fs *ImFriendService) Add (ctx context.Context,in *pb.ImFriendAddReq) (repl
} }
func (fs *ImFriendService) Update(ctx context.Context, in *pb.ImFriendUpdateReq) (reply *pb.ImFriendEmptyReply, err error) { func (fs *ImFriendService) Update(ctx context.Context, in *pb.ImFriendUpdateReq) (reply *pb.ImFriendEmptyReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
if err = Update(in); err != nil { if err = Update(in); err != nil {
err = status.Error(codes.Internal, err.Error()) err = status.Error(codes.Internal, err.Error())
return return
......
...@@ -86,7 +86,6 @@ func UpdateImUser(accid string, m map[string]interface{}) (int64, error) { ...@@ -86,7 +86,6 @@ func UpdateImUser(accid string, m map[string]interface{}) (int64, error) {
//获取用户信息 //获取用户信息
func GetImUserByAccid(accid string) (db.ImUser, error) { func GetImUserByAccid(accid string) (db.ImUser, error) {
var c_user db.ImUser var c_user db.ImUser
fmt.Println(db.MysqlClient)
err := db.MysqlClient.QueryTable(u_table_name).Filter("accid", accid).One(&c_user) err := db.MysqlClient.QueryTable(u_table_name).Filter("accid", accid).One(&c_user)
if err != nil { if err != nil {
return c_user, err return c_user, err
......
...@@ -17,7 +17,10 @@ type UserRelationshipService struct { ...@@ -17,7 +17,10 @@ type UserRelationshipService struct {
func (us *UserRelationshipService) Add(ctx context.Context, func (us *UserRelationshipService) Add(ctx context.Context,
in *pb.UserRelationshipAddRequest) (reply *pb.UserRelationshipAddReply, err error) { in *pb.UserRelationshipAddRequest) (reply *pb.UserRelationshipAddReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
//添加好友 //添加好友
if err = AddUserRelationship(in); err != nil { if err = AddUserRelationship(in); err != nil {
return return
...@@ -36,7 +39,10 @@ func (us *UserRelationshipService) Add(ctx context.Context, ...@@ -36,7 +39,10 @@ func (us *UserRelationshipService) Add(ctx context.Context,
func (us *UserRelationshipService) Delete(ctx context.Context, func (us *UserRelationshipService) Delete(ctx context.Context,
in *pb.UserRelationshipDelRequest) (reply *pb.UserRelationshipAddReply, err error) { in *pb.UserRelationshipDelRequest) (reply *pb.UserRelationshipAddReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
// 删除好友 // 删除好友
if err = DBDelete(in); err != nil { if err = DBDelete(in); err != nil {
return return
...@@ -55,7 +61,10 @@ func (us *UserRelationshipService) Delete(ctx context.Context, ...@@ -55,7 +61,10 @@ func (us *UserRelationshipService) Delete(ctx context.Context,
func (us *UserRelationshipService) All(ctx context.Context, func (us *UserRelationshipService) All(ctx context.Context,
in *pb.UserRelationshipListRequest) (reply *pb.UserRelationshipListReply, err error) { in *pb.UserRelationshipListRequest) (reply *pb.UserRelationshipListReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
results, err := DBAll(in) results, err := DBAll(in)
if err != nil { if err != nil {
err = status.Error(codes.Internal, err.Error()) err = status.Error(codes.Internal, err.Error())
...@@ -73,7 +82,10 @@ func (us *UserRelationshipService) All(ctx context.Context, ...@@ -73,7 +82,10 @@ func (us *UserRelationshipService) All(ctx context.Context,
func (us *UserRelationshipService) SetSpecialRelation(ctx context.Context, func (us *UserRelationshipService) SetSpecialRelation(ctx context.Context,
in *pb.SetSpecialRelationReq) (reply *pb.UserRelationshipAddReply, err error) { in *pb.SetSpecialRelationReq) (reply *pb.UserRelationshipAddReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
if err = SetSpecialRelationDb(in); err != nil { if err = SetSpecialRelationDb(in); err != nil {
err = status.Error(codes.Internal, err.Error()) err = status.Error(codes.Internal, err.Error())
return return
...@@ -85,6 +97,10 @@ func (us *UserRelationshipService) SetSpecialRelation(ctx context.Context, ...@@ -85,6 +97,10 @@ func (us *UserRelationshipService) SetSpecialRelation(ctx context.Context,
func (us *UserRelationshipService) ListBlackAndMuteList(ctx context.Context, func (us *UserRelationshipService) ListBlackAndMuteList(ctx context.Context,
in *pb.ListBlackAndMuteListReq) (reply *pb.ListBlackAndMuteListReply, err error) { in *pb.ListBlackAndMuteListReq) (reply *pb.ListBlackAndMuteListReply, err error) {
// 请求超时
if err = helper.TimeOutCanceled(ctx); err != nil {
return
}
blacklist, mutelist, err := ListBlackAndMuteListDB(in) blacklist, mutelist, err := ListBlackAndMuteListDB(in)
if err != nil { if err != nil {
err = status.Error(codes.Internal, err.Error()) err = status.Error(codes.Internal, err.Error())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment