1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

fix(server): create new request for every post trial

Updates #1169
This commit is contained in:
wdvxdr 2021-11-17 21:45:51 +08:00
parent c478870870
commit 7b02f8b670
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6

View File

@ -236,7 +236,14 @@ func (c *HTTPClient) onBotPushEvent(e *coolq.Event) {
}
client := http.Client{Timeout: time.Second * time.Duration(c.timeout)}
req, _ := http.NewRequest("POST", c.addr, bytes.NewReader(e.JSONBytes()))
// see https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0
// we should create a new request for every single post trial
createRequest := func() (*http.Request, error) {
req, err := http.NewRequest("POST", c.addr, bytes.NewReader(e.JSONBytes()))
if err != nil {
return nil, err
}
req.Header.Set("X-Self-ID", strconv.FormatInt(c.bot.Client.Uin, 10))
req.Header.Set("User-Agent", "CQHttp/4.15.0")
req.Header.Set("Content-Type", "application/json")
@ -248,12 +255,20 @@ func (c *HTTPClient) onBotPushEvent(e *coolq.Event) {
if c.apiPort != 0 {
req.Header.Set("X-API-Port", strconv.FormatInt(int64(c.apiPort), 10))
}
return req, nil
}
var res *http.Response
var err error
const maxAttemptTimes = 5
for i := 0; i <= maxAttemptTimes; i++ {
req, err := createRequest()
if err != nil {
log.Warnf("上报 Event 数据到 %v 时创建请求失败: %v", c.addr, err)
return
}
res, err = client.Do(req)
if err == nil {
//goland:noinspection GoDeferInLoop