Implement retry logic for UpdateAttributes

This commit is contained in:
Jason Streifling 2024-03-15 15:18:02 +01:00
parent 3d3fb3c826
commit 6d3a28a6ce
3 changed files with 40 additions and 24 deletions

View File

@ -20,8 +20,7 @@ func (db *DB) AddArticle(a *Article) (int64, error) {
query := `
INSERT INTO articles
(title, description, content, published, rejected, author_id)
VALUES
(?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?)
`
result, err := db.Exec(query, a.Title, a.Description, a.Content, a.Published,

View File

@ -5,14 +5,19 @@ import (
"database/sql"
"fmt"
"log"
"math"
"math/rand/v2"
"os"
"strings"
"syscall"
"time"
"github.com/go-sql-driver/mysql"
"golang.org/x/term"
)
var TxMaxRetries = 3
type DB struct {
*sql.DB
}
@ -92,6 +97,8 @@ func OpenDB(dbName string) (*DB, error) {
}
func (db *DB) UpdateAttributes(a ...*Attribute) error {
for i := 0; i < TxMaxRetries; i++ {
err := func() error {
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error starting transaction: %v", err)
@ -115,6 +122,17 @@ func (db *DB) UpdateAttributes(a ...*Attribute) error {
return fmt.Errorf("error committing transaction: %v", err)
}
return nil
}()
if err == nil {
return nil
}
log.Println(err)
waitTime := time.Duration(math.Pow(2, float64(i))) * time.Second
jitter := time.Duration(rand.IntN(1000)) * time.Millisecond
time.Sleep(waitTime + jitter)
}
return fmt.Errorf("error: %v unsuccessful retries for DB operation, aborting", TxMaxRetries)
}
func (db *DB) CountEntries(table string) (int64, error) {

View File

@ -28,8 +28,7 @@ func (db *DB) AddUser(user *User, pass string) error {
}
query := `
INSERT INTO users
(username, password, first_name, last_name, role)
INSERT INTO users (username, password, first_name, last_name, role)
VALUES (?, ?, ?, ?, ?)
`
if _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role); err != nil {