commit f98d1330a85581cfceaa6371c78f26fa909c2d65 Author: Asim Aslam Date: Sun Jul 7 10:37:34 2019 +0100 Move transport to network/transport diff --git a/http.go b/http.go new file mode 100644 index 0000000..a3a6bbf --- /dev/null +++ b/http.go @@ -0,0 +1,11 @@ +// Package http returns a http2 transport using net/http +package http + +import ( + "github.com/micro/go-micro/network/transport" +) + +// NewTransport returns a new http transport using net/http and supporting http2 +func NewTransport(opts ...transport.Option) transport.Transport { + return transport.NewTransport(opts...) +} diff --git a/http_test.go b/http_test.go new file mode 100644 index 0000000..5744503 --- /dev/null +++ b/http_test.go @@ -0,0 +1,138 @@ +package http + +import ( + "sync" + "testing" + + "github.com/micro/go-micro/network/transport" +) + +func call(b *testing.B, c int) { + b.StopTimer() + + tr := NewTransport() + + // server listen + l, err := tr.Listen("localhost:0") + if err != nil { + b.Fatal(err) + } + defer l.Close() + + // socket func + fn := func(sock transport.Socket) { + defer sock.Close() + + for { + var m transport.Message + if err := sock.Recv(&m); err != nil { + return + } + + if err := sock.Send(&m); err != nil { + return + } + } + } + + done := make(chan bool) + + // accept connections + go func() { + if err := l.Accept(fn); err != nil { + select { + case <-done: + default: + b.Fatalf("Unexpected accept err: %v", err) + } + } + }() + + m := transport.Message{ + Header: map[string]string{ + "Content-Type": "application/json", + }, + Body: []byte(`{"message": "Hello World"}`), + } + + // client connection + client, err := tr.Dial(l.Addr()) + if err != nil { + b.Fatalf("Unexpected dial err: %v", err) + } + + send := func(c transport.Client) { + // send message + if err := c.Send(&m); err != nil { + b.Fatalf("Unexpected send err: %v", err) + } + + var rm transport.Message + // receive message + if err := c.Recv(&rm); err != nil { + b.Fatalf("Unexpected recv err: %v", err) + } + } + + // warm + for i := 0; i < 10; i++ { + send(client) + } + + client.Close() + + ch := make(chan int, c*4) + + var wg sync.WaitGroup + wg.Add(c) + + for i := 0; i < c; i++ { + go func() { + cl, err := tr.Dial(l.Addr()) + if err != nil { + b.Fatalf("Unexpected dial err: %v", err) + } + defer cl.Close() + + for range ch { + send(cl) + } + + wg.Done() + }() + } + + b.StartTimer() + + for i := 0; i < b.N; i++ { + ch <- i + } + + b.StopTimer() + close(ch) + + wg.Wait() + + // finish + close(done) +} + +func BenchmarkTransport1(b *testing.B) { + call(b, 1) +} + +func BenchmarkTransport8(b *testing.B) { + call(b, 8) +} + +func BenchmarkTransport16(b *testing.B) { + call(b, 16) +} + +func BenchmarkTransport64(b *testing.B) { + call(b, 64) +} + +func BenchmarkTransport128(b *testing.B) { + call(b, 128) +} diff --git a/options.go b/options.go new file mode 100644 index 0000000..3d70f94 --- /dev/null +++ b/options.go @@ -0,0 +1,23 @@ +package http + +import ( + "context" + "net/http" + + "github.com/micro/go-micro/network/transport" +) + +// Handle registers the handler for the given pattern. +func Handle(pattern string, handler http.Handler) transport.Option { + return func(o *transport.Options) { + if o.Context == nil { + o.Context = context.Background() + } + handlers, ok := o.Context.Value("http_handlers").(map[string]http.Handler) + if !ok { + handlers = make(map[string]http.Handler) + } + handlers[pattern] = handler + o.Context = context.WithValue(o.Context, "http_handlers", handlers) + } +}