From 160703f827cf200900a6a8abe6dfc368d932559f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:46:21 +0900 Subject: [PATCH] add timeout --- bot.go | 7 +++++++ openapi.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bot.go b/bot.go index b647b1d..4a27222 100644 --- a/bot.go +++ b/bot.go @@ -40,6 +40,7 @@ type Bot struct { mu sync.Mutex // 写锁 conn *websocket.Conn // conn 目前的 wss 连接 hbonce sync.Once // hbonce 保证仅执行一次 heartbeat + client *http.Client // client 主要配置 timeout ready EventReady // ready 连接成功后下发的 bot 基本信息 } @@ -117,6 +118,12 @@ func Run(bots ...*Bot) error { func (b *Bot) Init(gateway string, shard [2]byte) *Bot { b.gateway = gateway b.shard = shard + if b.Timeout == 0 { + b.Timeout = time.Minute + } + b.client = &http.Client{ + Timeout: b.Timeout, + } if b.Handler != nil { h := reflect.ValueOf(b.Handler).Elem() t := h.Type() diff --git a/openapi.go b/openapi.go index 968cd2d..e56857b 100644 --- a/openapi.go +++ b/openapi.go @@ -41,7 +41,7 @@ func (bot *Bot) dohttprequest(constructer HTTPRequsetConstructer, ep, contenttyp if err != nil { return errors.Wrap(err, getCallerFuncName()) } - resp, err := http.DefaultClient.Do(req) + resp, err := bot.client.Do(req) if err != nil { return errors.Wrap(err, getCallerFuncName()) }