github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/selector/filter.go (about) 1 package selector 2 3 import ( 4 "github.com/volts-dev/volts/registry" 5 "github.com/volts-dev/volts/transport" 6 ) 7 8 // 过滤传输层 9 func FilterTrasport(tp transport.ITransport) Filter { 10 return func(services []*registry.Service) []*registry.Service { 11 var nodes []*registry.Node 12 for _, service := range services { 13 for _, node := range service.Nodes { 14 if node.Metadata["transport"] == tp.String() { 15 nodes = append(nodes, node) 16 } 17 } 18 service.Nodes = nodes 19 } 20 21 return services 22 } 23 } 24 25 // FilterEndpoint is an endpoint based Select Filter which will 26 // only return services with the endpoint specified. 27 func FilterEndpoint(name string) Filter { 28 return func(old []*registry.Service) []*registry.Service { 29 var services []*registry.Service 30 31 for _, service := range old { 32 for _, ep := range service.Endpoints { 33 if ep.Name == name { 34 services = append(services, service) 35 break 36 } 37 } 38 } 39 40 return services 41 } 42 } 43 44 // FilterLabel is a label based Select Filter which will 45 // only return services with the label specified. 46 func FilterLabel(key, val string) Filter { 47 return func(old []*registry.Service) []*registry.Service { 48 var services []*registry.Service 49 50 for _, service := range old { 51 serv := new(registry.Service) 52 var nodes []*registry.Node 53 54 for _, node := range service.Nodes { 55 if node.Metadata == nil { 56 continue 57 } 58 59 if node.Metadata[key] == val { 60 nodes = append(nodes, node) 61 } 62 } 63 64 // only add service if there's some nodes 65 if len(nodes) > 0 { 66 // copy 67 *serv = *service 68 serv.Nodes = nodes 69 services = append(services, serv) 70 } 71 } 72 73 return services 74 } 75 } 76 77 // FilterVersion is a version based Select Filter which will 78 // only return services with the version specified. 79 func FilterVersion(version string) Filter { 80 return func(old []*registry.Service) []*registry.Service { 81 var services []*registry.Service 82 83 for _, service := range old { 84 if service.Version == version { 85 services = append(services, service) 86 } 87 } 88 89 return services 90 } 91 }