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: 300, // 最大空闲连接数 MaxIdleConnsPerHost: 150, // 每个主机的最大空闲连接数 IdleConnTimeout: 30 * time.Second, // 空闲连接超时时间 MaxConnsPerHost: 200, // 每个主机的最大并发连接数(默认 100,可根据需要调整) }, } 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 }