From 70535dc7516f30fbc10b6a7759e92636fbd1c6c6 Mon Sep 17 00:00:00 2001 From: baozhecheng Date: Fri, 14 Apr 2023 14:40:13 +0800 Subject: [PATCH] fix(resolver): filter out nodes with weight 0 --- transport/grpc/resolver/discovery/resolver.go | 7 +++++++ transport/http/resolver.go | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/transport/grpc/resolver/discovery/resolver.go b/transport/grpc/resolver/discovery/resolver.go index 10775d749..fdc51203d 100644 --- a/transport/grpc/resolver/discovery/resolver.go +++ b/transport/grpc/resolver/discovery/resolver.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "strconv" "time" "google.golang.org/grpc/attributes" @@ -66,6 +67,12 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) { if _, ok := endpoints[ept]; ok { continue } + // filter weight <= 0 + if w, ok := in.Metadata["weight"]; ok { + if i, err := strconv.ParseInt(w, 10, 64); err == nil && i <= 0 { + continue + } + } filtered = append(filtered, in) } if r.subsetSize != 0 { diff --git a/transport/http/resolver.go b/transport/http/resolver.go index bcbf8f3b3..f12a33bcd 100644 --- a/transport/http/resolver.go +++ b/transport/http/resolver.go @@ -4,6 +4,7 @@ import ( "context" "errors" "net/url" + "strconv" "strings" "time" @@ -130,6 +131,12 @@ func (r *resolver) update(services []*registry.ServiceInstance) bool { if ept == "" { continue } + // filter weight <= 0 + if w, ok := ins.Metadata["weight"]; ok { + if i, err := strconv.ParseInt(w, 10, 64); err == nil && i <= 0 { + continue + } + } filtered = append(filtered, ins) } if r.subsetSize != 0 {