Create background context for background update to ensure that the DB increment doesn't get terminated when the request finishes

This commit is contained in:
David Dworken 2023-10-14 16:52:44 -07:00
parent 1def4edc00
commit 218c70f5e7
No known key found for this signature in database

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html" "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. // blocking the entire response. This does have a potential race condition, but that is fine.
if s.isProductionEnvironment { if s.isProductionEnvironment {
go func() { go func() {
span, ctx := tracer.StartSpanFromContext(ctx, "apiQueryHandler.incrementReadCount") span, backgroundCtx := tracer.StartSpanFromContext(context.Background(), "apiQueryHandler.incrementReadCount")
err := s.db.IncrementEntryReadCountsForDevice(ctx, deviceId) err := s.db.IncrementEntryReadCountsForDevice(backgroundCtx, deviceId)
if err != nil {
fmt.Printf("failed to increment read counts: %v\n", err)
}
span.Finish(tracer.WithError(err)) span.Finish(tracer.WithError(err))
}() }()
} else { } else {