2019-05-31 01:52:10 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/selector"
|
2019-05-31 01:52:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type roundTripper struct {
|
|
|
|
rt http.RoundTripper
|
2020-07-01 19:06:59 +03:00
|
|
|
st selector.Selector
|
2019-05-31 01:52:10 +03:00
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
2020-08-21 11:23:01 +03:00
|
|
|
routes, err := r.opts.Router.Lookup(req.URL.Host)
|
2019-05-31 01:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// rudimentary retry 3 times
|
2020-08-18 00:44:45 +03:00
|
|
|
for _, route := range routes {
|
2020-07-01 19:06:59 +03:00
|
|
|
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")
|
|
|
|
}
|