mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-24 22:53:52 +02:00
сделал postgres_pgxpool
This commit is contained in:
@@ -24,8 +24,8 @@ import (
|
|||||||
// PgxPool - пул соединений к базе данных
|
// PgxPool - пул соединений к базе данных
|
||||||
var PgxPool *pgxpool.Pool
|
var PgxPool *pgxpool.Pool
|
||||||
|
|
||||||
// mutex_Connect - защита от многопоточности Connect()
|
//// mutex_Connect - защита от многопоточности Connect()
|
||||||
var mutex_Connect = &sync.RWMutex{}
|
//var mutex_Connect = &sync.RWMutex{}
|
||||||
|
|
||||||
// mutex_ReConnect - защита от многопоточности ReConnect()
|
// mutex_ReConnect - защита от многопоточности ReConnect()
|
||||||
var mutex_ReConnect = &sync.RWMutex{}
|
var mutex_ReConnect = &sync.RWMutex{}
|
||||||
@@ -50,7 +50,7 @@ type SettingsINI struct {
|
|||||||
const TextConnBusy = "conn busy"
|
const TextConnBusy = "conn busy"
|
||||||
|
|
||||||
// timeOutSeconds - время ожидания для Ping()
|
// timeOutSeconds - время ожидания для Ping()
|
||||||
const timeOutSeconds = 60
|
const timeOutSeconds = 1
|
||||||
|
|
||||||
// Connect_err - подключается к базе данных
|
// Connect_err - подключается к базе данных
|
||||||
func Connect() {
|
func Connect() {
|
||||||
@@ -142,15 +142,15 @@ func IsClosed() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := contextmain.GetContext()
|
ctxMain := contextmain.GetContext()
|
||||||
conn, err := GetConnection_err()
|
ctx, cancelFunc := context.WithTimeout(ctxMain, timeOutSeconds*time.Second)
|
||||||
if err != nil {
|
defer cancelFunc()
|
||||||
return true
|
|
||||||
}
|
err := GetConnection().Ping(ctx)
|
||||||
err = conn.Ping(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return Otvet
|
return Otvet
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,11 +231,11 @@ func CloseConnection_err() error {
|
|||||||
//ctx, cancel := context.WithTimeout(ctxMain, 60*time.Second)
|
//ctx, cancel := context.WithTimeout(ctxMain, 60*time.Second)
|
||||||
//defer cancel()
|
//defer cancel()
|
||||||
|
|
||||||
mutex_Connect.Lock()
|
//mutex_Connect.Lock()
|
||||||
defer mutex_Connect.Unlock()
|
//defer mutex_Connect.Unlock()
|
||||||
|
|
||||||
PgxPool.Reset()
|
//PgxPool.Reset()
|
||||||
//PgxPool.Close()
|
PgxPool.Close()
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -377,7 +377,7 @@ loop:
|
|||||||
|
|
||||||
//ping в базе данных
|
//ping в базе данных
|
||||||
//mutex_Connect.RLock() //race
|
//mutex_Connect.RLock() //race
|
||||||
//err = GetConnection_err().Ping(ctx) //ping делать нельзя т.к. data race
|
//err = GetConnection().Ping(ctx) //ping делать нельзя т.к. data race
|
||||||
err = Ping_err(ctx)
|
err = Ping_err(ctx)
|
||||||
//mutex_Connect.RUnlock()
|
//mutex_Connect.RUnlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -414,8 +414,8 @@ loop:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConnection_err - возвращает соединение к нужной базе данных
|
// GetConnection - возвращает соединение к нужной базе данных
|
||||||
func GetConnection_err() (*pgxpool.Conn, error) {
|
func GetConnection() *pgxpool.Pool {
|
||||||
//мьютекс чтоб не подключаться одновременно
|
//мьютекс чтоб не подключаться одновременно
|
||||||
//mutex_Connect.RLock()
|
//mutex_Connect.RLock()
|
||||||
//defer mutex_Connect.RUnlock()
|
//defer mutex_Connect.RUnlock()
|
||||||
@@ -430,16 +430,16 @@ func GetConnection_err() (*pgxpool.Conn, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctxMain := contextmain.GetContext()
|
//ctxMain := contextmain.GetContext()
|
||||||
ctx, cancelFunc := context.WithTimeout(ctxMain, timeOutSeconds*time.Second)
|
//ctx, cancelFunc := context.WithTimeout(ctxMain, timeOutSeconds*time.Second)
|
||||||
defer cancelFunc()
|
//defer cancelFunc()
|
||||||
|
|
||||||
connection, err := PgxPool.Acquire(ctx)
|
//connection, err := PgxPool.Acquire(ctx)
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
err = fmt.Errorf("Ошибка при получении соединения из пула базы данных, error: %w", err)
|
// err = fmt.Errorf("Ошибка при получении соединения из пула базы данных, error: %w", err)
|
||||||
return connection, nil
|
// return connection
|
||||||
}
|
//}
|
||||||
return connection, nil
|
return PgxPool
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConnection_WithApplicationName - возвращает соединение к нужной базе данных, с указанием имени приложения
|
// GetConnection_WithApplicationName - возвращает соединение к нужной базе данных, с указанием имени приложения
|
||||||
@@ -458,7 +458,7 @@ func GetConnection_WithApplicationName(ApplicationName string) *pgxpool.Pool {
|
|||||||
// }
|
// }
|
||||||
// defer rows.Close()
|
// defer rows.Close()
|
||||||
|
|
||||||
func RawMultipleSQL(tx *pgxpool.Conn, TextSQL string) (pgx.Rows, error) {
|
func RawMultipleSQL(tx *pgxpool.Pool, TextSQL string) (pgx.Rows, error) {
|
||||||
var rows pgx.Rows
|
var rows pgx.Rows
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@@ -513,13 +513,17 @@ func RawMultipleSQL(tx *pgxpool.Conn, TextSQL string) (pgx.Rows, error) {
|
|||||||
func Ping_err(ctxMain context.Context) error {
|
func Ping_err(ctxMain context.Context) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
//ctx, cancelFunc := context.WithTimeout(ctxMain, timeOutSeconds*time.Second)
|
ctx, cancelFunc := context.WithTimeout(ctxMain, timeOutSeconds*time.Second)
|
||||||
ctx, cancelFunc := context.WithTimeout(ctxMain, 1*time.Second)
|
//ctx, cancelFunc := context.WithTimeout(ctxMain, 1*time.Second)
|
||||||
defer cancelFunc()
|
defer cancelFunc()
|
||||||
|
|
||||||
//mutex_Connect.Lock() //убрал т.к. зависает всё
|
//mutex_Connect.Lock() //убрал т.к. зависает всё
|
||||||
//defer mutex_Connect.Unlock()
|
//defer mutex_Connect.Unlock()
|
||||||
|
|
||||||
_, err = PgxPool.Exec(ctx, ";")
|
_, err = GetConnection().Exec(ctx, ";")
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("Ping_err() Exec() error: %w", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,9 +115,9 @@ func TestConnect_WithApplicationName_err(t *testing.T) {
|
|||||||
|
|
||||||
func TestRawMultipleSQL(t *testing.T) {
|
func TestRawMultipleSQL(t *testing.T) {
|
||||||
config_main.LoadEnv()
|
config_main.LoadEnv()
|
||||||
connection, _ := GetConnection_err()
|
connection := GetConnection()
|
||||||
defer connection.Release()
|
//defer connection.Release()
|
||||||
//defer CloseConnection()
|
defer CloseConnection()
|
||||||
|
|
||||||
TimeStart := time.Now()
|
TimeStart := time.Now()
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ SELECT * FROM temp_TestRawMultipleSQL2
|
|||||||
// TestRawMultipleSQL2 - negative test, with error
|
// TestRawMultipleSQL2 - negative test, with error
|
||||||
func TestRawMultipleSQL2(t *testing.T) {
|
func TestRawMultipleSQL2(t *testing.T) {
|
||||||
config_main.LoadEnv()
|
config_main.LoadEnv()
|
||||||
GetConnection_err()
|
GetConnection()
|
||||||
defer CloseConnection()
|
defer CloseConnection()
|
||||||
|
|
||||||
TimeStart := time.Now()
|
TimeStart := time.Now()
|
||||||
|
|||||||
Reference in New Issue
Block a user