Skip to content

Commit

Permalink
solve database is locked error (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark0smith authored Oct 20, 2023
1 parent 10ddfd1 commit 4885929
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ func init() {
Logger: logger.Discard,
})
checkErr(err)

// According to https://github.com/mattn/go-sqlite3/issues/274#issuecomment-191597862,
// solve "database is locked" error using `db.SetMaxOpenConns(1)`
sqlDB, err := DB.DB()
checkErr(err)
sqlDB.SetMaxOpenConns(1)

DB.AutoMigrate(&ProxyURL{})

}

func CreateProxyURL(url string) error {
Expand Down Expand Up @@ -93,6 +101,7 @@ func RandomProxyURL(regionFlag int, strategyFlag int) (pu string, markUnavail fu
switch regionFlag {
case 1:
tx = DB.Raw(fmt.Sprintf("SELECT * FROM %s WHERE available = ? AND can_bypass_gfw = ? ORDER BY RANDOM() LIMIT 1;", proxyURL.TableName()), true, false).Scan(&proxyURL)

case 2:
tx = DB.Raw(fmt.Sprintf("SELECT * FROM %s WHERE available = ? AND can_bypass_gfw = ? ORDER BY RANDOM() LIMIT 1;", proxyURL.TableName()), true, true).Scan(&proxyURL)
default:
Expand Down

0 comments on commit 4885929

Please sign in to comment.