Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
internal/providers: fix race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksherron committed Feb 5, 2020
1 parent f8e6eb2 commit b097d5c
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 87 deletions.
13 changes: 6 additions & 7 deletions internal/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,26 @@ func dbCacheStats() {

func loadDb(proxy *Proxy) {
defer mutex.Unlock()
mutex.Lock()
_, err := DB.Exec(`insert into proxies("created_at", "updated_at", "check_count", "country", "fail_count",
"last_status", "proxy", "timeout_count", "source", "success_count", "anonymous", "losing_streak")
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)
ON CONFLICT (proxy) DO UPDATE SET updated_at = EXCLUDED.updated_at
`, time.Now(), time.Now(), proxy.CheckCount, proxy.Country, proxy.FailCount,
proxy.LastStatus, proxy.Proxy, proxy.TimeoutCount, proxy.Source, proxy.SuccessCount, proxy.Anonymous, proxy.LosingStreak)
mutex.Lock()
`, time.Now(), time.Now(), &proxy.CheckCount, &proxy.Country, &proxy.FailCount,
&proxy.LastStatus, &proxy.Proxy, &proxy.TimeoutCount, &proxy.Source, &proxy.SuccessCount, &proxy.Anonymous, &proxy.LosingStreak)
if err != nil {
log.Fatal(err)
}
}

func dbInsert(proxy *Proxy) {
defer mutex.Unlock()

mutex.Lock()
_, err := DB.Exec(`update proxies SET "updated_at" = $1, "check_count" = $2 ,"fail_count" = $3,
"last_status" = $4, "timeout_count" = $5, "success_count" = $6, "losing_streak" = $7,
"deleted" = $8, "anonymous" = $9 , "proxy" = $10, judge = $11, "resp_time" = $12 where id = $13`,
time.Now(), proxy.CheckCount, proxy.FailCount, proxy.LastStatus, proxy.TimeoutCount,
proxy.SuccessCount, proxy.LosingStreak, proxy.Deleted, proxy.Anonymous, proxy.Proxy, proxy.Judge, proxy.RespTime, proxy.ID)
mutex.Lock()
time.Now(), &proxy.CheckCount, &proxy.FailCount, &proxy.LastStatus, &proxy.TimeoutCount,
&proxy.SuccessCount, &proxy.LosingStreak, &proxy.Deleted, &proxy.Anonymous, &proxy.Proxy, &proxy.Judge, &proxy.RespTime, &proxy.ID)

if err != nil {
log.Println(err)
Expand Down
Loading

0 comments on commit b097d5c

Please sign in to comment.