From 9a0ae07dfe5cae44eb013ff2fba1443f0817d3f7 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 29 Mar 2020 18:36:44 +0200 Subject: [PATCH] Reduce response time by using goroutine to update hit counter --- main.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 31d222f..c4c1789 100644 --- a/main.go +++ b/main.go @@ -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) }