-
Notifications
You must be signed in to change notification settings - Fork 38
/
relation.go
115 lines (104 loc) · 4.01 KB
/
relation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2022 a76yyyy && CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* @Author: a76yyyy q981331502@163.com
* @Date: 2022-06-12 23:16:36
* @LastEditors: a76yyyy q981331502@163.com
* @LastEditTime: 2022-06-18 23:46:09
* @FilePath: /tiktok/cmd/api/rpc/relation.go
* @Description: Relation RPC 客户端初始化及 相关 RPC 通信操作定义
*/
package rpc
import (
"context"
"fmt"
"time"
etcd "github.com/a76yyyy/registry-etcd"
"github.com/a76yyyy/tiktok/kitex_gen/relation"
"github.com/a76yyyy/tiktok/kitex_gen/relation/relationsrv"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/a76yyyy/tiktok/pkg/middleware"
"github.com/a76yyyy/tiktok/pkg/ttviper"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/kitex-contrib/obs-opentelemetry/provider"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
)
var relationClient relationsrv.Client
// Relation RPC 客户端初始化
func initRelationRpc(Config *ttviper.Config) {
EtcdAddress := fmt.Sprintf("%s:%d", Config.Viper.GetString("Etcd.Address"), Config.Viper.GetInt("Etcd.Port"))
r, err := etcd.NewEtcdResolver([]string{EtcdAddress})
if err != nil {
panic(err)
}
ServiceName := Config.Viper.GetString("Server.Name")
p := provider.NewOpenTelemetryProvider(
provider.WithServiceName(ServiceName),
provider.WithExportEndpoint("localhost:4317"),
provider.WithInsecure(),
)
defer p.Shutdown(context.Background())
c, err := relationsrv.NewClient(
ServiceName,
client.WithMiddleware(middleware.CommonMiddleware),
client.WithInstanceMW(middleware.ClientMiddleware),
client.WithMuxConnection(1), // mux
client.WithRPCTimeout(30*time.Second), // rpc timeout
client.WithConnectTimeout(30000*time.Millisecond), // conn timeout
client.WithFailureRetry(retry.NewFailurePolicy()), // retry
client.WithSuite(tracing.NewClientSuite()), // tracer
client.WithResolver(r), // resolver
// Please keep the same as provider.WithServiceName
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: ServiceName}),
)
if err != nil {
panic(err)
}
relationClient = c
}
// 传递 关注操作 的上下文, 并获取 RPC Server 端的响应.
func RelationAction(ctx context.Context, req *relation.DouyinRelationActionRequest) (resp *relation.DouyinRelationActionResponse, err error) {
resp, err = relationClient.RelationAction(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return nil, errno.NewErrNo(int(resp.StatusCode), *resp.StatusMsg)
}
return resp, nil
}
// 传递 获取正在关注列表操作 的上下文, 并获取 RPC Server 端的响应.
func RelationFollowList(ctx context.Context, req *relation.DouyinRelationFollowListRequest) (resp *relation.DouyinRelationFollowListResponse, err error) {
resp, err = relationClient.RelationFollowList(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return nil, errno.NewErrNo(int(resp.StatusCode), *resp.StatusMsg)
}
return resp, nil
}
// 传递 获取粉丝列表操作 的上下文, 并获取 RPC Server 端的响应.
func RelationFollowerList(ctx context.Context, req *relation.DouyinRelationFollowerListRequest) (resp *relation.DouyinRelationFollowerListResponse, err error) {
resp, err = relationClient.RelationFollowerList(ctx, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return nil, errno.NewErrNo(int(resp.StatusCode), *resp.StatusMsg)
}
return resp, nil
}