voucher/internal/data/mixrepoimpl/kx.go

58 lines
1.4 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: 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 (this *KxMixRepoImpl) Request(ctx context.Context, req *kog.Notice) error {
body, err := req.Marshal()
if err != nil {
return err
}
url := this.bc.Cmb.KxNoticeUrl
_, 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
}
log.Warnf("请求kx,url:%s,reqBody:%s,respBody:%s", url, string(body), string(bodyBytes))
return nil
}