Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return Retry-After header in case of 429/Too Many Requests on thumbnail WebDAV endpoint #9240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 14 additions & 7 deletions services/webdav/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ import (
"github.com/owncloud/ocis/v2/services/webdav/pkg/dav/requests"
)

func init() {
// register method with chi before any routing is set up
chi.RegisterMethod("REPORT")
}

var (
codesEnum = map[int]string{
http.StatusBadRequest: "Sabre\\DAV\\Exception\\BadRequest",
Expand All @@ -58,6 +53,9 @@ func NewService(opts ...Option) (Service, error) {
options := newOptions(opts...)
conf := options.Config

// register method with chi before any routing is set up
chi.RegisterMethod("REPORT")

m := chi.NewMux()
m.Use(
otelchi.Middleware(
Expand Down Expand Up @@ -454,13 +452,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 +468,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