voucher/internal/data/mixrepoimpl/kx.go

62 lines
1.6 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 mixrepoimpl
import (
"context"
"github.com/go-kratos/kratos/v2/log"
"net/http"
"time"
"voucher/internal/biz/kog"
"voucher/internal/biz/mixrepos"
"voucher/internal/conf"
"voucher/internal/pkg/request"
)
// KxMixRepoImpl kog 空港
type KxMixRepoImpl struct {
bc *conf.Bootstrap
// 连接池复用(优化网络开销)
options *request.Options
}
func NewKxMixRepoImpl(bc *conf.Bootstrap) mixrepos.KxMixRepo {
h := http.Header{
"Content-Type": []string{"application/json"},
}
hc := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 350, // 最大空闲连接数
MaxIdleConnsPerHost: 150, // 每个主机的最大空闲连接数
IdleConnTimeout: 30 * time.Second, // 空闲连接超时时间
MaxConnsPerHost: 300, // 每个主机的最大并发连接数(默认 100可根据需要调整
TLSHandshakeTimeout: 3 * time.Second, // 等待TLS握手。零表示没有超时。 TLS握手超时
},
}
return &KxMixRepoImpl{
bc: bc,
options: request.NewOptions(request.WithHeaders(h), request.WithHttpClient(hc)),
}
}
func (this *KxMixRepoImpl) Request(ctx context.Context, req *kog.Notice) error {
url := this.bc.Cmb.KxNoticeUrl
if url == "" {
return nil
}
body, err := req.Marshal()
if err != nil {
return err
}
start := time.Now()
_, bodyBytes, err := request.POST(ctx, url, body, this.options)
if err != nil {
log.Errorf("请求kx发生错误,耗时:%s,url:%s,reqBody:%s,respBody:%s,err:%v", time.Now().Sub(start).String(), url, string(body), string(bodyBytes), err)
return err
}
return nil
}