亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓 golang mysql 驅動程序在 2 秒內超時 ping?

如何讓 golang mysql 驅動程序在 2 秒內超時 ping?

Go
桃花長相依 2023-03-29 16:05:11
我在弄清楚如何在 Go 中正確設置數據庫連接嘗試超時時遇到了一些麻煩。我正在使用這個優秀資源中的一些示例作為基礎。我相信我已正確設置所有內容,但我的 ping 只是在 2 秒后拒絕超時。我已經將有問題的代碼提取到示例程序中,如下所示。請注意,在 172.1.2.3 上沒有運行數據庫。package mainimport (    "context"    "database/sql"    _ "github.com/go-sql-driver/mysql" //MySQL driver    "log"    "time")func main() {    log.Print("Trying to ping database!")    //Prepare a "context" to execute queries in, this will allow us to use timeouts    var bgCtx = context.Background()    var ctx2SecondTimeout, cancelFunc2SecondTimeout = context.WithTimeout(bgCtx, time.Second*2)    defer cancelFunc2SecondTimeout()    //Open  database connection    db, err := sql.Open("mysql", "root:@tcp(172.1.2.3)/testdb?parseTime=true")    if err != nil {        log.Printf("Unable to open db, %s", err.Error())        return    }    log.Print("Successfully called open()")    //Ping database connection with 2 second timeout    err = db.PingContext(ctx2SecondTimeout)    if err != nil {        log.Printf("Can't ping database server in order to use storage, %s", err.Error())        return    }    log.Print("Successfully pinged database!")}運行該程序最多需要 2 秒,但實際需要 2 分鐘以上:$ go run lambdatry.go2018/09/03 16:33:33 Trying to ping database!2018/09/03 16:33:33 Successfully called open()2018/09/03 16:35:43 Can't ping database server in order to use storage, dial tcp 172.1.2.3:3306: connect: connection timed out如果我更改“數據庫”的 IP(我只是選擇了一個隨機 IP,因此該地址沒有數據庫)數據庫有時會立即超時,有時需要很長時間才能超時。我在 ubuntu 18.04 上運行 go 1.10.1。
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

看來這不是 的錯PingContext。tldr 跳到底部。


PingContext支持帶有超時的上下文,但在調試時,似乎原因的根源是調用sql.Open(),或者其他一些調用,然后sql.Conn()是對 after 的調用PingContext。前兩個獲取db.mu.lock,在它PingContext有機會使用您的超時上下文之前最終等待: https://github.com/golang/go/blob/master/src/database/sql/sql.go#L1394


conn.PingContext當使用instead of 時變得更加清晰db.PingContext,例如對于阻塞的端口,超時是這樣發生的:


db, err := sql.Open(driver, connStr)

if err != nil {

? ? // This error will not happen

? ? log.Fatalln(err)

}


// Create a 1 second timeout

ctxBG := context.Background()

ctxConnTimeout, cancel := context.WithTimeout(ctxBG, 1*time.Second)

defer cancel()


conn, err := db.Conn(ctxConnTimeout) // Hangs here for 60 seconds, never uses my timeout

if err != nil {

? ? ? ? // This error fires

? ? log.Fatalln(err)

}


// Never got here

ctxPingTimeout, cancel := context.WithTimeout(ctxBG, 1*time.Second)

defer cancel()


err = conn.PingContext(ctxPingTimeout)

if err != nil {

? ? log.Fatalln(err)

}

那么如何將超時傳遞給sql.Openorsql.Conn呢?似乎這是唯一的方法:


sql.Open("mysql", "user:password@/dbname?timeout=5s")


sql.Open("postgres", "user=user dbname=dbname connect_timeout=5")


sql.Open("sqlserver", "sqlserver://username:password@host/instance?dial+timeout=5")


查看完整回答
反對 回復 2023-03-29
?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

我使用以下 MySQL DSN 格式在 2 秒內超時,它工作得很好。

user:password@tcp(localhost:80)/dbname?timeout=2s


查看完整回答
反對 回復 2023-03-29
  • 1 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號