1
Fork 0

Reduce response time by using goroutine to update hit counter

This commit is contained in:
Jan-Lukas Else 2020-03-29 18:36:44 +02:00
parent 4d04abb778
commit 9a0ae07dfe
1 changed files with 7 additions and 10 deletions

17
main.go
View File

@ -218,16 +218,13 @@ func ShortenedUrlHandler(w http.ResponseWriter, r *http.Request) {
return
}
stmt, err := db.Prepare("UPDATE redirect SET hits = hits + 1 WHERE slug = ?")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = stmt.Exec(slug)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
stmt, err := db.Prepare("UPDATE redirect SET hits = hits + 1 WHERE slug = ?")
if err != nil {
return
}
_, _ = stmt.Exec(slug)
}()
http.Redirect(w, r, redirectUrl, http.StatusTemporaryRedirect)
}