只有在lib/pq中观察时才会出现错误



我试着用gorm&pgsql,当gorm尝试连接到pgsql时,它因恐慌而退出,代码为:

d, err := gorm.Open("postgres", param)

当我跟踪堆栈时,我发现这是lib/pq进程的问题——服务器版本:

case "server_version":
var major1 int
var major2 int
var minor int
//r.string is the string of pgsql version.
//mine is: 12.4 (Debian 12.4-1.pgdg100+1)
_, err = fmt.Sscanf(r.string(), "%d.%d.%d", &major1, &major2, &minor)
if err == nil {
cn.parameterStatus.serverVersion = major1*10000 + major2*100 + minor
}

不幸的是,字符串12.4 (Debian 12.4-1.pgdg100+1)与格式%d.%d.%d不匹配,因此它错误地退出。

但奇怪的是,它只有在GoLand中调试时才会退出(不知道vscode(。当简单地构建&运行时,输出为:

[INFO] 2022-11-04 15:30:52 +0800 [start_postgres.go:103] detecting database connecting... pgdir=%v/yak/tmp_build/database
[INFO] 2022-11-04 15:30:52 +0800 [start_postgres.go:106] detected exsited database.
[INFO] 2022-11-04 15:30:52 +0800 [core.go:107] health info manager is loading
[INFO] 2022-11-04 15:30:52 +0800 [manager.go:70] health info: cache 60 infos
[INFO] 2022-11-04 15:30:52 +0800 [core.go:112] start to connection postgres
[INFO] 2022-11-04 15:30:52 +0800 [core.go:117] build basic database manager instance

这是非常正常的。然后我对lib/pq/conn.go 中的源代码进行了一些修改

rString := r.string()
fmt.Printf("n%snn", rString)
_, err = fmt.Sscanf(r.string(), "%d.%d.%d", &major1, &major2, &minor)
if err == nil {
cn.parameterStatus.serverVersion = major1*10000 + major2*100 + minor
}

最奇怪的是,服务器崩溃了,错误记录循环无限:

[INFO] 2022-11-04 16:37:00 +0800 [start_postgres.go:103] detecting database connecting... pgdir=%v/yak/tmp_build/database
12.4 (Debian 12.4-1.pgdg100+1)
[WARN] 2022-11-04 16:37:00 +0800 [start_postgres.go:110] open database failed: pq: invalid message format; expected string terminator
[INFO] 2022-11-04 16:37:00 +0800 [start_postgres.go:113] try to start a database...
12.4 (Debian 12.4-1.pgdg100+1)
[WARN] 2022-11-04 16:37:02 +0800 [start_postgres.go:206] try pq: invalid message format; expected string terminator times... waiting for the postgres starting up...
12.4 (Debian 12.4-1.pgdg100+1)
[WARN] 2022-11-04 16:37:03 +0800 [start_postgres.go:206] try pq: invalid message format; expected string terminator times... waiting for the postgres starting up...
12.4 (Debian 12.4-1.pgdg100+1)
[WARN] 2022-11-04 16:37:04 +0800 [start_postgres.go:206] try pq: invalid message format; expected string terminator times... waiting for the postgres starting up...
...

我不明白为什么会发生这种情况,我不认为这是一场闹剧,因为主程序被阻止了,所以有人能提供一些帮助吗?

依赖项和IDE:的版本

GoLand v2022.2.4

go version go1.19.2 linux/amd64

gorm v1.9.2

github.com/lib/pq v1.1.0

postgresql v12.4 (Debian 12.4-1.pgdg100+1)

我发现了错误的无限循环,init连接后有一个select结构,代码是:

select {
case <-ticker:
count++
conn, err := gorm.Open("postgres", param)
//conn, err := net.Dial("tcp", "127.0.0.1:5432")
if err != nil {
log.Warningf("try %v times... waiting for the postgres starting up...", err)
continue
}
_ = conn.Close()
return nil
}

所以问题是,当我打印r.string()时,它会返回一个错误。

此外,它在调试(GoLand(时会出现死机,但在构建和运行时可以正常工作。

相关内容

  • 没有找到相关文章

最新更新