micro/util/http/roundtripper.go
ben-toogood 174e44b846
Deprecate client/selector (#1767)
* client/{grpc,rpc}: depricate selector (wip)

* {client,cmd}: remove client/selector

* deprecate client/selector

* router/static: fix lookup

* config/cmd: add support for legacy static selector flag

* config/cmd: add support for legacy dns selector flag
2020-07-01 17:06:59 +01:00

41 lines
699 B
Go

package http
import (
"errors"
"net/http"
"github.com/micro/go-micro/v2/router"
"github.com/micro/go-micro/v2/selector"
)
type roundTripper struct {
rt http.RoundTripper
st selector.Selector
opts Options
}
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
routes, err := r.opts.Router.Lookup(router.QueryService(req.URL.Host))
if err != nil {
return nil, err
}
// rudimentary retry 3 times
for i := 0; i < 3; i++ {
route, err := r.st.Select(routes)
if err != nil {
continue
}
req.URL.Host = route.Address
w, err := r.rt.RoundTrip(req)
if err != nil {
continue
}
return w, nil
}
return nil, errors.New("failed request")
}