Skip to content

Commit 4215243

Browse files
committed
Cluster: Add comments to service/cluster/provisioner/database.go
Signed-off-by: Michael Mayer <[email protected]>
1 parent 7c2bb9b commit 4215243

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

internal/service/cluster/provisioner/database.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ func GetDB(ctx context.Context) (*sql.DB, error) {
7474
return db, nil
7575
}
7676

77+
// setDB stores the shared provisioning connection under write lock.
7778
func setDB(db *sql.DB) {
7879
dbMutex.Lock()
7980
defer dbMutex.Unlock()
8081
dbConn = db
8182
}
8283

84+
// pingWithTimeout validates liveness by issuing a ping bounded by d.
8385
func pingWithTimeout(ctx context.Context, db *sql.DB, d time.Duration) error {
8486
c, cancel := context.WithTimeout(ctx, d)
8587
defer cancel()
@@ -93,6 +95,7 @@ func pingWithTimeout(ctx context.Context, db *sql.DB, d time.Duration) error {
9395
// Allow only safe characters in generated identifiers (you can tighten/loosen this).
9496
var identRe = regexp.MustCompile(`^[a-z0-9\-_.]+$`)
9597

98+
// quoteIdent wraps an identifier in backticks after validating its characters.
9699
func quoteIdent(s string) (string, error) {
97100
if s == "" {
98101
return "", errors.New("empty identifier")
@@ -104,6 +107,7 @@ func quoteIdent(s string) (string, error) {
104107
return "`" + strings.ReplaceAll(s, "`", "``") + "`", nil
105108
}
106109

110+
// quoteString escapes and quotes a string literal for SQL statements.
107111
func quoteString(s string) (string, error) {
108112
if strings.ContainsRune(s, '\x00') {
109113
return "", errors.New("string contains NUL")
@@ -112,6 +116,7 @@ func quoteString(s string) (string, error) {
112116
return "'" + strings.ReplaceAll(s, "'", "''") + "'", nil
113117
}
114118

119+
// quoteAccount formats a user@host identifier using SQL quoting rules.
115120
func quoteAccount(host, user string) (string, error) {
116121
u, err := quoteString(user)
117122
if err != nil {
@@ -124,7 +129,7 @@ func quoteAccount(host, user string) (string, error) {
124129
return u + "@" + h, nil
125130
}
126131

127-
// Exec with a timeout.
132+
// execTimeout executes stmt with a deadline by wrapping the call in a cancelable context.
128133
func execTimeout(ctx context.Context, db *sql.DB, d time.Duration, stmt string) error {
129134
c, cancel := context.WithTimeout(ctx, d)
130135
defer cancel()

0 commit comments

Comments
 (0)