1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 10:40:24 +08:00

add timeout

This commit is contained in:
源文雨
2023-10-16 12:46:21 +09:00
parent 415bd574f1
commit 160703f827
2 changed files with 8 additions and 1 deletions

7
bot.go
View File

@@ -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()

View File

@@ -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())
}