1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-11 17:18:28 +02:00
go-micro/util/http/roundtripper.go

40 lines
622 B
Go
Raw Normal View History

2019-05-31 00:52:10 +02:00
package http
import (
"errors"
"net/http"
2021-10-12 13:55:53 +02:00
"go-micro.dev/v4/selector"
2019-05-31 00:52:10 +02:00
)
type roundTripper struct {
rt http.RoundTripper
st selector.Strategy
opts Options
}
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
s, err := r.opts.Registry.GetService(req.URL.Host)
if err != nil {
return nil, err
}
next := r.st(s)
// rudimentary retry 3 times
for i := 0; i < 3; i++ {
n, err := next()
if err != nil {
continue
}
2019-07-08 09:01:42 +02:00
req.URL.Host = n.Address
2019-05-31 00:52:10 +02:00
w, err := r.rt.RoundTrip(req)
if err != nil {
continue
}
return w, nil
}
return nil, errors.New("failed request")
}