You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.5 KiB
76 lines
2.5 KiB
2 months ago
|
package rpc
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"runtime"
|
||
|
"time"
|
||
|
|
||
|
"github.com/TremblingV5/DouTok/kitex_gen/feed"
|
||
|
"github.com/TremblingV5/DouTok/kitex_gen/feed/feedservice"
|
||
|
"github.com/TremblingV5/DouTok/pkg/dtviper"
|
||
|
"github.com/TremblingV5/DouTok/pkg/errno"
|
||
|
"github.com/TremblingV5/DouTok/pkg/middleware"
|
||
|
"github.com/cloudwego/kitex/client"
|
||
|
"github.com/cloudwego/kitex/pkg/remote/trans/gonet"
|
||
|
"github.com/cloudwego/kitex/pkg/retry"
|
||
|
"github.com/cloudwego/kitex/pkg/rpcinfo"
|
||
|
"github.com/kitex-contrib/obs-opentelemetry/tracing"
|
||
|
etcd "github.com/kitex-contrib/registry-etcd"
|
||
|
)
|
||
|
|
||
|
var FeedClient feedservice.Client
|
||
|
|
||
|
// Feed RPC 客户端初始化
|
||
|
func initFeedRpc(Config *dtviper.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())
|
||
|
options := []client.Option{
|
||
|
client.WithMiddleware(middleware.CommonMiddleware),
|
||
|
client.WithInstanceMW(middleware.ClientMiddleware),
|
||
|
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 runtime.GOOS == "windows" {
|
||
|
options = append(options, client.WithTransHandlerFactory(gonet.NewCliTransHandlerFactory()))
|
||
|
} else {
|
||
|
options = append(options, client.WithMuxConnection(1)) // mux
|
||
|
}
|
||
|
c, err := feedservice.NewClient(
|
||
|
ServiceName,
|
||
|
options...,
|
||
|
)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
FeedClient = c
|
||
|
}
|
||
|
|
||
|
// 传递 获取视频流操作 的上下文, 并获取 RPC Server 端的响应.
|
||
|
func GetUserFeed(ctx context.Context, feedClient feedservice.Client, req *feed.DouyinFeedRequest) (resp *feed.DouyinFeedResponse, err error) {
|
||
|
resp, err = feedClient.GetUserFeed(ctx, req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if resp.StatusCode != 0 {
|
||
|
return nil, errno.NewErrNo(int(resp.StatusCode), resp.StatusMsg)
|
||
|
}
|
||
|
return resp, nil
|
||
|
}
|