From a075f41a51a743e560c497ea6f7c80c494636fcd Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Tue, 11 Aug 2020 15:19:54 +0800 Subject: [PATCH] http post timeout setting supported. close #77 --- global/config.go | 1 + main.go | 2 +- server/http.go | 15 ++++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/global/config.go b/global/config.go index ba94521..2c6ca75 100644 --- a/global/config.go +++ b/global/config.go @@ -43,6 +43,7 @@ type GoCQHttpConfig struct { Enabled bool `json:"enabled"` Host string `json:"host"` Port uint16 `json:"port"` + Timeout int32 `json:"timeout"` PostUrls map[string]string `json:"post_urls"` PostMessageFormat string `json:"post_message_format"` } diff --git a/main.go b/main.go index 1085e51..322414a 100644 --- a/main.go +++ b/main.go @@ -201,7 +201,7 @@ func main() { coolq.SetMessageFormat(conf.HttpConfig.PostMessageFormat) } for k, v := range conf.HttpConfig.PostUrls { - server.NewHttpClient().Run(k, v, b) + server.NewHttpClient().Run(k, v, conf.HttpConfig.Timeout, b) } } if conf.WSConfig != nil && conf.WSConfig.Enabled { diff --git a/server/http.go b/server/http.go index 15b9e8f..10504e2 100644 --- a/server/http.go +++ b/server/http.go @@ -21,9 +21,10 @@ type httpServer struct { } type httpClient struct { - bot *coolq.CQBot - secret string - addr string + bot *coolq.CQBot + secret string + addr string + timeout int32 } var HttpServer = &httpServer{} @@ -163,10 +164,14 @@ func NewHttpClient() *httpClient { return &httpClient{} } -func (c *httpClient) Run(addr, secret string, bot *coolq.CQBot) { +func (c *httpClient) Run(addr, secret string, timeout int32, bot *coolq.CQBot) { c.bot = bot c.secret = secret c.addr = addr + c.timeout = timeout + if c.timeout < 5 { + c.timeout = 5 + } bot.OnEventPush(c.onBotPushEvent) log.Infof("HTTP POST上报器已启动: %v", addr) } @@ -184,7 +189,7 @@ func (c *httpClient) onBotPushEvent(m coolq.MSG) { h["X-Signature"] = "sha1=" + hex.EncodeToString(mac.Sum(nil)) } return h - }()).SetTimeout(time.Second * 5).Do() + }()).SetTimeout(time.Second * time.Duration(c.timeout)).Do() if err != nil { log.Warnf("上报Event数据到 %v 失败: %v", c.addr, err) return