Skip to content

Commit

Permalink
feat: return Retry-After header in case of 429/Too Many Requests on t…
Browse files Browse the repository at this point in the history
…humbnail WebDAV endpoint
  • Loading branch information
DeepDiver1975 committed May 22, 2024
1 parent 58bd67b commit e003d29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/unreleased/thumbnail-request-limit.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ The number of concurrent requests to the thumbnail service can be limited now
to have more control over the consumed system resources.

https://github.com/owncloud/ocis/pull/9199
https://github.com/owncloud/ocis/pull/9240
16 changes: 15 additions & 1 deletion ocis-pkg/middleware/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ package middleware

import (
"net/http"
"time"

"github.com/go-chi/chi/v5/middleware"
)

func retryAfterFn(ctxDone bool) time.Duration {
if ctxDone {
return time.Minute
}
return time.Minute * 5
}

// Throttle limits the number of concurrent requests.
func Throttle(limit int) func(http.Handler) http.Handler {
if limit > 0 {
return middleware.Throttle(limit)
opts := middleware.ThrottleOpts{
RetryAfterFn: retryAfterFn,
Limit: limit,
BacklogLimit: 0,
BacklogTimeout: time.Minute,
}
return middleware.ThrottleWithOpts(opts)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
13 changes: 11 additions & 2 deletions services/webdav/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,13 @@ func (g Webdav) sendThumbnailResponse(rsp *thumbnailssvc.GetThumbnailResponse, w
// Timeout: time.Second * 5,
}

dlReq, err := http.NewRequest(http.MethodGet, rsp.DataEndpoint, http.NoBody)
dlReq, err := http.NewRequest(http.MethodGet, rsp.GetDataEndpoint(), http.NoBody)
if err != nil {
renderError(w, r, errInternalError(err.Error()))
logger.Error().Err(err).Msg("could not create download thumbnail request")
return
}
dlReq.Header.Set("Transfer-Token", rsp.TransferToken)
dlReq.Header.Set("Transfer-Token", rsp.GetTransferToken())

dlRsp, err := client.Do(dlReq)
if err != nil {
Expand All @@ -470,6 +470,15 @@ func (g Webdav) sendThumbnailResponse(rsp *thumbnailssvc.GetThumbnailResponse, w
}
defer dlRsp.Body.Close()

if dlRsp.StatusCode == http.StatusTooManyRequests {
retryAfter := dlRsp.Header.Get("Retry-After")
if retryAfter != "" {
w.Header().Set("Retry-After", retryAfter)
}
w.WriteHeader(http.StatusTooManyRequests)
return
}

if dlRsp.StatusCode != http.StatusOK {
logger.Debug().
Str("transfer_token", rsp.GetTransferToken()).
Expand Down

0 comments on commit e003d29

Please sign in to comment.