Commit 899cc402 authored by ale's avatar ale

Close sql.Stmt objects that are bound to the current transaction

parent cf09f6a2
Pipeline #7917 passed with stages
in 1 minute and 40 seconds
......@@ -34,7 +34,9 @@ func (d *analysisService) Close() {
func (d *analysisService) CheckDevice(ctx context.Context, username string, deviceInfo *auth.DeviceInfo) (bool, error) {
var seen bool
err := withReadonlyTX(d.db, func(tx *sql.Tx) error {
err := d.stmts.get(tx, "check_device_info").QueryRow(username, deviceInfo.ID).Scan(&seen)
stmt := d.stmts.get(tx, "check_device_info")
defer stmt.Close()
err := stmt.QueryRow(username, deviceInfo.ID).Scan(&seen)
if err != nil && err != sql.ErrNoRows {
return err
}
......
......@@ -74,7 +74,9 @@ func (l *lastloginDB) AddLastLogin(ctx context.Context, entry *usermetadb.LastLo
return retryBusy(ctx, func() error {
return withTX(l.db, func(tx *sql.Tx) error {
_, err := l.stmts.get(tx, stmt).Exec(args...)
stmt := l.stmts.get(tx, stmt)
defer stmt.Close()
_, err := stmt.Exec(args...)
return err
})
})
......@@ -98,7 +100,9 @@ func (l *lastloginDB) GetLastLogin(ctx context.Context, username string, service
var entries []*usermetadb.LastLoginEntry
err := withReadonlyTX(l.db, func(tx *sql.Tx) error {
rows, err := l.stmts.get(tx, stmt).Query(args...)
stmt := l.stmts.get(tx, stmt)
defer stmt.Close()
rows, err := stmt.Query(args...)
if err != nil {
return err
}
......@@ -147,6 +151,7 @@ func (l *lastloginDB) GetUnusedAccounts(ctx context.Context, usernames []string,
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var username string
if err := rows.Scan(&username); err != nil {
......
......@@ -89,7 +89,9 @@ func compactLogs(db *sql.DB, stmts statementMap, username string, count int) err
if err != nil {
return err
}
if _, err := stmts.get(tx, "consolidate_userlog").Exec(username, count); err != nil {
stmt := stmts.get(tx, "consolidate_userlog")
defer stmt.Close()
if _, err := stmt.Exec(username, count); err != nil {
return err
}
return tx.Commit()
......@@ -99,12 +101,15 @@ func compactLogs(db *sql.DB, stmts statementMap, username string, count int) err
func pruneLogs(db *sql.DB, stmts statementMap, pruneCutoffDays int) error {
// Run each batch deletion in its own transaction, just to be nice.
cutoff := time.Now().AddDate(0, 0, -pruneCutoffDays)
for _, stmt := range []string{"prune_userlog", "prune_device_info"} {
for _, stmtName := range []string{"prune_userlog", "prune_device_info"} {
tx, err := db.Begin()
if err != nil {
return err
}
if _, err := stmts.get(tx, stmt).Exec(cutoff); err != nil {
stmt := stmts.get(tx, stmtName)
_, err = stmt.Exec(cutoff)
stmt.Close()
if err != nil {
tx.Rollback() // nolint
return err
}
......
......@@ -168,9 +168,13 @@ func (u *userlogDB) updateDeviceInfo(tx *sql.Tx, username string, deviceInfo *au
now := roundTimestamp(time.Now().UTC())
var seen bool
err := u.stmts.get(tx, "check_device_info").QueryRow(username, deviceInfo.ID).Scan(&seen)
stmt := u.stmts.get(tx, "check_device_info")
defer stmt.Close()
err := stmt.QueryRow(username, deviceInfo.ID).Scan(&seen)
if err == sql.ErrNoRows {
_, err = u.stmts.get(tx, "insert_device_info").Exec(
insStmt := u.stmts.get(tx, "insert_device_info")
defer insStmt.Close()
_, err = insStmt.Exec(
username,
deviceInfo.ID,
deviceInfo.RemoteZone,
......@@ -188,7 +192,9 @@ func (u *userlogDB) updateDeviceInfo(tx *sql.Tx, username string, deviceInfo *au
// legitimately happen that the other parameters
// change too (like when restoring a backup to another
// platform/os), so we update them too.
_, err = u.stmts.get(tx, "update_device_info").Exec(
insStmt := u.stmts.get(tx, "update_device_info")
defer insStmt.Close()
_, err = insStmt.Exec(
now,
deviceInfo.RemoteZone,
deviceInfo.UserAgent,
......@@ -212,7 +218,7 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
// If we're given a DeviceInfo entry, update it or create it
// if it does not exist yet.
stmt := "insert_userlog"
stmtName := "insert_userlog"
args := []interface{}{
entry.Username,
entry.Service,
......@@ -222,7 +228,7 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
entry.Timestamp,
}
if entry.DeviceInfo != nil {
stmt = "insert_userlog_with_device_info"
stmtName = "insert_userlog_with_device_info"
args = append(args, entry.DeviceInfo.ID)
args = append(args, entry.DeviceInfo.RemoteZone)
args = append(args, entry.DeviceInfo.UserAgent)
......@@ -238,7 +244,9 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
return err
}
}
_, err := u.stmts.get(tx, stmt).Exec(args...)
stmt := u.stmts.get(tx, stmtName)
defer stmt.Close()
_, err := stmt.Exec(args...)
return err
})
}); err != nil {
......@@ -263,7 +271,9 @@ func (u *userlogDB) GetUserLogs(ctx context.Context, username string, maxDays, l
var out []*usermetadb.LogEntry
err := withReadonlyTX(u.db, func(tx *sql.Tx) error {
rows, err := u.stmts.get(tx, "get_user_logs").Query(username, cutoff, limit)
stmt := u.stmts.get(tx, "get_user_logs")
defer stmt.Close()
rows, err := stmt.Query(username, cutoff, limit)
if err != nil {
return err
}
......@@ -301,7 +311,9 @@ func (u *userlogDB) GetUserLogs(ctx context.Context, username string, maxDays, l
func (u *userlogDB) GetUserDevices(ctx context.Context, username string) ([]*usermetadb.MetaDeviceInfo, error) {
var out []*usermetadb.MetaDeviceInfo
err := withReadonlyTX(u.db, func(tx *sql.Tx) error {
rows, err := u.stmts.get(tx, "devices_with_counts").Query(username)
stmt := u.stmts.get(tx, "devices_with_counts")
defer stmt.Close()
rows, err := stmt.Query(username)
if err != nil {
return err
}
......
......@@ -92,11 +92,13 @@ func bulkLoadTestLogs(t testing.TB, db *sql.DB) *usermetadb.LogEntry {
t.Fatalf("insert_userlog: %v", err)
}
insertLogStmt := tx.Stmt(stmt)
defer insertLogStmt.Close()
stmt, err = db.Prepare(userlogDBStatements["insert_device_info"])
if err != nil {
t.Fatalf("insert_device_info: %v", err)
}
insertDeviceInfoStmt := tx.Stmt(stmt)
defer insertDeviceInfoStmt.Close()
userDevices := generateAllRandomDevices()
entries := generateTestLogs(1000, userDevices)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment