43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package paygrpc
|
||
|
||
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.ChatAppPayServiceClient
|
||
pool *upstream.Pool
|
||
}
|
||
|
||
// New 根据单个底层 gRPC client 构造支付服务调用器。
|
||
func New(client commonpb.ChatAppPayServiceClient, timeout time.Duration) *Client {
|
||
return &Client{
|
||
timeout: timeout,
|
||
client: client,
|
||
}
|
||
}
|
||
|
||
// NewWithPool 根据下游节点池构造支付服务调用器。
|
||
func NewWithPool(pool *upstream.Pool) *Client {
|
||
return &Client{pool: pool}
|
||
}
|
||
|
||
// Pay 调用支付服务最小支付接口。
|
||
func (c *Client) Pay(ctx context.Context, request *commonpb.PayRequest) (*commonpb.PayResponse, error) {
|
||
if c.pool == nil {
|
||
callCtx, cancel := context.WithTimeout(ctx, c.timeout)
|
||
defer cancel()
|
||
return c.client.Pay(callCtx, request)
|
||
}
|
||
|
||
return upstream.Call(ctx, c.pool, func(callCtx context.Context, handle upstream.Handle) (*commonpb.PayResponse, error) {
|
||
return commonpb.NewChatAppPayServiceClient(handle.Conn).Pay(callCtx, request)
|
||
})
|
||
}
|