当前位置: > > > > pgxpool.Connect 返回的池为 nil 或快速变为 nil 而不会出现错误
pgxpool.Connect 返回的池为 nil 或快速变为 nil 而不会出现错误
来源:stackoverflow
2024-04-18 23:51:34
0浏览
收藏
小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《pgxpool.Connect 返回的池为 nil 或快速变为 nil 而不会出现错误》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!
问题内容
我有以下用于连接到 postgres 数据库的代码:
func connecttopostgres(ctx context.context, url string) (*pgxpool.pool, error) {
var err error
for i := 0; i < 5; i++ {
p, err := pgxpool.connect(ctx, url)
if err != nil || p == nil {
time.sleep(3 * time.second)
continue
}
log.printf("pool returned from connect: %s", p)
return p, nil
}
return nil, errors.wrap(err, "timed out waiting to connect postgres")
}
用例是在使用 docker-compose 启动我的服务器时等待 postgres 变得可用。即使如果 p == nil 代码会休眠,第一次返回之前的日志也会打印出来: pool returned from connect: %!s(*pgxpool.pool=<nil>)
pgxpool 中的后台进程是否可以通过某种方式使 p == nil ?
对于为什么会发生这种情况有什么想法吗?
编辑:这似乎只在通过 docker-compose 运行我的应用程序和 postgres 时发生。我正在使用以下撰写文件:
services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- "db"
db:
image: postgres
restart: always
environment:
- postgres_db=demo_db
- postgres_password=${postgres_password}
ports:
- "8081:5432"
以及我的应用程序的 dockerfile:
from golang:1.17 workdir / copy go.mod . copy go.sum . copy *.go . run go mod download run go build expose 8080 cmd [ "./app" ]
以及一个最低限度可重复的示例 go 文件:
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
)
func main() {
log.Printf("connecting to postgres...")
pgpool, err := connectToPostgres(context.Background(), "postgresql://localhost:5432/demo_db")
log.Printf("pool: %s", pgpool)
if err != nil {
log.Fatalln(err)
}
log.Printf("successfully connected to postgres")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
log.Println("stopped")
}
func connectToPostgres(ctx context.Context, url string) (*pgxpool.Pool, error) {
var err error
for i := 0; i < 5; i++ {
p, err := pgxpool.Connect(ctx, url)
if err != nil || p == nil {
time.Sleep(3 * time.Second)
continue
}
log.Printf("pool returned from connect: %s", p)
return p, nil
}
return nil, errors.Wrap(err, "timed out waiting to connect postgres")
}
正确答案
问题是,在 docker-compose 网络中连接时,您必须连接到容器的主机名,在本例中为 db。
您还可以使用其他容器的 ip 但需要额外的工作量,仅使用主机名会更简单。
换句话说,你的连接字符串错误,我在连接到 localhost 时也得到了这个
app_1 | 2021/12/21 18:53:28 pool: %!s(*pgxpool.pool=<nil>) app_1 | 2021/12/21 18:53:28 successfully connected to postgres
使用正确的连接字符串连接时:
"postgres://postgres:mysecretpassword@db:5432/postgres"
它工作得很好。
其余日志
db_1 | 2021-12-21 18:56:04.122 UTC [1] LOG: database system is ready to accept connections
app_1 | 2021/12/21 18:56:06 pool returned from connect: &{%!s(*puddle.Pool=&{0xc00007c040 0xc0000280b0 [0xc00007c0c0] [0xc00007c0c0] 0x65cb60 0x65dc80 16 1 9872796 1 0 false}) %!s(*pgxpool.Config=&{0xc0000a2000 <nil> <nil> <nil> <nil> 3600000000000 1800000000000 16 0 60000000000 false true}) %!s(func(context.Context, *pgx.ConnConfig) error=<nil>) %!s(func(context.Context, *pgx.Conn) error=<nil>) %!s(func(context.Context, *pgx.Conn) bool=<nil>) %!s(func(*pgx.Conn) bool=<nil>) %!s(int32=0) %!s(time.Duration=3600000000000) %!s(time.Duration=1800000000000) %!s(time.Duration=60000000000) {%!s(uint32=0) {%!s(int32=0) %!s(uint32=0)}} %!s(chan struct {}=0xc000024060)}
app_1 | 2021/12/21 18:56:06 pool: &{%!s(*puddle.Pool=&{0xc00007c040 0xc0000280b0 [0xc00007c0c0] [0xc00007c0c0] 0x65cb60 0x65dc80 16 1 9872796 1 0 false}) %!s(*pgxpool.Config=&{0xc0000a2000 <nil> <nil> <nil> <nil> 3600000000000 1800000000000 16 0 60000000000 false true}) %!s(func(context.Context, *pgx.ConnConfig) error=<nil>) %!s(func(context.Context, *pgx.Conn) error=<nil>) %!s(func(context.Context, *pgx.Conn) bool=<nil>) %!s(func(*pgx.Conn) bool=<nil>) %!s(int32=0) %!s(time.Duration=3600000000000) %!s(time.Duration=1800000000000) %!s(time.Duration=60000000000) {%!s(uint32=0) {%!s(int32=0) %!s(uint32=0)}} %!s(chan struct {}=0xc000024060)}
app_1 | 2021/12/21 18:56:06 successfully connected to postgres
终于介绍完啦!小伙伴们,这篇关于《pgxpool.Connect 返回的池为 nil 或快速变为 nil 而不会出现错误》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~米云公众号也会发布Golang相关知识,快来关注吧!
