/
dbmgr.go
84 lines (69 loc) · 1.73 KB
/
dbmgr.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
// Package model
// Created by RTT.
// Author: teocci@yandex.com on 2021-Aug-30
package model
import (
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"hash/fnv"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
gopg "github.com/go-pg/pg/v10"
"github.com/jackc/pgx/v4"
)
const jinanBenchConfig = "user=jinan password=jinan#db host=localhost port=5432 dbname=jinan_db sslmode=disable"
var (
setupOnce sync.Once
pg *gopg.DB
)
func HashE(s string) (uint32, error) {
h := fnv.New32a()
_, err := h.Write([]byte(s))
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
func Setup() *gopg.DB {
setupOnce.Do(func() {
config, err := pgxpool.ParseConfig(jinanBenchConfig)
if err != nil {
log.Fatalf("extractConfig failed: %v", err)
}
pg, err = openPg(*config.ConnConfig)
if err != nil {
log.Fatalf("openPq failed: %v", err)
}
})
return pg
}
func openPg(config pgx.ConnConfig) (*gopg.DB, error) {
var options gopg.Options
options.Addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
_, err := os.Stat(config.Host)
if err == nil {
options.Network = "unix"
if !strings.Contains(config.Host, "/.s.PGSQL.") {
options.Addr = filepath.Join(config.Host, ".s.PGSQL.5432")
}
}
options.User = config.User
options.Database = config.Database
options.Password = config.Password
options.TLSConfig = config.TLSConfig
options.MaxRetries = 1
options.MinRetryBackoff = -1
options.DialTimeout = 30 * time.Second
options.ReadTimeout = 10 * time.Second
options.WriteTimeout = 10 * time.Second
options.PoolSize = 10
options.MaxConnAge = 10 * time.Second
options.PoolTimeout = 30 * time.Second
options.IdleTimeout = time.Second
options.IdleCheckFrequency = 100 * time.Millisecond
return gopg.Connect(&options), nil
}