-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
121 lines (109 loc) · 2.77 KB
/
client.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package pgxsql
// DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}
// https://pkg.go.dev/github.com/jackc/pgx/v5/pgtype
import (
"context"
"errors"
"fmt"
"github.com/advanced-go/core/messaging"
"github.com/jackc/pgx/v5/pgxpool"
"net/http"
"time"
)
var (
dbClient *pgxpool.Pool
)
const (
clientLoc = PkgPath + ":Startup"
)
var clientStartup messaging.MessageHandler = func(msg *messaging.Message) {
if isReady() {
return
}
start := time.Now()
err := clientStartup2(msg.Config())
if err != nil {
messaging.SendReply(msg, messaging.NewStatusDurationError(http.StatusOK, time.Since(start), err))
return
}
messaging.SendReply(msg, messaging.NewStatusDuration(http.StatusOK, time.Since(start)))
}
// clientStartup - entry point for creating the pooling client and verifying a connection can be acquired
func clientStartup2(cfg map[string]string) error {
if isReady() {
return nil
}
if cfg == nil {
return errors.New("error: strings map configuration is nil")
}
url, ok := cfg[uriConfigKey]
if !ok {
return errors.New("database URL is empty")
}
// Create connection string with credentials
s, err := connectString(url, cfg)
if err != nil {
return err
}
// Create pooled client and acquire connection
dbClient, err = pgxpool.New(context.Background(), s)
if err != nil {
return errors.New(fmt.Sprintf("unable to create connection pool: %v\n", err))
}
conn, err1 := dbClient.Acquire(context.Background())
if err1 != nil {
clientShutdown()
return errors.New(fmt.Sprintf("unable to acquire connection from pool: %v\n", err1))
}
conn.Release()
setReady()
return nil
}
func clientShutdown() {
if dbClient != nil {
resetReady()
dbClient.Close()
dbClient = nil
}
}
func connectString(url string, cfg map[string]string) (string, error) {
user, ok := cfg[userConfigKey]
pswd, ok0 := cfg[pswdConfigKey]
// Username and password can be in the connect string Url
if !ok && !ok0 {
return url, nil
}
if !ok {
return "", errors.New("error: user is not configured")
}
if !ok0 {
return "", errors.New("error: password is not configured")
}
return fmt.Sprintf(url, user, pswd), nil
}
/*
// accessCredentials - access function for Credentials in a message
func accessCredentials(msg *messaging.Message) startupCredentials {
if msg == nil || msg.Content == nil {
return nil
}
for _, c := range msg.Content {
if fn, ok := c.(func() (user string, pswd string, err error)); ok {
return fn
}
}
return nil
}
// accessResource - access function for a resource in a message
func accessResource(msg *messaging.Message) startupResource {
if msg == nil || msg.Content == nil {
return startupResource{}
}
for _, c := range msg.Content {
if url, ok := c.(struct{ Uri string }); ok {
return url
}
}
return startupResource{}
}
*/