micro/util/http/roundtripper.go
Vasiliy Tolstov 06136312bb
regen files with never protoc (#6)
* regen files with never protoc
* rewrite import path

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-08-19 17:47:17 +03:00

35 lines
641 B
Go

package http
import (
"errors"
"net/http"
"github.com/unistack-org/micro/v3/router"
"github.com/unistack-org/micro/v3/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 _, route := range routes {
req.URL.Host = route.Address
w, err := r.rt.RoundTrip(req)
if err != nil {
continue
}
return w, nil
}
return nil, errors.New("failed request")
}