-
Notifications
You must be signed in to change notification settings - Fork 16
/
pgxpool_psc_test.go
87 lines (78 loc) · 2.89 KB
/
pgxpool_psc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package alloydbconn_test
import (
"context"
"fmt"
"net"
"cloud.google.com/go/alloydbconn"
"github.com/jackc/pgx/v5/pgxpool"
)
// connectPgxWithPSC establishes a connection to your database using pgxpool
// and the AlloyDB Go Connector (aka alloydbconn.Dialer)
//
// The function takes an instance URI, a username, a password, and a database
// name. Usage looks like this:
//
// pool, cleanup, err := connectPgxWithPSC(
// context.Background(),
// "projects/myproject/locations/us-central1/clusters/mycluster/instances/myinstance",
// "postgres",
// "secretpassword",
// "mydb",
// )
//
// In addition to a *pgxpool.Pool type, the function returns a cleanup function
// that should be called when you're done with the database connection.
func connectPgxWithPSC(
ctx context.Context, instURI, user, pass, dbname string,
) (*pgxpool.Pool, func() error, error) {
// First initialize the dialer. alloydbconn.NewDialer accepts additional
// options to configure credentials, timeouts, etc.
//
// For details, see:
// https://pkg.go.dev/cloud.google.com/go/alloydbconn#Option
d, err := alloydbconn.NewDialer(ctx)
if err != nil {
noop := func() error { return nil }
return nil, noop, fmt.Errorf("failed to init Dialer: %v", err)
}
// The cleanup function will stop the dialer's background refresh
// goroutines. Call it when you're done with your database connection to
// avoid a goroutine leak.
cleanup := func() error { return d.Close() }
dsn := fmt.Sprintf(
// sslmode is disabled, because the Dialer will handle the SSL
// connection instead.
"user=%s password=%s dbname=%s sslmode=disable",
user, pass, dbname,
)
// Prefer pgxpool for applications.
// For more information, see:
// https://github.com/jackc/pgx/wiki/Getting-started-with-pgx#using-a-connection-pool
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, cleanup, fmt.Errorf("failed to parse pgx config: %v", err)
}
// Tell pgx to use alloydbconn.Dialer to connect to the instance.
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return d.Dial(ctx, instURI, alloydbconn.WithPSC())
}
// Establish the connection.
pool, connErr := pgxpool.NewWithConfig(ctx, config)
if connErr != nil {
return nil, cleanup, fmt.Errorf("failed to connect: %s", connErr)
}
return pool, cleanup, nil
}