Skip to content

Commit f3f6cfe

Browse files
committed
wip: reduce SQLite locked database errors
1 parent fac93d2 commit f3f6cfe

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

internal/storage/sqlite/sqlite.go

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,71 @@ func New(path string) (*SQLiteStorage, error) {
6262

6363
// Implement UserStorage interface
6464
func (s *SQLiteStorage) SaveUser(id string, publicKey []byte) error {
65-
_, err := s.Db.Exec(`INSERT INTO users (id, public_key) VALUES (?, ?)`, id, publicKey)
65+
var err error
66+
for {
67+
_, err = s.Db.Exec(`INSERT INTO users (id, public_key) VALUES (?, ?)`, id, publicKey)
68+
if isSQLiteBusy(err) {
69+
continue
70+
}
71+
break
72+
}
6673
return err
6774
}
6875

6976
func (s *SQLiteStorage) GetUserPublicKeyById(id string) ([]byte, error) {
70-
var publicKey []byte
71-
72-
err := s.Db.QueryRow("SELECT public_key FROM users WHERE id = ?", id).Scan(&publicKey)
73-
if err != nil {
74-
if err == sql.ErrNoRows {
75-
return nil, nil
76-
}
77-
return nil, err
78-
}
79-
80-
return publicKey, nil
77+
var (
78+
publicKey []byte
79+
err error
80+
)
81+
82+
for {
83+
err = s.Db.QueryRow("SELECT public_key FROM users WHERE id = ?", id).Scan(&publicKey)
84+
if isSQLiteBusy(err) {
85+
continue
86+
}
87+
if err != nil {
88+
if err == sql.ErrNoRows {
89+
return nil, nil
90+
}
91+
return nil, err
92+
}
93+
break
94+
}
95+
96+
return publicKey, err
8197
}
8298

8399
func (s *SQLiteStorage) SaveChallenge(challenge []byte, id interface{}, publicKey interface{}) error {
84-
_, err := s.Db.Exec(`INSERT INTO challenges (challenge, id, public_key) VALUES (?, ?, ?)`, challenge, id, publicKey)
100+
var err error
101+
for {
102+
_, err = s.Db.Exec(`INSERT INTO challenges (challenge, id, public_key) VALUES (?, ?, ?)`, challenge, id, publicKey)
103+
if isSQLiteBusy(err) {
104+
continue
105+
}
106+
break
107+
}
85108
return err
86109
}
87110

88111
func (s *SQLiteStorage) SaveServerInfo(url string, publicKey []byte, refetchDate string) error {
89-
_, err := s.Db.Exec(`INSERT INTO servers (url, public_key, refetch_date) VALUES (?, ?, ?)`, url, publicKey, refetchDate)
90-
if err != nil {
91-
_, err = s.Db.Exec(`UPDATE servers SET public_key = ?, refetch_date = ? WHERE url = ?`, publicKey, refetchDate, url)
92-
if err != nil {
93-
return err
94-
}
95-
}
112+
var err error
113+
for {
114+
_, err = s.Db.Exec(`INSERT INTO servers (url, public_key, refetch_date) VALUES (?, ?, ?)`, url, publicKey, refetchDate)
115+
if err != nil {
116+
if isSQLiteBusy(err) {
117+
continue
118+
}
119+
120+
_, err = s.Db.Exec(`UPDATE servers SET public_key = ?, refetch_date = ? WHERE url = ?`, publicKey, refetchDate, url)
121+
if err != nil {
122+
if isSQLiteBusy(err) {
123+
continue
124+
}
125+
return err
126+
}
127+
}
128+
break
129+
}
96130
return err
97131
}
98132

@@ -106,6 +140,7 @@ func (s *SQLiteStorage) GetServerInfo(url string) ([]byte, string, error) {
106140
if err == sql.ErrNoRows {
107141
return nil, "", nil
108142
}
143+
109144
return nil, "", err
110145
}
111146

@@ -120,7 +155,10 @@ func (s *SQLiteStorage) GetChallengeData(challenge []byte) ([]byte, string, erro
120155

121156
err := s.Db.QueryRow("SELECT id, public_key FROM challenges WHERE challenge = ?", challenge).Scan(&userId, &publicKey)
122157
if err != nil {
123-
return nil, "", err
158+
if isSQLiteBusy(err) {
159+
return nil, "", nil
160+
}
161+
return nil, "", err
124162
}
125163

126164
if userId.Valid {
@@ -138,7 +176,14 @@ func (s *SQLiteStorage) GetChallengeData(challenge []byte) ([]byte, string, erro
138176
}
139177

140178
func (s *SQLiteStorage) CleanupChallenges() error {
141-
_, err := s.Db.Exec(`DELETE FROM challenges`)
179+
var err error
180+
for {
181+
_, err = s.Db.Exec(`DELETE FROM challenges`)
182+
if isSQLiteBusy(err) {
183+
continue
184+
}
185+
break
186+
}
142187
return err
143188
}
144189

0 commit comments

Comments
 (0)