micro/util/http/roundtripper.go

35 lines
625 B
Go
Raw Normal View History

2019-05-31 01:52:10 +03:00
package http
import (
"errors"
"net/http"
"go.unistack.org/micro/v4/router"
"go.unistack.org/micro/v4/selector"
2019-05-31 01:52:10 +03:00
)
type roundTripper struct {
rt http.RoundTripper
st selector.Selector
2019-05-31 01:52:10 +03:00
opts Options
}
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
routes, err := r.opts.Router.Lookup(router.QueryService(req.URL.Host))
2019-05-31 01:52:10 +03:00
if err != nil {
return nil, err
}
// rudimentary retry 3 times
for _, route := range routes {
req.URL.Host = route.Address
2019-05-31 01:52:10 +03:00
w, err := r.rt.RoundTrip(req)
if err != nil {
continue
}
return w, nil
}
return nil, errors.New("failed request")
}