@@ -64,6 +64,14 @@ type databaseConf struct {
64
64
TablePrefix string `json:"tablePrefix" yaml:"tablePrefix"`
65
65
SingularTable bool `json:"singularTable" yaml:"singularTable"`
66
66
} `json:"namingStrategy" yaml:"namingStrategy"`
67
+ Pool poolConf `json:"pool" yaml:"pool"`
68
+ }
69
+
70
+ type poolConf struct {
71
+ ConnMaxIdleTime config.Duration `json:"connMaxIdleTime" yaml:"connMaxIdleTime"`
72
+ ConnMaxLifeTime config.Duration `json:"connMaxLifeTime" yaml:"connMaxLifeTime"`
73
+ MaxIdleConns int `json:"maxIdleConns" yaml:"maxIdleConns"`
74
+ MaxOpenConns int `json:"maxOpenConns" yaml:"maxOpenConns"`
67
75
}
68
76
69
77
type metricsConf struct {
@@ -152,7 +160,7 @@ func provideGormConfig(l log.Logger, conf *databaseConf) *gorm.Config {
152
160
// provideDialector and provideGormConfig. Gorm opens connection to database
153
161
// while building *gorm.db. This means if the database is not available, the system
154
162
// will fail when initializing dependencies.
155
- func provideGormDB (dialector gorm.Dialector , config * gorm.Config , tracer opentracing.Tracer ) (* gorm.DB , func (), error ) {
163
+ func provideGormDB (dialector gorm.Dialector , config * gorm.Config , tracer opentracing.Tracer , conf poolConf ) (* gorm.DB , func (), error ) {
156
164
db , err := gorm .Open (dialector , config )
157
165
158
166
var nerr * net.OpError
@@ -164,6 +172,24 @@ func provideGormDB(dialector gorm.Dialector, config *gorm.Config, tracer opentra
164
172
if tracer != nil {
165
173
AddGormCallbacks (db , tracer )
166
174
}
175
+
176
+ sqlDB , err := db .DB ()
177
+ if err != nil {
178
+ return nil , nil , err
179
+ }
180
+ if ! conf .ConnMaxIdleTime .IsZero () {
181
+ sqlDB .SetConnMaxIdleTime (conf .ConnMaxIdleTime .Duration )
182
+ }
183
+ if ! conf .ConnMaxLifeTime .IsZero () {
184
+ sqlDB .SetConnMaxLifetime (conf .ConnMaxLifeTime .Duration )
185
+ }
186
+ if conf .MaxIdleConns > 0 {
187
+ sqlDB .SetMaxIdleConns (conf .MaxIdleConns )
188
+ }
189
+ if conf .MaxOpenConns > 0 {
190
+ sqlDB .SetMaxOpenConns (conf .MaxOpenConns )
191
+ }
192
+
167
193
return db , func () {
168
194
if sqlDb , err := db .DB (); err == nil {
169
195
sqlDb .Close ()
@@ -202,7 +228,7 @@ func provideDBFactory(options *providersOption) func(p factoryIn) (databaseOut,
202
228
}
203
229
gormConfig := provideGormConfig (logger , & conf )
204
230
options .interceptor (name , gormConfig )
205
- conn , cleanup , err = provideGormDB (dialector , gormConfig , factoryIn .Tracer )
231
+ conn , cleanup , err = provideGormDB (dialector , gormConfig , factoryIn .Tracer , conf . Pool )
206
232
if err != nil {
207
233
return di.Pair {}, err
208
234
}
@@ -259,6 +285,12 @@ func provideConfig() configOut {
259
285
TablePrefix string `json:"tablePrefix" yaml:"tablePrefix"`
260
286
SingularTable bool `json:"singularTable" yaml:"singularTable"`
261
287
}{},
288
+ Pool : poolConf {
289
+ MaxIdleConns : 1 ,
290
+ MaxOpenConns : 10 ,
291
+ ConnMaxIdleTime : config.Duration {Duration : 5 * time .Minute },
292
+ ConnMaxLifeTime : config.Duration {Duration : 30 * time .Minute },
293
+ },
262
294
},
263
295
},
264
296
"gormMetrics" : metricsConf {
0 commit comments