下面是直接可應(yīng)用的實(shí)例:文章來源地址http://www.zghlxwxcb.cn/news/detail-683227.html
//該接口超過3s 未處理完就報(bào)超市錯誤
clientDeadline := time.Now().Add(time.Duration(3000 * time.Millisecond))
ctx, cancel := context.WithDeadline(ctx, clientDeadline)
defer cancel()
res, err := xx.Getxxx(ctx, req)
if err != nil {
fmt.Println("=========== err:", res)
statu, ok := status.FromError(err)
if ok {
//判斷是否為調(diào)用超時
if statu.Code() == codes.DeadlineExceeded {
fmt.Println("migration.UpdateProp timeout!")
}
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-683227.html
到了這里,關(guān)于golang 通過context設(shè)置接口請求超時時間的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!