sqltemplate, dbimpl: Remove single-method function types (#107525)
* Remove dbProviderFunc function. This removes one extra indirection that made the code bit more difficult to navigate. * Remove indirection function types implementing single-method interfaces. This streamlines the code and makes it bit easier to navigate. * Update pkg/storage/unified/sql/sqltemplate/dialect_mysql.go Co-authored-by: Mustafa Sencer Özcan <32759850+mustafasencer@users.noreply.github.com> --------- Co-authored-by: Mustafa Sencer Özcan <32759850+mustafasencer@users.noreply.github.com>
This commit is contained in:
@@ -43,21 +43,7 @@ func ProvideResourceDB(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.Trac
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("provide Resource DB: %w", err)
|
||||
}
|
||||
var once sync.Once
|
||||
var resourceDB db.DB
|
||||
|
||||
return dbProviderFunc(func(ctx context.Context) (db.DB, error) {
|
||||
once.Do(func() {
|
||||
resourceDB, err = p.init(ctx)
|
||||
})
|
||||
return resourceDB, err
|
||||
}), nil
|
||||
}
|
||||
|
||||
type dbProviderFunc func(context.Context) (db.DB, error)
|
||||
|
||||
func (f dbProviderFunc) Init(ctx context.Context) (db.DB, error) {
|
||||
return f(ctx)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
type resourceDBProvider struct {
|
||||
@@ -68,6 +54,10 @@ type resourceDBProvider struct {
|
||||
tracer trace.Tracer
|
||||
registerMetrics bool
|
||||
logQueries bool
|
||||
|
||||
once sync.Once
|
||||
resourceDB db.DB
|
||||
initErr error
|
||||
}
|
||||
|
||||
func newResourceDBProvider(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.Tracer) (p *resourceDBProvider, err error) {
|
||||
@@ -124,7 +114,14 @@ func newResourceDBProvider(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.
|
||||
}
|
||||
}
|
||||
|
||||
func (p *resourceDBProvider) init(ctx context.Context) (db.DB, error) {
|
||||
func (p *resourceDBProvider) Init(ctx context.Context) (db.DB, error) {
|
||||
p.once.Do(func() {
|
||||
p.resourceDB, p.initErr = p.initDB(ctx)
|
||||
})
|
||||
return p.resourceDB, p.initErr
|
||||
}
|
||||
|
||||
func (p *resourceDBProvider) initDB(ctx context.Context) (db.DB, error) {
|
||||
p.log.Info("Initializing Resource DB",
|
||||
"db_type",
|
||||
p.engine.Dialect().DriverName(),
|
||||
|
||||
Reference in New Issue
Block a user