Move transport to network/transport

This commit is contained in:
Asim Aslam 2019-07-07 10:37:34 +01:00 committed by Vasiliy Tolstov
commit f98d1330a8
3 changed files with 172 additions and 0 deletions

11
http.go Normal file
View File

@ -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...)
}

138
http_test.go Normal file
View File

@ -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)
}

23
options.go Normal file
View File

@ -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)
}
}