This commit is contained in:
Asim Aslam
2019-05-30 23:52:10 +01:00
parent 8618183508
commit b4874806df
19 changed files with 687 additions and 8 deletions

23
util/http/http.go Normal file
View File

@@ -0,0 +1,23 @@
package http
import (
"net/http"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
)
func NewRoundTripper(opts ...Option) http.RoundTripper {
options := Options{
Registry: registry.DefaultRegistry,
}
for _, o := range opts {
o(&options)
}
return &roundTripper{
rt: http.DefaultTransport,
st: selector.Random,
opts: options,
}
}

87
util/http/http_test.go Normal file
View File

@@ -0,0 +1,87 @@
package http
import (
"io/ioutil"
"net"
"net/http"
"strconv"
"testing"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/memory"
)
func TestRoundTripper(t *testing.T) {
m := memory.NewRegistry()
rt := NewRoundTripper(
WithRegistry(m),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello world`))
})
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go http.Serve(l, nil)
host, p, _ := net.SplitHostPort(l.Addr().String())
port, _ := strconv.Atoi(p)
m.Register(&registry.Service{
Name: "example.com",
Nodes: []*registry.Node{
{
Id: "1",
Address: host,
Port: port,
},
},
})
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatal(err)
}
w, err := rt.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
w.Body.Close()
if string(b) != "hello world" {
t.Fatal("response is", string(b))
}
// test http request
c := &http.Client{
Transport: rt,
}
rsp, err := c.Get("http://example.com")
if err != nil {
t.Fatal(err)
}
b, err = ioutil.ReadAll(rsp.Body)
if err != nil {
t.Fatal(err)
}
rsp.Body.Close()
if string(b) != "hello world" {
t.Fatal("response is", string(b))
}
}

17
util/http/options.go Normal file
View File

@@ -0,0 +1,17 @@
package http
import (
"github.com/micro/go-micro/registry"
)
type Options struct {
Registry registry.Registry
}
type Option func(*Options)
func WithRegistry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
}
}

40
util/http/roundtripper.go Normal file
View File

@@ -0,0 +1,40 @@
package http
import (
"errors"
"fmt"
"net/http"
"github.com/micro/go-micro/selector"
)
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
}
req.URL.Host = fmt.Sprintf("%s:%d", n.Address, n.Port)
w, err := r.rt.RoundTrip(req)
if err != nil {
continue
}
return w, nil
}
return nil, errors.New("failed request")
}