Skip to content

Commit 5ac3d1c

Browse files
authored
fix: export some useful configuration (#240)
* fix: export some useful configuration * fix: imports * fix: modules * test: cover new configs * test: fix tests
1 parent 521d316 commit 5ac3d1c

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

default_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ env: local
2222
http:
2323
addr: :8080
2424
disable: false
25+
readTimeout: 2s
26+
writeTimeout: 10s
27+
idleTimeout: 10s
2528
grpc:
2629
addr: :9090
2730
disable: false

otgorm/dependency.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ type databaseConf struct {
6464
TablePrefix string `json:"tablePrefix" yaml:"tablePrefix"`
6565
SingularTable bool `json:"singularTable" yaml:"singularTable"`
6666
} `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"`
6775
}
6876

6977
type metricsConf struct {
@@ -152,7 +160,7 @@ func provideGormConfig(l log.Logger, conf *databaseConf) *gorm.Config {
152160
// provideDialector and provideGormConfig. Gorm opens connection to database
153161
// while building *gorm.db. This means if the database is not available, the system
154162
// 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) {
156164
db, err := gorm.Open(dialector, config)
157165

158166
var nerr *net.OpError
@@ -164,6 +172,24 @@ func provideGormDB(dialector gorm.Dialector, config *gorm.Config, tracer opentra
164172
if tracer != nil {
165173
AddGormCallbacks(db, tracer)
166174
}
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+
167193
return db, func() {
168194
if sqlDb, err := db.DB(); err == nil {
169195
sqlDb.Close()
@@ -202,7 +228,7 @@ func provideDBFactory(options *providersOption) func(p factoryIn) (databaseOut,
202228
}
203229
gormConfig := provideGormConfig(logger, &conf)
204230
options.interceptor(name, gormConfig)
205-
conn, cleanup, err = provideGormDB(dialector, gormConfig, factoryIn.Tracer)
231+
conn, cleanup, err = provideGormDB(dialector, gormConfig, factoryIn.Tracer, conf.Pool)
206232
if err != nil {
207233
return di.Pair{}, err
208234
}
@@ -259,6 +285,12 @@ func provideConfig() configOut {
259285
TablePrefix string `json:"tablePrefix" yaml:"tablePrefix"`
260286
SingularTable bool `json:"singularTable" yaml:"singularTable"`
261287
}{},
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+
},
262294
},
263295
},
264296
"gormMetrics": metricsConf{

otgorm/dependency_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ func TestProvideDBFactory(t *testing.T) {
2323
"default": map[string]interface{}{
2424
"database": "sqlite",
2525
"dsn": ":memory:",
26+
"pool": map[string]interface{}{
27+
"maxOpenConns": 10,
28+
"maxIdleConns": 10,
29+
"connMaxIdleTime": "10s",
30+
"connMaxLifetime": "10s",
31+
},
2632
},
2733
"alternative": map[string]interface{}{
2834
"database": "mysql",

serve.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"os/signal"
1010
"syscall"
1111

12+
"github.com/DoNewsCode/core/config"
1213
"github.com/DoNewsCode/core/contract"
13-
cron "github.com/DoNewsCode/core/cron"
14+
"github.com/DoNewsCode/core/cron"
1415
"github.com/DoNewsCode/core/deprecated_cronopts"
1516
"github.com/DoNewsCode/core/di"
1617
"github.com/DoNewsCode/core/logging"
@@ -56,12 +57,31 @@ func (s serveModule) ProvideCommand(command *cobra.Command) {
5657
type runGroupFunc func(ctx context.Context, logger logging.LevelLogger) (func() error, func(err error), error)
5758

5859
func (s serveIn) httpServe(ctx context.Context, logger logging.LevelLogger) (func() error, func(err error), error) {
59-
if s.Config.Bool("http.disable") {
60+
type httpConfig struct {
61+
Disable bool `json:"disable" yaml:"disable"`
62+
Addr string `json:"addr" yaml:"addr"`
63+
ReadTimeout config.Duration `json:"readTimeout" yaml:"readTimeout"`
64+
ReadHeaderTimeout config.Duration `json:"readHeaderTimeout" yaml:"readHeaderTimeout"`
65+
WriteTimeout config.Duration `json:"writeTimeout" yaml:"writeTimeout"`
66+
IdleTimeout config.Duration `json:"idleTimeout" yaml:"idleTimeout"`
67+
MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
68+
}
69+
70+
var conf httpConfig
71+
s.Config.Unmarshal("http", &conf)
72+
73+
if conf.Disable {
6074
return nil, nil, nil
6175
}
6276

6377
if s.HTTPServer == nil {
64-
s.HTTPServer = &http.Server{}
78+
s.HTTPServer = &http.Server{
79+
ReadTimeout: conf.ReadTimeout.Duration,
80+
ReadHeaderTimeout: conf.ReadHeaderTimeout.Duration,
81+
WriteTimeout: conf.WriteTimeout.Duration,
82+
IdleTimeout: conf.IdleTimeout.Duration,
83+
MaxHeaderBytes: conf.MaxHeaderBytes,
84+
}
6585
}
6686
router := mux.NewRouter()
6787
applyRouter(s.Container, router)
@@ -73,8 +93,7 @@ func (s serveIn) httpServe(ctx context.Context, logger logging.LevelLogger) (fun
7393
})
7494

7595
s.HTTPServer.Handler = router
76-
77-
httpAddr := s.Config.String("http.addr")
96+
httpAddr := conf.Addr
7897
ln, err := net.Listen("tcp", httpAddr)
7998
if err != nil {
8099
return nil, nil, errors.Wrap(err, "failed start http server")

0 commit comments

Comments
 (0)