Add log warn when request is cancelled/interrupted

This commit is contained in:
Deluan 2022-07-27 14:27:18 -04:00
parent cd5bce7b16
commit c0066ebd85
2 changed files with 15 additions and 3 deletions

View File

@ -192,6 +192,10 @@ func h(r chi.Router, path string, f handler) {
sendError(w, r, err)
return
}
if r.Context().Err() != nil {
log.Warn("Request was interrupted", "path", path, r.Context().Err())
return
}
if res != nil {
sendResponse(w, r, res)
}

View File

@ -60,10 +60,14 @@ func doSearch[T any](ctx context.Context, wg *sync.WaitGroup, s searchFunc[T], q
}
done := make(chan struct{})
go func() {
typ := reflect.TypeOf(res).String()
var err error
start := time.Now()
res, err = s(q, offset, size)
if err != nil {
log.Error(ctx, "Error searching "+reflect.TypeOf(res).String(), err)
log.Error(ctx, "Error searching "+typ, err)
} else {
log.Trace(ctx, "Search for "+typ+" completed", "elapsedTime", time.Since(start))
}
done <- struct{}{}
}()
@ -85,8 +89,12 @@ func (c *SearchingController) searchAll(r *http.Request, sp *searchParams) (medi
go func() { artists = doSearch(ctx, wg, c.ds.Artist(ctx).Search, q, sp.artistOffset, sp.artistCount) }()
wg.Wait()
log.Debug(ctx, fmt.Sprintf("Search resulted in %d songs, %d albums and %d artists",
len(mediaFiles), len(albums), len(artists)), "query", sp.query, "elapsedTime", time.Since(start))
if ctx.Err() == nil {
log.Debug(ctx, fmt.Sprintf("Search resulted in %d songs, %d albums and %d artists",
len(mediaFiles), len(albums), len(artists)), "query", sp.query, "elapsedTime", time.Since(start))
} else {
log.Warn(ctx, "Search was interrupted", ctx.Err(), "query", sp.query, "elapsedTime", time.Since(start))
}
return mediaFiles, albums, artists
}