4
4
package collector
5
5
6
6
import (
7
+ "context"
7
8
"database/sql"
8
9
"fmt"
9
10
"github.com/godror/godror"
@@ -64,7 +65,6 @@ func (d *Database) constLabels() map[string]string {
64
65
65
66
func NewDatabase (logger * slog.Logger , dbname string , dbconfig DatabaseConfig ) * Database {
66
67
db , dbtype := connect (logger , dbname , dbconfig )
67
-
68
68
return & Database {
69
69
Name : dbname ,
70
70
Up : 0 ,
@@ -74,6 +74,42 @@ func NewDatabase(logger *slog.Logger, dbname string, dbconfig DatabaseConfig) *D
74
74
}
75
75
}
76
76
77
+ // WarmupConnectionPool serially acquires connections to "warm up" the connection pool.
78
+ // This is a workaround for a perceived bug in ODPI_C where rapid acquisition of connections
79
+ // results in a SIGABRT.
80
+ func (d * Database ) WarmupConnectionPool (logger * slog.Logger ) {
81
+ var connections []* sql.Conn
82
+ poolSize := d .Config .GetMaxOpenConns ()
83
+ if poolSize < 1 {
84
+ poolSize = d .Config .GetPoolMaxConnections ()
85
+ }
86
+ if poolSize > 100 { // defensively cap poolsize
87
+ poolSize = 100
88
+ }
89
+ warmup := func (i int ) {
90
+ time .Sleep (100 * time .Millisecond )
91
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
92
+ defer cancel ()
93
+
94
+ conn , err := d .Session .Conn (ctx )
95
+ if err != nil {
96
+ logger .Debug ("Failed to open database connection on warmup" , "conn" , i , "error" , err , "database" , d .Name )
97
+ return
98
+ }
99
+ connections = append (connections , conn )
100
+ }
101
+ for i := 0 ; i < poolSize ; i ++ {
102
+ warmup (i + 1 )
103
+ }
104
+
105
+ logger .Debug ("Warmed connection pool" , "total" , len (connections ), "database" , d .Name )
106
+ for i , conn := range connections {
107
+ if err := conn .Close (); err != nil {
108
+ logger .Debug ("Failed to return database connection to pool on warmup" , "conn" , i + 1 , "error" , err , "database" , d .Name )
109
+ }
110
+ }
111
+ }
112
+
77
113
func connect (logger * slog.Logger , dbname string , dbconfig DatabaseConfig ) (* sql.DB , float64 ) {
78
114
logger .Debug ("Launching connection to " + maskDsn (dbconfig .URL ), "database" , dbname )
79
115
0 commit comments