2026-04-06 17:08:51 +08:00

43 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package usergrpc
import (
"context"
"time"
"chatappgateway/internal/integration/upstream"
commonpb "gitea.haiyihy.com/hy/chatappcommon/proto"
)
// Client 封装用户服务 gRPC client并统一超时、重试和节点选择。
type Client struct {
timeout time.Duration
client commonpb.ChatAppUserServiceClient
pool *upstream.Pool
}
// New 根据单个底层 gRPC client 构造用户服务调用器。
func New(client commonpb.ChatAppUserServiceClient, timeout time.Duration) *Client {
return &Client{
timeout: timeout,
client: client,
}
}
// NewWithPool 根据下游节点池构造用户服务调用器。
func NewWithPool(pool *upstream.Pool) *Client {
return &Client{pool: pool}
}
// Register 调用用户服务注册接口。
func (c *Client) Register(ctx context.Context, request *commonpb.RegisterRequest) (*commonpb.RegisterResponse, error) {
if c.pool == nil {
callCtx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.client.Register(callCtx, request)
}
return upstream.Call(ctx, c.pool, func(callCtx context.Context, handle upstream.Handle) (*commonpb.RegisterResponse, error) {
return commonpb.NewChatAppUserServiceClient(handle.Conn).Register(callCtx, request)
})
}