This commit is contained in:
parent
ddab078e1f
commit
ee85d3debe
|
|
@ -268,7 +268,7 @@ func (s *CmbMixRepoImpl) Request(ctx context.Context, req *v1.CmbRequest, uri st
|
||||||
|
|
||||||
r := uri + "?" + uv.Encode()
|
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 {
|
if err != nil {
|
||||||
log.Errorf("请求掌上生活报错,url:%s,err:%v", r, err)
|
log.Errorf("请求掌上生活报错,url:%s,err:%v", r, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -14,26 +14,38 @@ import (
|
||||||
// KxMixRepoImpl kog 空港
|
// KxMixRepoImpl kog 空港
|
||||||
type KxMixRepoImpl struct {
|
type KxMixRepoImpl struct {
|
||||||
bc *conf.Bootstrap
|
bc *conf.Bootstrap
|
||||||
|
// 连接池复用(优化网络开销)
|
||||||
|
options *request.Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewKxMixRepoImpl(bc *conf.Bootstrap) mixrepos.KxMixRepo {
|
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()
|
body, err := req.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
h := http.Header{
|
url := this.bc.Cmb.KxNoticeUrl
|
||||||
"Content-Type": []string{"application/json"},
|
|
||||||
}
|
|
||||||
|
|
||||||
url := s.bc.Cmb.KxNoticeUrl
|
_, bodyBytes, err := request.POST(ctx, url, body, this.options)
|
||||||
|
|
||||||
_, bodyBytes, err := request.Post(ctx, url, body, request.WithHeaders(h), request.WithTimeout(time.Second*20))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("请求kx报错,url:%s,body:%s,err:%v", url, string(body), err)
|
log.Errorf("请求kx报错,url:%s,body:%s,err:%v", url, string(body), err)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
err2 "voucher/api/err"
|
err2 "voucher/api/err"
|
||||||
"voucher/internal/biz/bo"
|
"voucher/internal/biz/bo"
|
||||||
"voucher/internal/biz/vo"
|
"voucher/internal/biz/vo"
|
||||||
|
|
@ -244,7 +243,6 @@ func (c *CpnRepoImpl) RegisterNotifyTag(ctx context.Context, stockID string) err
|
||||||
body,
|
body,
|
||||||
request.WithHeaders(h),
|
request.WithHeaders(h),
|
||||||
request.WithStatusCodeFunc(isSuccess),
|
request.WithStatusCodeFunc(isSuccess),
|
||||||
request.WithTimeout(time.Second*20),
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,11 @@ type Options struct {
|
||||||
|
|
||||||
StatusCodeFunc func(int) bool
|
StatusCodeFunc func(int) bool
|
||||||
|
|
||||||
Timeout time.Duration
|
HttpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOptions(options ...Option) *Options {
|
func NewOptions(options ...Option) *Options {
|
||||||
|
|
||||||
o := &Options{
|
o := &Options{
|
||||||
Headers: http.Header{
|
Headers: http.Header{
|
||||||
"Content-Type": []string{"application/json"},
|
"Content-Type": []string{"application/json"},
|
||||||
|
|
@ -27,7 +28,14 @@ func NewOptions(options ...Option) *Options {
|
||||||
return code == http.StatusOK
|
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 {
|
for _, option := range options {
|
||||||
|
|
@ -39,9 +47,9 @@ func NewOptions(options ...Option) *Options {
|
||||||
|
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
func WithTimeout(timeout time.Duration) Option {
|
func WithHttpClient(httpClient *http.Client) Option {
|
||||||
return func(options *Options) {
|
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) {
|
func Post(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) {
|
||||||
return Request(ctx, http.MethodPost, url, body, NewOptions(options...))
|
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) {
|
func Request(_ context.Context, method, url string, body []byte, o *Options) (http.Header, []byte, error) {
|
||||||
|
|
||||||
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("创建HTTP请求失败: %w", err)
|
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
|
req.Header = o.Headers
|
||||||
|
|
||||||
httpClient := &http.Client{
|
// 使用共享客户端
|
||||||
Timeout: o.Timeout,
|
resp, err := o.HttpClient.Do(req)
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := httpClient.Do(req)
|
|
||||||
if err != nil {
|
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()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Get(t *testing.T) {
|
func Test_Get(t *testing.T) {
|
||||||
|
|
@ -37,7 +36,7 @@ func Test_RequestHeaders(t *testing.T) {
|
||||||
"Authorization": []string{"Bearer token"},
|
"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 {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue