Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-10 08:09:17 -05:00
pkg-proxy/internal/handler/upstream_held.go

27 lines
647 B
Go
Raw Permalink Normal View History

package handler
import (
"errors"
"math"
"net/http"
"strconv"
"github.com/git-pkgs/proxy/internal/httpclient"
)
// writeUpstreamHeld answers a request the rate limit gate refused to send
// with 503 and the Retry-After the upstream asked for, and reports whether
// err was such a refusal.
func writeUpstreamHeld(w http.ResponseWriter, err error) bool {
var held *httpclient.HeldError
if !errors.As(err, &held) {
return false
}
secs := int(math.Ceil(held.RetryAfter.Seconds()))
if secs < 1 {
secs = 1
}
w.Header().Set("Retry-After", strconv.Itoa(secs))
http.Error(w, held.Error(), http.StatusServiceUnavailable)
return true
}