trpc.group/trpc-go/trpc-go@v1.0.3/naming/servicerouter/servicerouter.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 // Package servicerouter is service router which filters server instances. It is between service 15 // discovery and load balance. 16 package servicerouter 17 18 import ( 19 "trpc.group/trpc-go/trpc-go/naming/registry" 20 ) 21 22 // DefaultServiceRouter is the default service router which is initialized by configuration. 23 var DefaultServiceRouter ServiceRouter = &NoopServiceRouter{} 24 25 // SetDefaultServiceRouter set the default service router. 26 func SetDefaultServiceRouter(s ServiceRouter) { 27 DefaultServiceRouter = s 28 } 29 30 // ServiceRouter is the interface that defines the service router. 31 type ServiceRouter interface { 32 Filter(serviceName string, nodes []*registry.Node, opt ...Option) ([]*registry.Node, error) 33 } 34 35 var ( 36 servicerouters = make(map[string]ServiceRouter) 37 ) 38 39 // Register registers a named service router. 40 func Register(name string, s ServiceRouter) { 41 servicerouters[name] = s 42 } 43 44 // Get gets a named service router. 45 func Get(name string) ServiceRouter { 46 return servicerouters[name] 47 } 48 49 // NoopServiceRouter is the noop service router. 50 type NoopServiceRouter struct { 51 } 52 53 // Filter returns all nodes. 54 func (*NoopServiceRouter) Filter(serviceName string, nodes []*registry.Node, opt ...Option) ([]*registry.Node, error) { 55 return nodes, nil 56 } 57 58 func unregisterForTesting(name string) { 59 delete(servicerouters, name) 60 }