From 218c70f5e77a393107157389f09633741af6c3a5 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 14 Oct 2023 16:52:44 -0700 Subject: [PATCH] Create background context for background update to ensure that the DB increment doesn't get terminated when the request finishes --- backend/server/internal/server/api_handlers.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/server/internal/server/api_handlers.go b/backend/server/internal/server/api_handlers.go index 65469c4..af997d6 100644 --- a/backend/server/internal/server/api_handlers.go +++ b/backend/server/internal/server/api_handlers.go @@ -1,6 +1,7 @@ package server import ( + "context" "encoding/json" "fmt" "html" @@ -121,8 +122,11 @@ func (s *Server) apiQueryHandler(w http.ResponseWriter, r *http.Request) { // blocking the entire response. This does have a potential race condition, but that is fine. if s.isProductionEnvironment { go func() { - span, ctx := tracer.StartSpanFromContext(ctx, "apiQueryHandler.incrementReadCount") - err := s.db.IncrementEntryReadCountsForDevice(ctx, deviceId) + span, backgroundCtx := tracer.StartSpanFromContext(context.Background(), "apiQueryHandler.incrementReadCount") + err := s.db.IncrementEntryReadCountsForDevice(backgroundCtx, deviceId) + if err != nil { + fmt.Printf("failed to increment read counts: %v\n", err) + } span.Finish(tracer.WithError(err)) }() } else {