Repository: go-kratos/kratos
Pull request #1932 (2022-04-14 04:32:20)

feat(selector): add node scheme

codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1932?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#1932](https://codecov.io/gh/go-kratos/kratos/pull/1932?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (7bb4b55) into [main](https://codecov.io/gh/go-kratos/kratos/commit/3990d91b9bd33b9804f44480ec03437e1036ef05?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (3990d91) will **decrease** coverage by `0.03%`.
> The diff coverage is `66.66%`.

```diff
@@            Coverage Diff             @@
##             main    #1932      +/-   ##
==========================================
- Coverage   80.75%   80.71%   -0.04%     
==========================================
  Files          81       81              
  Lines        3606     3609       +3     
==========================================
+ Hits         2912     2913       +1     
- Misses        487      489       +2     
  Partials      207      207              
```


| [Impacted Files](https://codecov.io/gh/go-kratos/kratos/pull/1932?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [selector/default\_node.go](https://codecov.io/gh/go-kratos/kratos/pull/1932/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-c2VsZWN0b3IvZGVmYXVsdF9ub2RlLmdv) | `91.66% <50.00%> (-8.34%)` | :arrow_down: |
| [transport/grpc/balancer.go](https://codecov.io/gh/go-kratos/kratos/pull/1932/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2dycGMvYmFsYW5jZXIuZ28=) | `92.00% <100.00%> (ø)` | |
| [transport/http/resolver.go](https://codecov.io/gh/go-kratos/kratos/pull/1932/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-dHJhbnNwb3J0L2h0dHAvcmVzb2x2ZXIuZ28=) | `64.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1932?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1932?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [3990d91...7bb4b55](https://codecov.io/gh/go-kratos/kratos/pull/1932?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/selector/default_node.go b/selector/default_node.go
index b6889981..186629e7 100644
--- a/selector/default_node.go
+++ b/selector/default_node.go
@@ -8,6 +8,7 @@ import (
 
 // DefaultNode is selector node
 type DefaultNode struct {
+	scheme   string
 	addr     string
 	weight   *int64
 	version  string
@@ -15,6 +16,11 @@ type DefaultNode struct {
 	metadata map[string]string
 }
 
+// Scheme is node scheme
+func (n *DefaultNode) Scheme() string {
+	return n.scheme
+}
+
 // Address is node address
 func (n *DefaultNode) Address() string {
 	return n.addr
@@ -41,9 +47,10 @@ func (n *DefaultNode) Metadata() map[string]string {
 }
 
 // NewNode new node
-func NewNode(addr string, ins *registry.ServiceInstance) Node {
+func NewNode(scheme, addr string, ins *registry.ServiceInstance) Node {
 	n := &DefaultNode{
-		addr: addr,
+		scheme: scheme,
+		addr:   addr,
 	}
 	if ins != nil {
 		n.name = ins.Name
diff --git a/selector/selector.go b/selector/selector.go
index ee423c9a..79ecdf78 100644
--- a/selector/selector.go
+++ b/selector/selector.go
@@ -31,6 +31,9 @@ type Builder interface {
 
 // Node is node interface.
 type Node interface {
+	// Scheme is service node scheme
+	Scheme() string
+
 	// Address is the unique address under the same service
 	Address() string
 
diff --git a/transport/grpc/balancer.go b/transport/grpc/balancer.go
index 0181792d..2a27c05a 100644
--- a/transport/grpc/balancer.go
+++ b/transport/grpc/balancer.go
@@ -58,7 +58,7 @@ func (b *Builder) Build(info base.PickerBuildInfo) gBalancer.Picker {
 	for conn, info := range info.ReadySCs {
 		ins, _ := info.Address.Attributes.Value("rawServiceInstance").(*registry.ServiceInstance)
 		nodes = append(nodes, &grpcNode{
-			Node:    selector.NewNode(info.Address.Addr, ins),
+			Node:    selector.NewNode("grpc", info.Address.Addr, ins),
 			subConn: conn,
 		})
 	}
diff --git a/transport/http/resolver.go b/transport/http/resolver.go
index a0a1e979..6a264cb3 100644
--- a/transport/http/resolver.go
+++ b/transport/http/resolver.go
@@ -122,7 +122,7 @@ func (r *resolver) update(services []*registry.ServiceInstance) bool {
 		if ept == "" {
 			continue
 		}
-		nodes = append(nodes, selector.NewNode(ept, ins))
+		nodes = append(nodes, selector.NewNode("http", ept, ins))
 	}
 	if len(nodes) == 0 {
 		r.logger.Warnf("[http resolver]Zero endpoint found,refused to write,set: %s ins: %v", r.target.Endpoint, nodes)


Test patch:
diff --git a/selector/filter/version_test.go b/selector/filter/version_test.go
index 3845ec7c..8c0895e3 100644
--- a/selector/filter/version_test.go
+++ b/selector/filter/version_test.go
@@ -13,6 +13,7 @@ func TestVersion(t *testing.T) {
 	f := Version("v2.0.0")
 	var nodes []selector.Node
 	nodes = append(nodes, selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
@@ -22,6 +23,7 @@ func TestVersion(t *testing.T) {
 		}))
 
 	nodes = append(nodes, selector.NewNode(
+		"http",
 		"127.0.0.2:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.2:9090",
diff --git a/selector/node/direct/direct_test.go b/selector/node/direct/direct_test.go
index d498c55c..5da0007a 100644
--- a/selector/node/direct/direct_test.go
+++ b/selector/node/direct/direct_test.go
@@ -13,6 +13,7 @@ import (
 func TestDirect(t *testing.T) {
 	b := &Builder{}
 	wn := b.Build(selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
@@ -42,6 +43,7 @@ func TestDirect(t *testing.T) {
 func TestDirectDefaultWeight(t *testing.T) {
 	b := &Builder{}
 	wn := b.Build(selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
diff --git a/selector/node/ewma/node_test.go b/selector/node/ewma/node_test.go
index 0da13fd4..feac09f2 100644
--- a/selector/node/ewma/node_test.go
+++ b/selector/node/ewma/node_test.go
@@ -13,6 +13,7 @@ import (
 func TestDirect(t *testing.T) {
 	b := &Builder{}
 	wn := b.Build(selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
@@ -53,6 +54,7 @@ func TestDirect(t *testing.T) {
 func TestDirectError(t *testing.T) {
 	b := &Builder{}
 	wn := b.Build(selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
@@ -89,6 +91,7 @@ func TestDirectErrorHandler(t *testing.T) {
 		},
 	}
 	wn := b.Build(selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
diff --git a/selector/p2c/p2c_test.go b/selector/p2c/p2c_test.go
index 7065d499..03d549e2 100644
--- a/selector/p2c/p2c_test.go
+++ b/selector/p2c/p2c_test.go
@@ -21,6 +21,7 @@ func TestWrr3(t *testing.T) {
 	for i := 0; i < 3; i++ {
 		addr := fmt.Sprintf("127.0.0.%d:8080", i)
 		nodes = append(nodes, selector.NewNode(
+			"http",
 			addr,
 			&registry.ServiceInstance{
 				ID:       addr,
@@ -96,6 +97,7 @@ func TestOne(t *testing.T) {
 	for i := 0; i < 1; i++ {
 		addr := fmt.Sprintf("127.0.0.%d:8080", i)
 		nodes = append(nodes, selector.NewNode(
+			"http",
 			addr,
 			&registry.ServiceInstance{
 				ID:       addr,
diff --git a/selector/random/random_test.go b/selector/random/random_test.go
index d8857411..7e69f3ee 100644
--- a/selector/random/random_test.go
+++ b/selector/random/random_test.go
@@ -13,6 +13,7 @@ func TestWrr(t *testing.T) {
 	random := New(WithFilter(filter.Version("v2.0.0")))
 	var nodes []selector.Node
 	nodes = append(nodes, selector.NewNode(
+		"http",
 		"127.0.0.1:8080",
 		&registry.ServiceInstance{
 			ID:       "127.0.0.1:8080",
@@ -20,6 +21,7 @@ func TestWrr(t *testing.T) {
 			Metadata: map[string]string{"weight": "10"},
 		}))
 	nodes = append(nodes, selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:       "127.0.0.1:9090",
diff --git a/selector/selector_test.go b/selector/selector_test.go
index 9d624f3b..25e70acd 100644
--- a/selector/selector_test.go
+++ b/selector/selector_test.go
@@ -89,6 +89,7 @@ func TestDefault(t *testing.T) {
 	selector := builder.Build()
 	var nodes []Node
 	nodes = append(nodes, NewNode(
+		"http",
 		"127.0.0.1:8080",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:8080",
@@ -98,6 +99,7 @@ func TestDefault(t *testing.T) {
 			Metadata:  map[string]string{"weight": "10"},
 		}))
 	nodes = append(nodes, NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:        "127.0.0.1:9090",
diff --git a/selector/wrr/wrr_test.go b/selector/wrr/wrr_test.go
index ed069331..e3e96ed4 100644
--- a/selector/wrr/wrr_test.go
+++ b/selector/wrr/wrr_test.go
@@ -14,6 +14,7 @@ func TestWrr(t *testing.T) {
 	wrr := New(WithFilter(filter.Version("v2.0.0")))
 	var nodes []selector.Node
 	nodes = append(nodes, selector.NewNode(
+		"http",
 		"127.0.0.1:8080",
 		&registry.ServiceInstance{
 			ID:       "127.0.0.1:8080",
@@ -21,6 +22,7 @@ func TestWrr(t *testing.T) {
 			Metadata: map[string]string{"weight": "10"},
 		}))
 	nodes = append(nodes, selector.NewNode(
+		"http",
 		"127.0.0.1:9090",
 		&registry.ServiceInstance{
 			ID:       "127.0.0.1:9090",