Discard request for image canceled by the client before any further processing

This commit is contained in:
Deluan 2023-02-02 14:53:28 -05:00
parent 3c5032a3e8
commit 588ee94f7c
2 changed files with 11 additions and 2 deletions

View File

@ -14,6 +14,11 @@ import (
)
func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
// If context is already canceled, discard request without further processing
if r.Context().Err() != nil {
return
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
id := r.URL.Query().Get(":id")
@ -27,10 +32,9 @@ func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
size := utils.ParamInt(r, "size", 0)
imgReader, lastUpdate, err := p.artwork.Get(ctx, artId, size)
imgReader, lastUpdate, err := p.artwork.Get(ctx, artId, size)
switch {
case errors.Is(err, context.Canceled):
return

View File

@ -53,6 +53,11 @@ func (api *Router) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request)
}
func (api *Router) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
// If context is already canceled, discard request without further processing
if r.Context().Err() != nil {
return nil, nil //nolint:nilerr
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()