pgxpool.Connect返回的pool是nil或者很快就变成nil没有报错
The pool returned by pgxpool.Connect is nil or becomes nil quickly without error
我有以下用于连接到 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
时休眠,第一个 return 之前的日志也会打印出来:pool returned from connect: %!s(*pgxpool.Pool=<nil>)
pgxpool
中的后台进程是否可以通过某种方式使 p == nil
?
想知道为什么会发生这种情况吗?
编辑:这似乎只发生在 运行 我的应用程序和 Postgres 通过 docker-compose 时。我正在使用以下撰写文件:
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" ]
以及一个最小可复制的示例转到文件:
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
我有以下用于连接到 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
时休眠,第一个 return 之前的日志也会打印出来:pool returned from connect: %!s(*pgxpool.Pool=<nil>)
pgxpool
中的后台进程是否可以通过某种方式使 p == nil
?
想知道为什么会发生这种情况吗?
编辑:这似乎只发生在 运行 我的应用程序和 Postgres 通过 docker-compose 时。我正在使用以下撰写文件:
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" ]
以及一个最小可复制的示例转到文件:
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