fix: missing data source issue

This commit is contained in:
Marvin Zhang
2024-06-26 12:37:24 +08:00
parent 5daeccb87d
commit 326a8d67d0
18 changed files with 843 additions and 45 deletions

View File

@@ -58,3 +58,48 @@ func getPostgresqlSession(ctx context.Context, ds *models.DataSource) (s db.Sess
return s, err
}
func GetPostgresqlSessionWithTimeoutV2(ds *models.DataSourceV2, timeout time.Duration) (s db.Session, err error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return getPostgresqlSessionV2(ctx, ds)
}
func getPostgresqlSessionV2(ctx context.Context, ds *models.DataSourceV2) (s db.Session, err error) {
// normalize settings
host := ds.Host
port := ds.Port
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
port = constants.DefaultPostgresqlPort
}
// connect settings
settings := postgresql.ConnectionURL{
User: ds.Username,
Password: ds.Password,
Database: ds.Database,
Host: fmt.Sprintf("%s:%s", host, port),
Options: nil,
}
// session
done := make(chan struct{})
go func() {
s, err = postgresql.Open(settings)
close(done)
}()
// wait for done
select {
case <-ctx.Done():
if ctx.Err() != nil {
err = ctx.Err()
}
case <-done:
}
return s, err
}