62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
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: 5 * 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
|
||
}
|
||
//fmt.Printf("请求kx,url:%s,reqBody:%s", url, string(body))
|
||
|
||
_, bodyBytes, err := request.POST(ctx, url, body, this.options)
|
||
if err != nil {
|
||
log.Errorf("请求kx,url:%s,reqBody:%s,respBody:%s,err:%v", url, string(body), string(bodyBytes), err)
|
||
return nil
|
||
}
|
||
|
||
return nil
|
||
}
|