From 06ee80fd0a9594fc38e68d16d2e7a7e9b2ed0ecf Mon Sep 17 00:00:00 2001 From: Asim Date: Sun, 22 May 2016 18:34:47 +0100 Subject: [PATCH 01/17] Strip old encoding --- registry/encoding.go | 51 +------------------------------------------- 1 file changed, 1 insertion(+), 50 deletions(-) diff --git a/registry/encoding.go b/registry/encoding.go index 900773b6..12143035 100644 --- a/registry/encoding.go +++ b/registry/encoding.go @@ -45,10 +45,6 @@ func encodeEndpoints(en []*Endpoint) []string { var tags []string for _, e := range en { if b, err := json.Marshal(e); err == nil { - // old encoding - // TODO: remove in 09/2016 - tags = append(tags, "e="+string(b)) - // new encoding tags = append(tags, "e-"+encode(b)) } } @@ -58,27 +54,14 @@ func encodeEndpoints(en []*Endpoint) []string { func decodeEndpoints(tags []string) []*Endpoint { var en []*Endpoint - // use the first format you find - var ver byte - for _, tag := range tags { if len(tag) == 0 || tag[0] != 'e' { continue } - // check version - if ver > 0 && tag[1] != ver { - continue - } - var e *Endpoint var buf []byte - // Old encoding was plain - if tag[1] == '=' { - buf = []byte(tag[2:]) - } - // New encoding is hex if tag[1] == '-' { buf = decode(tag[2:]) @@ -87,9 +70,6 @@ func decodeEndpoints(tags []string) []*Endpoint { if err := json.Unmarshal(buf, &e); err == nil { en = append(en, e) } - - // set version - ver = tag[1] } return en } @@ -100,9 +80,6 @@ func encodeMetadata(md map[string]string) []string { if b, err := json.Marshal(map[string]string{ k: v, }); err == nil { - // old encoding - // TODO: remove in 09/2016 - tags = append(tags, "t="+string(b)) // new encoding tags = append(tags, "t-"+encode(b)) } @@ -113,26 +90,14 @@ func encodeMetadata(md map[string]string) []string { func decodeMetadata(tags []string) map[string]string { md := make(map[string]string) - var ver byte - for _, tag := range tags { if len(tag) == 0 || tag[0] != 't' { continue } - // check version - if ver > 0 && tag[1] != ver { - continue - } - var kv map[string]string var buf []byte - // Old encoding was plain - if tag[1] == '=' { - buf = []byte(tag[2:]) - } - // New encoding is hex if tag[1] == '-' { buf = decode(tag[2:]) @@ -144,21 +109,12 @@ func decodeMetadata(tags []string) map[string]string { md[k] = v } } - - // set version - ver = tag[1] } return md } func encodeVersion(v string) []string { - return []string{ - // old encoding, - // TODO: remove in 09/2016 - "v=" + v, - // new encoding, - "v-" + encode([]byte(v)), - } + return []string{"v-" + encode([]byte(v))} } func decodeVersion(tags []string) (string, bool) { @@ -167,11 +123,6 @@ func decodeVersion(tags []string) (string, bool) { continue } - // Old encoding was plain - if tag[1] == '=' { - return tag[2:], true - } - // New encoding is hex if tag[1] == '-' { return string(decode(tag[2:])), true From 2708f9c04b10156e033f34b44f894e090f7e7dc1 Mon Sep 17 00:00:00 2001 From: Asim Date: Sun, 22 May 2016 18:41:06 +0100 Subject: [PATCH 02/17] No longer need to test old encoding --- registry/encoding_test.go | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/registry/encoding_test.go b/registry/encoding_test.go index a58bb143..ba62111e 100644 --- a/registry/encoding_test.go +++ b/registry/encoding_test.go @@ -57,8 +57,8 @@ func TestEncodingEndpoints(t *testing.T) { e := encodeEndpoints([]*Endpoint{ep}) // check there are two tags; old and new - if len(e) != 2 { - t.Fatalf("Expected 2 encoded tags, got %v", e) + if len(e) != 1 { + t.Fatalf("Expected 1 encoded tags, got %v", e) } // check old encoding @@ -104,30 +104,24 @@ func TestEncodingEndpoints(t *testing.T) { // HEX encoded hencoded := encode(jencoded) // endpoint tag - jepTag := "e=" + string(jencoded) hepTag := "e-" + hencoded - - // test old - testEp(ep, jepTag) - // test new testEp(ep, hepTag) } } func TestEncodingVersion(t *testing.T) { testData := []struct { - decoded string - encoded string - oldEncoded string + decoded string + encoded string }{ - {"1.0.0", "v-789c32d433d03300040000ffff02ce00ee", "v=1.0.0"}, - {"latest", "v-789cca492c492d2e01040000ffff08cc028e", "v=latest"}, + {"1.0.0", "v-789c32d433d03300040000ffff02ce00ee"}, + {"latest", "v-789cca492c492d2e01040000ffff08cc028e"}, } for _, data := range testData { e := encodeVersion(data.decoded) - if e[1] != data.encoded { + if e[0] != data.encoded { t.Fatalf("Expected %s got %s", data.encoded, e) } @@ -148,14 +142,5 @@ func TestEncodingVersion(t *testing.T) { if d != data.decoded { t.Fatalf("Expected %s got %s", data.decoded, d) } - - d, ok = decodeVersion([]string{data.oldEncoded}) - if !ok { - t.Fatalf("Unexpected %t for %s", ok, data.oldEncoded) - } - - if d != data.decoded { - t.Fatalf("Expected %s got %s", data.decoded, d) - } } } From 04bc20798dc6456e3393851d5edba863fe629f95 Mon Sep 17 00:00:00 2001 From: Asim Date: Sun, 22 May 2016 18:43:47 +0100 Subject: [PATCH 03/17] Don't strip decode because its good to be backwards compatible still --- registry/encoding.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/registry/encoding.go b/registry/encoding.go index 12143035..b5bb9d6c 100644 --- a/registry/encoding.go +++ b/registry/encoding.go @@ -54,14 +54,27 @@ func encodeEndpoints(en []*Endpoint) []string { func decodeEndpoints(tags []string) []*Endpoint { var en []*Endpoint + // use the first format you find + var ver byte + for _, tag := range tags { if len(tag) == 0 || tag[0] != 'e' { continue } + // check version + if ver > 0 && tag[1] != ver { + continue + } + var e *Endpoint var buf []byte + // Old encoding was plain + if tag[1] == '=' { + buf = []byte(tag[2:]) + } + // New encoding is hex if tag[1] == '-' { buf = decode(tag[2:]) @@ -70,6 +83,9 @@ func decodeEndpoints(tags []string) []*Endpoint { if err := json.Unmarshal(buf, &e); err == nil { en = append(en, e) } + + // set version + ver = tag[1] } return en } @@ -90,14 +106,26 @@ func encodeMetadata(md map[string]string) []string { func decodeMetadata(tags []string) map[string]string { md := make(map[string]string) + var ver byte + for _, tag := range tags { if len(tag) == 0 || tag[0] != 't' { continue } + // check version + if ver > 0 && tag[1] != ver { + continue + } + var kv map[string]string var buf []byte + // Old encoding was plain + if tag[1] == '=' { + buf = []byte(tag[2:]) + } + // New encoding is hex if tag[1] == '-' { buf = decode(tag[2:]) @@ -109,6 +137,9 @@ func decodeMetadata(tags []string) map[string]string { md[k] = v } } + + // set version + ver = tag[1] } return md } @@ -123,6 +154,11 @@ func decodeVersion(tags []string) (string, bool) { continue } + // Old encoding was plain + if tag[1] == '=' { + return tag[2:], true + } + // New encoding is hex if tag[1] == '-' { return string(decode(tag[2:])), true From 0a6d1ff0abfbc7a8a15b7c48b476420cfe58e7a5 Mon Sep 17 00:00:00 2001 From: Asim Date: Sun, 22 May 2016 19:33:41 +0100 Subject: [PATCH 04/17] Add slack badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 641809ad..40ea76a3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Go Micro [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/micro/go-micro?status.svg)](https://godoc.org/github.com/micro/go-micro) [![Travis CI](https://travis-ci.org/micro/go-micro.svg?branch=master)](https://travis-ci.org/micro/go-micro) [![Go Report Card](https://goreportcard.com/badge/micro/go-micro)](https://goreportcard.com/report/github.com/micro/go-micro) +# Go Micro [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/micro/go-micro?status.svg)](https://godoc.org/github.com/micro/go-micro) [![Travis CI](https://travis-ci.org/micro/go-micro.svg?branch=master)](https://travis-ci.org/micro/go-micro) [![Go Report Card](https://goreportcard.com/badge/micro/go-micro)](https://goreportcard.com/report/github.com/micro/go-micro) [![Slack Status](http://slack.micro.mu/badge.svg)](https://micro-services.slack.com) Go Micro is a pluggable RPC based microservice library which provides the fundamental building blocks for writing distributed applications. It is part of the [Micro](https://github.com/micro/micro) toolkit. It supports Proto-RPC and JSON-RPC as the request/response protocol out of the box and defaults to Consul for discovery. From 9d50d51c40a66365ba209e6b868822823027d489 Mon Sep 17 00:00:00 2001 From: Asim Date: Mon, 23 May 2016 14:13:03 +0100 Subject: [PATCH 05/17] Ugh slack on heroku sucks --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40ea76a3..641809ad 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Go Micro [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/micro/go-micro?status.svg)](https://godoc.org/github.com/micro/go-micro) [![Travis CI](https://travis-ci.org/micro/go-micro.svg?branch=master)](https://travis-ci.org/micro/go-micro) [![Go Report Card](https://goreportcard.com/badge/micro/go-micro)](https://goreportcard.com/report/github.com/micro/go-micro) [![Slack Status](http://slack.micro.mu/badge.svg)](https://micro-services.slack.com) +# Go Micro [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/micro/go-micro?status.svg)](https://godoc.org/github.com/micro/go-micro) [![Travis CI](https://travis-ci.org/micro/go-micro.svg?branch=master)](https://travis-ci.org/micro/go-micro) [![Go Report Card](https://goreportcard.com/badge/micro/go-micro)](https://goreportcard.com/report/github.com/micro/go-micro) Go Micro is a pluggable RPC based microservice library which provides the fundamental building blocks for writing distributed applications. It is part of the [Micro](https://github.com/micro/micro) toolkit. It supports Proto-RPC and JSON-RPC as the request/response protocol out of the box and defaults to Consul for discovery. From 1254a872860b111c905f05a565ce60bb75da6100 Mon Sep 17 00:00:00 2001 From: Asim Date: Tue, 24 May 2016 22:22:46 +0100 Subject: [PATCH 06/17] Add package comments --- broker/doc.go | 4 ++++ client/client.go | 21 --------------------- client/doc.go | 23 +++++++++++++++++++++++ cmd/doc.go | 4 ++++ codec/doc.go | 4 ++++ errors/doc.go | 4 ++++ metadata/doc.go | 4 ++++ registry/doc.go | 4 ++++ selector/doc.go | 8 ++++++++ selector/selector.go | 6 ------ server/doc.go | 31 +++++++++++++++++++++++++++++++ server/server.go | 28 ---------------------------- transport/doc.go | 4 ++++ 13 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 broker/doc.go create mode 100644 client/doc.go create mode 100644 cmd/doc.go create mode 100644 codec/doc.go create mode 100644 errors/doc.go create mode 100644 metadata/doc.go create mode 100644 registry/doc.go create mode 100644 selector/doc.go create mode 100644 server/doc.go create mode 100644 transport/doc.go diff --git a/broker/doc.go b/broker/doc.go new file mode 100644 index 00000000..f0bff3c4 --- /dev/null +++ b/broker/doc.go @@ -0,0 +1,4 @@ +/* +Package broker is an interface used for asynchronous messaging. +*/ +package broker diff --git a/client/client.go b/client/client.go index 404a9132..6f4859d2 100644 --- a/client/client.go +++ b/client/client.go @@ -1,24 +1,3 @@ -/* -Package client provides a method to make synchronous, asynchronous and -streaming requests to services. By default json and protobuf codecs are -supported. - - import "github.com/micro/go-micro/client" - - c := client.NewClient() - - req := c.NewRequest("go.micro.srv.greeter", "Greeter.Hello", &greeter.Request{ - Name: "John", - }) - - rsp := &greeter.Response{} - - if err := c.Call(context.Background(), req, rsp); err != nil { - return err - } - - fmt.Println(rsp.Msg) -*/ package client import ( diff --git a/client/doc.go b/client/doc.go new file mode 100644 index 00000000..3954b032 --- /dev/null +++ b/client/doc.go @@ -0,0 +1,23 @@ +/* +Package client is an interface for making requests. + +It provides a method to make synchronous, asynchronous and streaming requests to services. +By default json and protobuf codecs are supported. + + import "github.com/micro/go-micro/client" + + c := client.NewClient() + + req := c.NewRequest("go.micro.srv.greeter", "Greeter.Hello", &greeter.Request{ + Name: "John", + }) + + rsp := &greeter.Response{} + + if err := c.Call(context.Background(), req, rsp); err != nil { + return err + } + + fmt.Println(rsp.Msg) +*/ +package client diff --git a/cmd/doc.go b/cmd/doc.go new file mode 100644 index 00000000..1393799d --- /dev/null +++ b/cmd/doc.go @@ -0,0 +1,4 @@ +/* +Package cmd is an interface for parsing the command line. +*/ +package cmd diff --git a/codec/doc.go b/codec/doc.go new file mode 100644 index 00000000..3e1b3e17 --- /dev/null +++ b/codec/doc.go @@ -0,0 +1,4 @@ +/* +Package codec is an interface for encoding messages. +*/ +package codec diff --git a/errors/doc.go b/errors/doc.go new file mode 100644 index 00000000..618c419a --- /dev/null +++ b/errors/doc.go @@ -0,0 +1,4 @@ +/* +Package errors is an interface for defining detailed errors. +*/ +package errors diff --git a/metadata/doc.go b/metadata/doc.go new file mode 100644 index 00000000..e60c020b --- /dev/null +++ b/metadata/doc.go @@ -0,0 +1,4 @@ +/* +Package metadata is a way of defining message headers. +*/ +package metadata diff --git a/registry/doc.go b/registry/doc.go new file mode 100644 index 00000000..8edd49fd --- /dev/null +++ b/registry/doc.go @@ -0,0 +1,4 @@ +/* +Package registry is an interface for service discovery. +*/ +package registry diff --git a/selector/doc.go b/selector/doc.go new file mode 100644 index 00000000..d0ca4295 --- /dev/null +++ b/selector/doc.go @@ -0,0 +1,8 @@ +/* +Package selector is a way to load balance service nodes. + +It algorithmically filter and return nodes required by the client or any other system. +Selector's implemented by Micro build on the registry but it's of optional use. One could +provide a static Selector that has a fixed pool. +*/ +package selector diff --git a/selector/selector.go b/selector/selector.go index e7dd8996..6961040a 100644 --- a/selector/selector.go +++ b/selector/selector.go @@ -1,9 +1,3 @@ -/* -The Selector package provides a way to algorithmically filter and return -nodes required by the client or any other system. Selector's implemented -by Micro build on the registry but it's of optional use. One could -provide a static Selector that has a fixed pool. -*/ package selector import ( diff --git a/server/doc.go b/server/doc.go new file mode 100644 index 00000000..c3510d46 --- /dev/null +++ b/server/doc.go @@ -0,0 +1,31 @@ +/* +Package server is an interface for a micro server. + +It represents a server instance in go-micro which handles synchronous +requests via handlers and asynchronous requests via subscribers that +register with a broker. + +The server combines the all the packages in go-micro to create a whole unit +used for building applications including discovery, client/server communication +and pub/sub. + + import "github.com/micro/go-micro/server" + + type Greeter struct {} + + func (g *Greeter) Hello(ctx context.Context, req *greeter.Request, rsp *greeter.Response) error { + rsp.Msg = "Hello " + req.Name + return nil + } + + s := server.NewServer() + + + s.Handle( + s.NewHandler(&Greeter{}), + ) + + s.Start() + +*/ +package server diff --git a/server/server.go b/server/server.go index 95aca9e7..79e9e5de 100644 --- a/server/server.go +++ b/server/server.go @@ -1,31 +1,3 @@ -/* -Server represents a server instance in go-micro which handles synchronous -requests via handlers and asynchronous requests via subscribers that -register with a broker. - -The server combines the all the packages in go-micro to create a whole unit -used for building applications including discovery, client/server communication -and pub/sub. - - import "github.com/micro/go-micro/server" - - type Greeter struct {} - - func (g *Greeter) Hello(ctx context.Context, req *greeter.Request, rsp *greeter.Response) error { - rsp.Msg = "Hello " + req.Name - return nil - } - - s := server.NewServer() - - - s.Handle( - s.NewHandler(&Greeter{}), - ) - - s.Start() - -*/ package server import ( diff --git a/transport/doc.go b/transport/doc.go new file mode 100644 index 00000000..a8300db6 --- /dev/null +++ b/transport/doc.go @@ -0,0 +1,4 @@ +/* +Package is an interface for synchronous communication. +*/ +package transport From 8245390b5a4872330a98d1ce5e3681dfb8dd9a18 Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 26 May 2016 18:01:02 +0100 Subject: [PATCH 07/17] Support endpoint metadata --- server/handler.go | 9 +++++++++ server/rpc_handler.go | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/server/handler.go b/server/handler.go index b944efeb..d3a25d86 100644 --- a/server/handler.go +++ b/server/handler.go @@ -34,6 +34,7 @@ type Subscriber interface { type HandlerOptions struct { Internal bool + Metadata map[string]map[string]string } type SubscriberOptions struct { @@ -50,6 +51,14 @@ func InternalHandler(b bool) HandlerOption { } } +// EndpointMetadata is a Handler option that allows metadata to be added to +// individual endpoints. +func EndpointMetadata(endpoint string, metadata map[string]string) HandlerOption { + return func(o *HandlerOptions) { + o.Metadata[endpoint] = metadata + } +} + // Internal Subscriber options specifies that a subscriber is not advertised // to the discovery system. func InternalSubscriber(b bool) SubscriberOption { diff --git a/server/rpc_handler.go b/server/rpc_handler.go index e01b4b25..42ec7f0f 100644 --- a/server/rpc_handler.go +++ b/server/rpc_handler.go @@ -14,7 +14,10 @@ type rpcHandler struct { } func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler { - var options HandlerOptions + options := HandlerOptions{ + Metadata: make(map[string]map[string]string), + } + for _, o := range opts { o(&options) } @@ -28,6 +31,11 @@ func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler { for m := 0; m < typ.NumMethod(); m++ { if e := extractEndpoint(typ.Method(m)); e != nil { e.Name = name + "." + e.Name + + for k, v := range options.Metadata[e.Name] { + e.Metadata[k] = v + } + endpoints = append(endpoints, e) } } From 4d24f4e30caca2d75251e767fb06dbb580f6974a Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 26 May 2016 18:26:02 +0100 Subject: [PATCH 08/17] Blacklist for longer to ensure it works --- selector/default_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selector/default_test.go b/selector/default_test.go index c735040b..7897160b 100644 --- a/selector/default_test.go +++ b/selector/default_test.go @@ -90,7 +90,7 @@ func TestBlackList(t *testing.T) { } // blacklist all of it - for i := 0; i < 9; i++ { + for i := 0; i < 20; i++ { node, err = next() if err != nil { t.Fatal(err) From ee3ae89ca805e1a33c39eaaf60623710b577fa2d Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 26 May 2016 20:25:00 +0100 Subject: [PATCH 09/17] naming nitpick --- server/handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/handler.go b/server/handler.go index d3a25d86..79543b8b 100644 --- a/server/handler.go +++ b/server/handler.go @@ -53,9 +53,9 @@ func InternalHandler(b bool) HandlerOption { // EndpointMetadata is a Handler option that allows metadata to be added to // individual endpoints. -func EndpointMetadata(endpoint string, metadata map[string]string) HandlerOption { +func EndpointMetadata(name string, md map[string]string) HandlerOption { return func(o *HandlerOptions) { - o.Metadata[endpoint] = metadata + o.Metadata[name] = md } } From 7a5a9013ef4a34135603c56e0a53f30dc35afe03 Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 26 May 2016 20:43:05 +0100 Subject: [PATCH 10/17] move the option --- server/handler.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server/handler.go b/server/handler.go index 79543b8b..5e81e5c3 100644 --- a/server/handler.go +++ b/server/handler.go @@ -42,6 +42,14 @@ type SubscriberOptions struct { Internal bool } +// EndpointMetadata is a Handler option that allows metadata to be added to +// individual endpoints. +func EndpointMetadata(name string, md map[string]string) HandlerOption { + return func(o *HandlerOptions) { + o.Metadata[name] = md + } +} + // Internal Handler options specifies that a handler is not advertised // to the discovery system. In the future this may also limit request // to the internal network or authorised user. @@ -51,14 +59,6 @@ func InternalHandler(b bool) HandlerOption { } } -// EndpointMetadata is a Handler option that allows metadata to be added to -// individual endpoints. -func EndpointMetadata(name string, md map[string]string) HandlerOption { - return func(o *HandlerOptions) { - o.Metadata[name] = md - } -} - // Internal Subscriber options specifies that a subscriber is not advertised // to the discovery system. func InternalSubscriber(b bool) SubscriberOption { From 6de507b7b57f157ca4ebafc767d277b28db9cb77 Mon Sep 17 00:00:00 2001 From: Mr Talbot Date: Sat, 28 May 2016 20:52:59 +0100 Subject: [PATCH 11/17] MockClient.CallRemote now returns nil on success CallRemote was not returning nil on success as it should due to a missing return --- client/mock/mock.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/mock/mock.go b/client/mock/mock.go index 1cc22968..337df041 100644 --- a/client/mock/mock.go +++ b/client/mock/mock.go @@ -123,6 +123,8 @@ func (m *MockClient) CallRemote(ctx context.Context, addr string, req client.Req } v.Set(reflect.ValueOf(r.Response)) + + return nil } return fmt.Errorf("rpc: can't find service %s", req.Method()) From 93c1ae6221dae5837cefd12537e88e5551a957c4 Mon Sep 17 00:00:00 2001 From: Asim Date: Sat, 28 May 2016 22:30:47 +0100 Subject: [PATCH 12/17] Add debug stats handler --- server/debug.go | 7 ---- server/debug/debug.go | 28 +++++++++++++-- server/debug/proto/debug.pb.go | 63 ++++++++++++++++++++++++++++------ server/debug/proto/debug.proto | 18 ++++++++++ 4 files changed, 97 insertions(+), 19 deletions(-) diff --git a/server/debug.go b/server/debug.go index 3aaf5717..18225cf7 100644 --- a/server/debug.go +++ b/server/debug.go @@ -2,9 +2,6 @@ package server import ( "github.com/micro/go-micro/server/debug" - proto "github.com/micro/go-micro/server/debug/proto" - - "golang.org/x/net/context" ) // We use this to wrap any debug handlers so we preserve the signature Debug.{Method} @@ -12,10 +9,6 @@ type Debug struct { debug.DebugHandler } -func (d *Debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error { - return d.DebugHandler.Health(ctx, req, rsp) -} - func registerDebugHandler(s Server) { s.Handle(s.NewHandler(&Debug{s.Options().DebugHandler}, InternalHandler(true))) } diff --git a/server/debug/debug.go b/server/debug/debug.go index accd0bc4..2712a001 100644 --- a/server/debug/debug.go +++ b/server/debug/debug.go @@ -1,6 +1,9 @@ package debug import ( + "runtime" + "time" + proto "github.com/micro/go-micro/server/debug/proto" "golang.org/x/net/context" @@ -12,16 +15,37 @@ import ( // and /varz type DebugHandler interface { Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error + Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.StatsResponse) error } // Our own internal handler -type debug struct{} +type debug struct { + started int64 +} var ( - DefaultDebugHandler DebugHandler = new(debug) + DefaultDebugHandler DebugHandler = newDebug() ) +func newDebug() *debug { + return &debug{ + started: time.Now().Unix(), + } +} + func (d *debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error { rsp.Status = "ok" return nil } + +func (d *debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.StatsResponse) error { + var mstat runtime.MemStats + runtime.ReadMemStats(&mstat) + + rsp.Started = uint64(d.started) + rsp.Uptime = uint64(time.Now().Unix() - d.started) + rsp.Memory = mstat.Alloc + rsp.Gc = mstat.PauseTotalNs + rsp.Threads = uint64(runtime.NumGoroutine()) + return nil +} diff --git a/server/debug/proto/debug.pb.go b/server/debug/proto/debug.pb.go index 923ff277..7cfa7d5e 100644 --- a/server/debug/proto/debug.pb.go +++ b/server/debug/proto/debug.pb.go @@ -1,16 +1,18 @@ // Code generated by protoc-gen-go. -// source: go-micro/server/debug/proto/debug.proto +// source: github.com/micro/go-micro/server/debug/proto/debug.proto // DO NOT EDIT! /* Package debug is a generated protocol buffer package. It is generated from these files: - go-micro/server/debug/proto/debug.proto + github.com/micro/go-micro/server/debug/proto/debug.proto It has these top-level messages: HealthRequest HealthResponse + StatsRequest + StatsResponse */ package debug @@ -23,6 +25,12 @@ var _ = proto.Marshal var _ = fmt.Errorf var _ = math.Inf +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + type HealthRequest struct { } @@ -32,6 +40,7 @@ func (*HealthRequest) ProtoMessage() {} func (*HealthRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } type HealthResponse struct { + // default: ok Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"` } @@ -40,18 +49,52 @@ func (m *HealthResponse) String() string { return proto.CompactTextSt func (*HealthResponse) ProtoMessage() {} func (*HealthResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } +type StatsRequest struct { +} + +func (m *StatsRequest) Reset() { *m = StatsRequest{} } +func (m *StatsRequest) String() string { return proto.CompactTextString(m) } +func (*StatsRequest) ProtoMessage() {} +func (*StatsRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } + +type StatsResponse struct { + // unix timestamp + Started uint64 `protobuf:"varint,1,opt,name=started" json:"started,omitempty"` + // in seconds + Uptime uint64 `protobuf:"varint,2,opt,name=uptime" json:"uptime,omitempty"` + // in bytes + Memory uint64 `protobuf:"varint,3,opt,name=memory" json:"memory,omitempty"` + // num threads + Threads uint64 `protobuf:"varint,4,opt,name=threads" json:"threads,omitempty"` + // in nanoseconds + Gc uint64 `protobuf:"varint,5,opt,name=gc" json:"gc,omitempty"` +} + +func (m *StatsResponse) Reset() { *m = StatsResponse{} } +func (m *StatsResponse) String() string { return proto.CompactTextString(m) } +func (*StatsResponse) ProtoMessage() {} +func (*StatsResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } + func init() { proto.RegisterType((*HealthRequest)(nil), "HealthRequest") proto.RegisterType((*HealthResponse)(nil), "HealthResponse") + proto.RegisterType((*StatsRequest)(nil), "StatsRequest") + proto.RegisterType((*StatsResponse)(nil), "StatsResponse") } var fileDescriptor0 = []byte{ - // 107 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x4f, 0xcf, 0xd7, 0xcd, - 0xcd, 0x4c, 0x2e, 0xca, 0xd7, 0x2f, 0x4e, 0x2d, 0x2a, 0x4b, 0x2d, 0xd2, 0x4f, 0x49, 0x4d, 0x2a, - 0x4d, 0xd7, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0x87, 0xb0, 0xf5, 0xc0, 0x6c, 0x25, 0x7e, 0x2e, 0x5e, - 0x8f, 0xd4, 0xc4, 0x9c, 0x92, 0x8c, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x25, 0x05, 0x2e, - 0x3e, 0x98, 0x40, 0x71, 0x41, 0x7e, 0x5e, 0x71, 0xaa, 0x10, 0x1f, 0x17, 0x5b, 0x71, 0x49, 0x62, - 0x49, 0x69, 0xb1, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x12, 0x1b, 0x58, 0xa7, 0x31, 0x20, 0x00, - 0x00, 0xff, 0xff, 0x1c, 0xef, 0x98, 0xac, 0x64, 0x00, 0x00, 0x00, + // 201 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x34, 0x8f, 0xbd, 0x6e, 0x83, 0x30, + 0x14, 0x85, 0x05, 0xa5, 0x54, 0xbd, 0x2a, 0x54, 0x62, 0xa8, 0x3c, 0x56, 0x4c, 0x2c, 0xc5, 0x43, + 0x97, 0x3e, 0x42, 0x67, 0xf2, 0x04, 0xfc, 0x5c, 0x19, 0xa4, 0x38, 0x26, 0xbe, 0xd7, 0x91, 0x32, + 0xe7, 0xc5, 0x03, 0xb6, 0xd9, 0xce, 0xf7, 0xd9, 0xe7, 0x48, 0x17, 0xfe, 0xd4, 0xc2, 0xb3, 0x1b, + 0xda, 0xd1, 0x68, 0xa9, 0x97, 0xd1, 0x1a, 0xa9, 0xcc, 0x4f, 0x08, 0x84, 0xf6, 0x86, 0x56, 0x4e, + 0x38, 0x38, 0x25, 0x57, 0x6b, 0xd8, 0x84, 0xdc, 0xfa, 0x5c, 0x7f, 0x42, 0xf1, 0x8f, 0xfd, 0x99, + 0xe7, 0x0e, 0xaf, 0x0e, 0x89, 0xeb, 0x06, 0xca, 0x43, 0xd0, 0x6a, 0x2e, 0x84, 0xd5, 0x17, 0xe4, + 0xc4, 0x3d, 0x3b, 0x12, 0xc9, 0x77, 0xd2, 0xbc, 0x77, 0x91, 0xea, 0x12, 0x3e, 0x4e, 0x5b, 0xa2, + 0xa3, 0xf9, 0x48, 0xa0, 0x88, 0x22, 0x36, 0x05, 0xbc, 0x6d, 0x7f, 0x2d, 0xe3, 0xe4, 0xab, 0x59, + 0x77, 0xe0, 0xbe, 0xe9, 0x56, 0x5e, 0x34, 0x8a, 0xd4, 0x3f, 0x44, 0xda, 0xbd, 0x46, 0x6d, 0xec, + 0x5d, 0xbc, 0x04, 0x1f, 0x68, 0x5f, 0xe2, 0xd9, 0x62, 0x3f, 0x91, 0xc8, 0xc2, 0x52, 0xc4, 0xaa, + 0x84, 0x54, 0x8d, 0xe2, 0xd5, 0xcb, 0x2d, 0x0d, 0xb9, 0xbf, 0xeb, 0xf7, 0x19, 0x00, 0x00, 0xff, + 0xff, 0xc6, 0x75, 0x51, 0x35, 0x13, 0x01, 0x00, 0x00, } diff --git a/server/debug/proto/debug.proto b/server/debug/proto/debug.proto index 345eae2e..8d96192e 100644 --- a/server/debug/proto/debug.proto +++ b/server/debug/proto/debug.proto @@ -6,11 +6,29 @@ syntax = "proto3"; // // service Debug { // rpc Health(HealthRequest) returns (HealthResponse) {} +// rpc Stats(StatsRequest) returns (StatsResponse) {} // } message HealthRequest { } message HealthResponse { + // default: ok string status = 1; } + +message StatsRequest { +} + +message StatsResponse { + // unix timestamp + uint64 started = 1; + // in seconds + uint64 uptime = 2; + // in bytes + uint64 memory = 3; + // num threads + uint64 threads = 4; + // total gc in nanoseconds + uint64 gc = 5; +} From 702072f177a4dad9de63e78c1f1433d12511159c Mon Sep 17 00:00:00 2001 From: Asim Date: Wed, 1 Jun 2016 15:22:27 +0100 Subject: [PATCH 13/17] Make mock listen on random port if 0 is specified --- transport/mock/mock.go | 22 ++++++++++++++++++---- transport/mock/mock_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/transport/mock/mock.go b/transport/mock/mock.go index 9e11577d..5c0d3258 100644 --- a/transport/mock/mock.go +++ b/transport/mock/mock.go @@ -2,7 +2,11 @@ package mock import ( "errors" + "fmt" + "math/rand" + "strings" "sync" + "time" "github.com/micro/go-micro/transport" ) @@ -126,15 +130,25 @@ func (m *mockTransport) Listen(addr string, opts ...transport.ListenOption) (tra m.Lock() defer m.Unlock() - if _, ok := m.listeners[addr]; ok { - return nil, errors.New("already listening on " + addr) - } - var options transport.ListenOptions for _, o := range opts { o(&options) } + parts := strings.Split(addr, ":") + + // if zero port then randomly assign one + if len(parts) > 1 && parts[len(parts)-1] == "0" { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + i := r.Intn(10000) + // set addr with port + addr = fmt.Sprintf("%s:%d", parts[:len(parts)-1], 10000+i) + } + + if _, ok := m.listeners[addr]; ok { + return nil, errors.New("already listening on " + addr) + } + listener := &mockListener{ opts: options, addr: addr, diff --git a/transport/mock/mock_test.go b/transport/mock/mock_test.go index b39d56c9..193d575a 100644 --- a/transport/mock/mock_test.go +++ b/transport/mock/mock_test.go @@ -58,3 +58,32 @@ func TestTransport(t *testing.T) { } } + +func TestListener(t *testing.T) { + tr := NewTransport() + + // bind / listen on random port + l, err := tr.Listen(":0") + if err != nil { + t.Fatalf("Unexpected error listening %v", err) + } + defer l.Close() + + // try again + l2, err := tr.Listen(":0") + if err != nil { + t.Fatalf("Unexpected error listening %v", err) + } + defer l2.Close() + + // now make sure it still fails + l3, err := tr.Listen(":8080") + if err != nil { + t.Fatalf("Unexpected error listening %v", err) + } + defer l3.Close() + + if _, err := tr.Listen(":8080"); err == nil { + t.Fatal("Expected error binding to :8080 got nil") + } +} From 34365812d8d61965159e261391d3ebd5fdfee5e7 Mon Sep 17 00:00:00 2001 From: Asim Date: Fri, 3 Jun 2016 11:08:58 +0100 Subject: [PATCH 14/17] Fix double close --- transport/mock/mock.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/transport/mock/mock.go b/transport/mock/mock.go index 5c0d3258..d544d96b 100644 --- a/transport/mock/mock.go +++ b/transport/mock/mock.go @@ -63,7 +63,12 @@ func (ms *mockSocket) Send(m *transport.Message) error { } func (ms *mockSocket) Close() error { - close(ms.exit) + select { + case <-ms.exit: + return nil + default: + close(ms.exit) + } return nil } @@ -72,7 +77,12 @@ func (m *mockListener) Addr() string { } func (m *mockListener) Close() error { - close(m.exit) + select { + case <-m.exit: + return nil + default: + close(m.exit) + } return nil } From eb0422cee1eb08d768aa9606ed532982cc40865f Mon Sep 17 00:00:00 2001 From: Zeal Date: Sun, 5 Jun 2016 23:13:29 +0800 Subject: [PATCH 15/17] improve self-signed host list --- transport/http_transport.go | 49 ++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/transport/http_transport.go b/transport/http_transport.go index 0ad00ea1..9fc35401 100644 --- a/transport/http_transport.go +++ b/transport/http_transport.go @@ -409,7 +409,16 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err fn := func(addr string) (net.Listener, error) { if config == nil { - cert, err := mls.Certificate(addr) + hosts := []string{addr} + if h, _, e := net.SplitHostPort(addr); e == nil { + if h == "" { + hosts = getIPAddrList() + } else { + hosts = []string{h} + } + } + + cert, err := mls.Certificate(hosts...) if err != nil { return nil, err } @@ -447,3 +456,41 @@ func newHTTPTransport(opts ...Option) *httpTransport { } return &httpTransport{opts: options} } + +func getIPAddrList() []string { + ifaces, err := net.Interfaces() + + if err != nil { + return nil + } + + var ipAddrlist []string + + for _, i := range ifaces { + if addrs, err := i.Addrs(); err != nil { + continue + } else { + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + + if ip == nil { + continue + } + + ip = ip.To4() + if ip == nil { + continue + } + + ipAddrlist = append(ipAddrlist, ip.String()) + } + } + } + return ipAddrlist +} From d8ecd428682f8d9d8e7a9b672397b70b8a3f8992 Mon Sep 17 00:00:00 2001 From: Zeal Date: Mon, 6 Jun 2016 20:56:53 +0800 Subject: [PATCH 16/17] rename getIPAddrList() to getIPAddrs and improve code format --- transport/http_transport.go | 52 ++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/transport/http_transport.go b/transport/http_transport.go index 9fc35401..8a4814cf 100644 --- a/transport/http_transport.go +++ b/transport/http_transport.go @@ -411,8 +411,8 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err if config == nil { hosts := []string{addr} if h, _, e := net.SplitHostPort(addr); e == nil { - if h == "" { - hosts = getIPAddrList() + if len(h) == 0 { + hosts = getIPAddrs() } else { hosts = []string{h} } @@ -457,40 +457,40 @@ func newHTTPTransport(opts ...Option) *httpTransport { return &httpTransport{opts: options} } -func getIPAddrList() []string { +func getIPAddrs() []string { ifaces, err := net.Interfaces() - if err != nil { return nil } - var ipAddrlist []string + var ipAddrs []string for _, i := range ifaces { - if addrs, err := i.Addrs(); err != nil { + addrs, err := i.Addrs() + if err != nil { continue - } else { - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } + } - if ip == nil { - continue - } - - ip = ip.To4() - if ip == nil { - continue - } - - ipAddrlist = append(ipAddrlist, ip.String()) + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP } + + if ip == nil { + continue + } + + ip = ip.To4() + if ip == nil { + continue + } + + ipAddrs = append(ipAddrs, ip.String()) } } - return ipAddrlist + return ipAddrs } From 69aa8537d3ea541bdf226679ab882a0a69b0745e Mon Sep 17 00:00:00 2001 From: Asim Date: Mon, 6 Jun 2016 14:05:02 +0100 Subject: [PATCH 17/17] Add some comments --- transport/http_transport.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/transport/http_transport.go b/transport/http_transport.go index 8a4814cf..8e2e3d8a 100644 --- a/transport/http_transport.go +++ b/transport/http_transport.go @@ -410,14 +410,17 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err fn := func(addr string) (net.Listener, error) { if config == nil { hosts := []string{addr} - if h, _, e := net.SplitHostPort(addr); e == nil { - if len(h) == 0 { + + // check if its a valid host:port + if host, _, err := net.SplitHostPort(addr); err == nil { + if len(host) == 0 { hosts = getIPAddrs() } else { - hosts = []string{h} + hosts = []string{host} } } + // generate a certificate cert, err := mls.Certificate(hosts...) if err != nil { return nil, err @@ -449,14 +452,6 @@ func (h *httpTransport) String() string { return "http" } -func newHTTPTransport(opts ...Option) *httpTransport { - var options Options - for _, o := range opts { - o(&options) - } - return &httpTransport{opts: options} -} - func getIPAddrs() []string { ifaces, err := net.Interfaces() if err != nil { @@ -492,5 +487,14 @@ func getIPAddrs() []string { ipAddrs = append(ipAddrs, ip.String()) } } + return ipAddrs } + +func newHTTPTransport(opts ...Option) *httpTransport { + var options Options + for _, o := range opts { + o(&options) + } + return &httpTransport{opts: options} +}