Skip to content

Commit

Permalink
Add pgx5 check (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
remast authored Feb 13, 2023
1 parent c509188 commit 951640b
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ tasks:
sh: docker-compose port pg-pq 5432
PG_PGX4_HOST:
sh: docker-compose port pg-pgx4 5432
PG_PGX5_HOST:
sh: docker-compose port pg-pgx5 5432
RABBIT_HOST_AMQP:
sh: docker-compose port rabbit 5672
RABBIT_HOST_HTTP:
Expand All @@ -51,6 +53,7 @@ tasks:
env:
HEALTH_GO_PG_PQ_DSN: 'postgres://test:test@{{.PG_PQ_HOST}}/test?sslmode=disable'
HEALTH_GO_PG_PGX4_DSN: 'postgres://test:test@{{.PG_PGX4_HOST}}/test?sslmode=disable'
HEALTH_GO_PG_PGX5_DSN: 'postgres://test:test@{{.PG_PGX5_HOST}}/test?sslmode=disable'
HEALTH_GO_MQ_DSN: 'amqp://guest:guest@{{.RABBIT_HOST_AMQP}}/'
HEALTH_GO_MQ_URL: 'http://guest:guest@{{.RABBIT_HOST_HTTP}}/'
HEALTH_GO_RD_DSN: 'redis://{{.REDIS_HOST}}/'
Expand Down
52 changes: 52 additions & 0 deletions checks/pgx5/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package pgx5

import (
"context"
"fmt"

"github.com/jackc/pgx/v5"
)

// Config is the PostgreSQL checker configuration settings container.
type Config struct {
// DSN is the PostgreSQL instance connection DSN. Required.
DSN string
}

// New creates new PostgreSQL health check that verifies the following:
// - connection establishing
// - doing the ping command
// - selecting postgres version
func New(config Config) func(ctx context.Context) error {
return func(ctx context.Context) (checkErr error) {
conn, err := pgx.Connect(ctx, config.DSN)
if err != nil {
checkErr = fmt.Errorf("PostgreSQL health check failed on connect: %w", err)
return
}

defer func() {
// override checkErr only if there were no other errors
if err := conn.Close(ctx); err != nil && checkErr == nil {
checkErr = fmt.Errorf("PostgreSQL health check failed on connection closing: %w", err)
}
}()

err = conn.Ping(ctx)
if err != nil {
checkErr = fmt.Errorf("PostgreSQL health check failed on ping: %w", err)
return
}

rows, err := conn.Query(ctx, `SELECT VERSION()`)
if err != nil {
checkErr = fmt.Errorf("PostgreSQL health check failed on select: %w", err)
return
}
defer func() {
rows.Close()
}()

return
}
}
100 changes: 100 additions & 0 deletions checks/pgx5/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package pgx5

import (
"context"
"os"
"sync"
"testing"
"time"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const pgDSNEnv = "HEALTH_GO_PG_PGX5_DSN"

func TestNew(t *testing.T) {
initDB(t)

check := New(Config{
DSN: getDSN(t),
})

err := check(context.Background())
require.NoError(t, err)
}

func TestEnsureConnectionIsClosed(t *testing.T) {
initDB(t)

pgDSN := getDSN(t)
ctx := context.Background()

conn, err := pgx.Connect(ctx, getDSN(t))
require.NoError(t, err)

defer func() {
err := conn.Close(ctx)
assert.NoError(t, err)
}()

var initialConnections int
row := conn.QueryRow(ctx, `SELECT sum(numbackends) FROM pg_stat_database`)
err = row.Scan(&initialConnections)
require.NoError(t, err)

check := New(Config{
DSN: pgDSN,
})

for i := 0; i < 10; i++ {
err := check(ctx)
assert.NoError(t, err)
time.Sleep(100 * time.Millisecond)
}

var currentConnections int
row = conn.QueryRow(ctx, `SELECT sum(numbackends) FROM pg_stat_database`)
err = row.Scan(&currentConnections)
require.NoError(t, err)

assert.Equal(t, initialConnections, currentConnections)
}

func getDSN(t *testing.T) string {
t.Helper()

pgDSN, ok := os.LookupEnv(pgDSNEnv)
require.True(t, ok)

return pgDSN
}

var dbInit sync.Once

func initDB(t *testing.T) {
t.Helper()

dbInit.Do(func() {
ctx := context.Background()

conn, err := pgx.Connect(ctx, getDSN(t))
require.NoError(t, err)

defer func() {
err := conn.Close(ctx)
assert.NoError(t, err)
}()

_, err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS test_pgx4 (
id TEXT NOT NULL PRIMARY KEY,
secret TEXT NOT NULL,
extra TEXT NOT NULL,
redirect_uri TEXT NOT NULL
);
`)
require.NoError(t, err)
})
}
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ services:
timeout: 5s
retries: 5

pg-pgx5:
image: postgres:10
ports:
- "5432"
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test
tmpfs:
- /var/lib/postgresql/data
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5

rabbit:
image: rabbitmq:3.6-management-alpine
ports:
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v5 v5.2.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -43,11 +44,11 @@ require (
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQ
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
github.com/jackc/pgx/v4 v4.16.1 h1:JzTglcal01DrghUqt+PmzWsZx/Yh7SC/CTQmSBMTd0Y=
github.com/jackc/pgx/v4 v4.16.1/go.mod h1:SIhx0D5hoADaiXZVyv+3gSm3LCIIINTVO0PficsvWGQ=
github.com/jackc/pgx/v5 v5.2.0 h1:NdPpngX0Y6z6XDFKqmFQaE+bCtkqzvQIOt1wvBlAqs8=
github.com/jackc/pgx/v5 v5.2.0/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
Expand Down Expand Up @@ -287,6 +289,8 @@ golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand Down Expand Up @@ -315,6 +319,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 h1:ZrnxWX62AgTKOSagEqxvb3ffipvEDX2pl7E1TdqLqIc=
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -349,6 +355,8 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down

0 comments on commit 951640b

Please sign in to comment.