添加 API 限流和支付渠道熔断
This commit is contained in:
@@ -338,7 +338,7 @@ func (c lakalaChannel) VerifyNotify(params map[string]string, rawPayload string,
|
||||
func buildChannelClient(dto *paymentconfig.ConfigDTO) (channelClient, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(dto.Provider)) {
|
||||
case "leshua":
|
||||
return newLeshuaChannel(leshua.Config{
|
||||
return withChannelBreaker("leshua", newLeshuaChannel(leshua.Config{
|
||||
GatewayURL: dto.GatewayURL,
|
||||
MerchantID: dto.MerchantID,
|
||||
SignKey: dto.SignKey,
|
||||
@@ -348,9 +348,9 @@ func buildChannelClient(dto *paymentconfig.ConfigDTO) (channelClient, error) {
|
||||
PayWay: firstNonEmpty(dto.PayWay, "ZFBZF"),
|
||||
JSPayFlag: firstNonEmpty(dto.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(dto.SignType, "MD5"),
|
||||
}), nil
|
||||
})), nil
|
||||
case "lakala":
|
||||
return newLakalaChannel(lakala.Config{
|
||||
return withChannelBreaker("lakala", newLakalaChannel(lakala.Config{
|
||||
GatewayURL: dto.GatewayURL,
|
||||
AppID: extraString(dto.ExtraConfig, "app_id"),
|
||||
SerialNo: extraString(dto.ExtraConfig, "serial_no"),
|
||||
@@ -365,7 +365,7 @@ func buildChannelClient(dto *paymentconfig.ConfigDTO) (channelClient, error) {
|
||||
JSPayFlag: firstNonEmpty(dto.JSPayFlag, "2"),
|
||||
PayMode: extraString(dto.ExtraConfig, "pay_mode"),
|
||||
OrderExpireMinutes: extraInt(dto.ExtraConfig, "order_expire_minutes"),
|
||||
}), nil
|
||||
})), nil
|
||||
case "mock":
|
||||
return nil, nil
|
||||
default:
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/sony/gobreaker/v2"
|
||||
)
|
||||
|
||||
type breakerChannel struct {
|
||||
next channelClient
|
||||
createPayment *gobreaker.CircuitBreaker[*channelCreatePaymentResponse]
|
||||
queryPayment *gobreaker.CircuitBreaker[*channelQueryPaymentResponse]
|
||||
createRefund *gobreaker.CircuitBreaker[*channelCreateRefundResponse]
|
||||
queryRefund *gobreaker.CircuitBreaker[*channelQueryRefundResponse]
|
||||
}
|
||||
|
||||
func withChannelBreaker(name string, next channelClient) channelClient {
|
||||
if next == nil {
|
||||
return nil
|
||||
}
|
||||
return breakerChannel{
|
||||
next: next,
|
||||
createPayment: newPaymentBreaker[*channelCreatePaymentResponse](name + ".create_payment"),
|
||||
queryPayment: newPaymentBreaker[*channelQueryPaymentResponse](name + ".query_payment"),
|
||||
createRefund: newPaymentBreaker[*channelCreateRefundResponse](name + ".create_refund"),
|
||||
queryRefund: newPaymentBreaker[*channelQueryRefundResponse](name + ".query_refund"),
|
||||
}
|
||||
}
|
||||
|
||||
func newPaymentBreaker[T any](name string) *gobreaker.CircuitBreaker[T] {
|
||||
return gobreaker.NewCircuitBreaker[T](gobreaker.Settings{
|
||||
Name: name,
|
||||
MaxRequests: 3,
|
||||
Timeout: 10 * time.Second,
|
||||
ReadyToTrip: func(counts gobreaker.Counts) bool {
|
||||
return counts.ConsecutiveFailures >= 5
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (c breakerChannel) CreatePayment(ctx context.Context, req channelCreatePaymentRequest) (*channelCreatePaymentResponse, error) {
|
||||
return c.createPayment.Execute(func() (*channelCreatePaymentResponse, error) {
|
||||
return c.next.CreatePayment(ctx, req)
|
||||
})
|
||||
}
|
||||
|
||||
func (c breakerChannel) QueryPayment(ctx context.Context, thirdOrderID string, providerOrderID string) (*channelQueryPaymentResponse, error) {
|
||||
return c.queryPayment.Execute(func() (*channelQueryPaymentResponse, error) {
|
||||
return c.next.QueryPayment(ctx, thirdOrderID, providerOrderID)
|
||||
})
|
||||
}
|
||||
|
||||
func (c breakerChannel) CreateRefund(ctx context.Context, req channelCreateRefundRequest) (*channelCreateRefundResponse, error) {
|
||||
return c.createRefund.Execute(func() (*channelCreateRefundResponse, error) {
|
||||
return c.next.CreateRefund(ctx, req)
|
||||
})
|
||||
}
|
||||
|
||||
func (c breakerChannel) QueryRefund(ctx context.Context, req channelQueryRefundRequest) (*channelQueryRefundResponse, error) {
|
||||
return c.queryRefund.Execute(func() (*channelQueryRefundResponse, error) {
|
||||
return c.next.QueryRefund(ctx, req)
|
||||
})
|
||||
}
|
||||
|
||||
func (c breakerChannel) VerifyNotify(params map[string]string, rawPayload string, contentType string, authorization string) (channelVerifyNotifyResult, error) {
|
||||
return c.next.VerifyNotify(params, rawPayload, contentType, authorization)
|
||||
}
|
||||
Reference in New Issue
Block a user