max redirects should be configurable
@@ -51,7 +51,10 @@ var (
}
)
-const debug = false
+const (
+ debug = false
+ defaultMaxRedirectAttempts = 9 // per-Do invocation
+)
// DoFunc sends an HTTP request and returns an HTTP response.
//
@@ -85,6 +88,7 @@ type Client struct {
do DoFunc
header http.Header
codec *encoding.Codec
+ maxRedirects int
}
// New returns a new Client with the given Opts applied.
@@ -95,6 +99,7 @@ func New(opts ...Opt) *Client {
codec: &encoding.ProtobufCodec,
do: With(),
header: http.Header{},
+ maxRedirects: defaultMaxRedirectAttempts,
}
c.With(opts...)
return c
@@ -121,8 +126,6 @@ func (c *Client) With(opts ...Opt) Opt {
return last
}
-const maxRedirectAttempts = 9
-
// Do sends a Call and returns a streaming Decoder from which callers can read
// Events from, an io.Closer to close the event stream on graceful termination
// and an error in case of failure. Callers are expected to *always* close a
@@ -181,7 +184,7 @@ func (c *Client) Do(m encoding.Marshaler, opt ...RequestOpt) (*Response, error)
if debug {
log.Println("master changed!")
}
- if attempt < maxRedirectAttempts {
+ if attempt < c.maxRedirects {
attempt++
newMaster := res.Header.Get("Location")
if newMaster != "" {
@@ -219,6 +222,15 @@ func (c *Client) Do(m encoding.Marshaler, opt ...RequestOpt) (*Response, error)
}
}
+// MaxRedirects returns an Opt that sets the max number of redirect attempts per-Do.
+func MaxRedirects(mr int) Opt {
+ return func(c *Client) Opt {
+ old := c.maxRedirects
+ c.maxRedirects = mr
+ return MaxRedirects(old)
+ }
+}
+
// URL returns an Opt that sets a Client's URL.
func URL(rawurl string) Opt {
return func(c *Client) Opt {
