Add util
This commit is contained in:
23
util/http/http.go
Normal file
23
util/http/http.go
Normal 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
87
util/http/http_test.go
Normal 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(®istry.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
17
util/http/options.go
Normal 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
40
util/http/roundtripper.go
Normal 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")
|
||||
}
|
||||
Reference in New Issue
Block a user