From ee85d3debeb06b8e8a80a3769cd1312eadbe817a Mon Sep 17 00:00:00 2001 From: ziming Date: Wed, 21 May 2025 14:19:43 +0800 Subject: [PATCH] kg --- internal/data/mixrepoimpl/cmb.go | 2 +- internal/data/mixrepoimpl/kx.go | 28 ++++++++++++++------ internal/data/wechatrepoimpl/cpn.go | 2 -- internal/pkg/request/request.go | 38 ++++++++++++++++++++-------- internal/pkg/request/request_test.go | 3 +-- 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/internal/data/mixrepoimpl/cmb.go b/internal/data/mixrepoimpl/cmb.go index 570e024..f8086d0 100644 --- a/internal/data/mixrepoimpl/cmb.go +++ b/internal/data/mixrepoimpl/cmb.go @@ -268,7 +268,7 @@ func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri st r := uri + "?" + uv.Encode() - _, bodyBytes, err := request.Post(ctx, r, nil, request.WithHeaders(h), request.WithTimeout(time.Second*20)) + _, bodyBytes, err := request.Post(ctx, r, nil, request.WithHeaders(h)) if err != nil { log.Errorf("请求掌上生活报错,url:%s,err:%v", r, err) return nil, err diff --git a/internal/data/mixrepoimpl/kx.go b/internal/data/mixrepoimpl/kx.go index 2262065..54d2530 100644 --- a/internal/data/mixrepoimpl/kx.go +++ b/internal/data/mixrepoimpl/kx.go @@ -14,26 +14,38 @@ import ( // KxMixRepoImpl kog 空港 type KxMixRepoImpl struct { bc *conf.Bootstrap + // 连接池复用(优化网络开销) + options *request.Options } func NewKxMixRepoImpl(bc *conf.Bootstrap) mixrepos.KxMixRepo { - return &KxMixRepoImpl{bc: bc} + h := http.Header{ + "Content-Type": []string{"application/json"}, + } + hc := &http.Client{ + Timeout: 15 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 150, // 最大空闲连接数 + MaxIdleConnsPerHost: 30, // 每个主机的最大空闲连接数 + IdleConnTimeout: 30 * time.Second, // 空闲连接超时时间 + }, + } + return &KxMixRepoImpl{ + bc: bc, + options: request.NewOptions(request.WithHeaders(h), request.WithHttpClient(hc)), + } } -func (s *KxMixRepoImpl) Request(ctx context.Context, req *kog.Notice) error { +func (this *KxMixRepoImpl) Request(ctx context.Context, req *kog.Notice) error { body, err := req.Marshal() if err != nil { return err } - h := http.Header{ - "Content-Type": []string{"application/json"}, - } + url := this.bc.Cmb.KxNoticeUrl - url := s.bc.Cmb.KxNoticeUrl - - _, bodyBytes, err := request.Post(ctx, url, body, request.WithHeaders(h), request.WithTimeout(time.Second*20)) + _, bodyBytes, err := request.POST(ctx, url, body, this.options) if err != nil { log.Errorf("请求kx报错,url:%s,body:%s,err:%v", url, string(body), err) return nil diff --git a/internal/data/wechatrepoimpl/cpn.go b/internal/data/wechatrepoimpl/cpn.go index 96381c6..f60738e 100644 --- a/internal/data/wechatrepoimpl/cpn.go +++ b/internal/data/wechatrepoimpl/cpn.go @@ -9,7 +9,6 @@ import ( "github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons" "io" "net/http" - "time" err2 "voucher/api/err" "voucher/internal/biz/bo" "voucher/internal/biz/vo" @@ -244,7 +243,6 @@ func (c *CpnRepoImpl) RegisterNotifyTag(ctx context.Context, stockID string) err body, request.WithHeaders(h), request.WithStatusCodeFunc(isSuccess), - request.WithTimeout(time.Second*20), ) if err != nil { return err diff --git a/internal/pkg/request/request.go b/internal/pkg/request/request.go index 28da386..5c02531 100644 --- a/internal/pkg/request/request.go +++ b/internal/pkg/request/request.go @@ -14,10 +14,11 @@ type Options struct { StatusCodeFunc func(int) bool - Timeout time.Duration + HttpClient *http.Client } func NewOptions(options ...Option) *Options { + o := &Options{ Headers: http.Header{ "Content-Type": []string{"application/json"}, @@ -27,7 +28,14 @@ func NewOptions(options ...Option) *Options { return code == http.StatusOK }, - Timeout: 15 * time.Second, + HttpClient: &http.Client{ + Timeout: 10 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 100, // 最大空闲连接数 + MaxIdleConnsPerHost: 20, // 每个主机的最大空闲连接数 + IdleConnTimeout: 30 * time.Second, // 空闲连接超时时间 + }, + }, } for _, option := range options { @@ -39,9 +47,9 @@ func NewOptions(options ...Option) *Options { type Option func(*Options) -func WithTimeout(timeout time.Duration) Option { +func WithHttpClient(httpClient *http.Client) Option { return func(options *Options) { - options.Timeout = timeout + options.HttpClient = httpClient } } @@ -57,6 +65,18 @@ func WithStatusCodeFunc(statusCodeFunc func(int) bool) Option { } } +func POST(ctx context.Context, url string, body []byte, options *Options) (http.Header, []byte, error) { + return Request(ctx, http.MethodPost, url, body, options) +} + +func GET(ctx context.Context, url string, options *Options) (http.Header, []byte, error) { + return Request(ctx, http.MethodGet, url, nil, options) +} + +func PUT(ctx context.Context, url string, body []byte, options *Options) (http.Header, []byte, error) { + return Request(ctx, http.MethodPut, url, body, options) +} + func Post(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) { return Request(ctx, http.MethodPost, url, body, NewOptions(options...)) } @@ -70,6 +90,7 @@ func Put(ctx context.Context, url string, body []byte, options ...Option) (http. } func Request(_ context.Context, method, url string, body []byte, o *Options) (http.Header, []byte, error) { + req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) if err != nil { return nil, nil, fmt.Errorf("创建HTTP请求失败: %w", err) @@ -77,13 +98,10 @@ func Request(_ context.Context, method, url string, body []byte, o *Options) (ht req.Header = o.Headers - httpClient := &http.Client{ - Timeout: o.Timeout, - } - - resp, err := httpClient.Do(req) + // 使用共享客户端 + resp, err := o.HttpClient.Do(req) if err != nil { - return nil, nil, fmt.Errorf("发送请求失败,timeout:%v,err:%w", o.Timeout, err) + return nil, nil, fmt.Errorf("发送请求失败,err:%w", err) } defer resp.Body.Close() diff --git a/internal/pkg/request/request_test.go b/internal/pkg/request/request_test.go index 4f13710..ab75fa6 100644 --- a/internal/pkg/request/request_test.go +++ b/internal/pkg/request/request_test.go @@ -5,7 +5,6 @@ import ( "net/http" "net/url" "testing" - "time" ) func Test_Get(t *testing.T) { @@ -37,7 +36,7 @@ func Test_RequestHeaders(t *testing.T) { "Authorization": []string{"Bearer token"}, } - respHeader, respBody, err := Post(context.Background(), uri, body, WithTimeout(10*time.Second), WithHeaders(h)) + respHeader, respBody, err := Post(context.Background(), uri, body, WithHeaders(h)) if err != nil { t.Error(err) return