fix(webdav): Set Retry-After header for 429 responses
We're now setting a 'Retry-After' in the webdav response when the thunbnailer is rate limiting the requests. For now the timeout is just a random number between 1 and 15 seconds.
This commit is contained in:
committed by
Ralf Haferkamp
parent
74d54579cb
commit
f536dacd4d
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
@@ -259,6 +261,7 @@ func (g Webdav) SpacesThumbnail(w http.ResponseWriter, r *http.Request) {
|
||||
renderError(w, r, errTooEarly(e.Detail))
|
||||
return
|
||||
case http.StatusTooManyRequests:
|
||||
addRetryAfterHeader(w)
|
||||
renderError(w, r, errTooManyRequests(e.Detail))
|
||||
case http.StatusBadRequest:
|
||||
renderError(w, r, errBadRequest(e.Detail))
|
||||
@@ -356,6 +359,7 @@ func (g Webdav) Thumbnail(w http.ResponseWriter, r *http.Request) {
|
||||
renderError(w, r, errTooEarly(e.Detail))
|
||||
return
|
||||
case http.StatusTooManyRequests:
|
||||
addRetryAfterHeader(w)
|
||||
renderError(w, r, errTooManyRequests(e.Detail))
|
||||
case http.StatusBadRequest:
|
||||
renderError(w, r, errBadRequest(e.Detail))
|
||||
@@ -404,6 +408,7 @@ func (g Webdav) PublicThumbnail(w http.ResponseWriter, r *http.Request) {
|
||||
case http.StatusBadRequest:
|
||||
renderError(w, r, errBadRequest(e.Detail))
|
||||
case http.StatusTooManyRequests:
|
||||
addRetryAfterHeader(w)
|
||||
renderError(w, r, errTooManyRequests(e.Detail))
|
||||
default:
|
||||
renderError(w, r, errInternalError(err.Error()))
|
||||
@@ -448,6 +453,7 @@ func (g Webdav) PublicThumbnailHead(w http.ResponseWriter, r *http.Request) {
|
||||
case http.StatusBadRequest:
|
||||
renderError(w, r, errBadRequest(e.Detail))
|
||||
case http.StatusTooManyRequests:
|
||||
addRetryAfterHeader(w)
|
||||
renderError(w, r, errTooManyRequests(e.Detail))
|
||||
default:
|
||||
renderError(w, r, errInternalError(err.Error()))
|
||||
@@ -566,3 +572,8 @@ func renderError(w http.ResponseWriter, r *http.Request, err *errResponse) {
|
||||
func notFoundMsg(name string) string {
|
||||
return "File with name " + name + " could not be located"
|
||||
}
|
||||
|
||||
func addRetryAfterHeader(w http.ResponseWriter) {
|
||||
after := rand.IntN(14) + 1
|
||||
w.Header().Set("Retry-After", strconv.Itoa(after))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user