@@ -22,7 +22,19 @@ import (
2222 "go.uber.org/zap"
2323)
2424
25- type NamespaceManager struct {
25+ type NamespaceManager interface {
26+ Init (logger * zap.Logger , nscs []* config.Namespace , tpFetcher observer.TopologyFetcher ,
27+ promFetcher metricsreader.PromInfoFetcher , httpCli * http.Client , cfgMgr * mconfig.ConfigManager ,
28+ metricsReader metricsreader.MetricsReader ) error
29+ CommitNamespaces (nss []* config.Namespace , nssDelete []bool ) error
30+ GetNamespace (nm string ) (* Namespace , bool )
31+ GetNamespaceByUser (user string ) (* Namespace , bool )
32+ RedirectConnections () []error
33+ Ready () bool
34+ Close () error
35+ }
36+
37+ type namespaceManager struct {
2638 sync.RWMutex
2739 nsm map [string ]* Namespace
2840 tpFetcher observer.TopologyFetcher
@@ -33,17 +45,17 @@ type NamespaceManager struct {
3345 cfgMgr * mconfig.ConfigManager
3446}
3547
36- func NewNamespaceManager () * NamespaceManager {
37- return & NamespaceManager {}
48+ func NewNamespaceManager () * namespaceManager {
49+ return & namespaceManager {}
3850}
3951
40- func (mgr * NamespaceManager ) buildNamespace (cfg * config.Namespace ) (* Namespace , error ) {
52+ func (mgr * namespaceManager ) buildNamespace (cfg * config.Namespace ) (* Namespace , error ) {
4153 logger := mgr .logger .With (zap .String ("namespace" , cfg .Namespace ))
4254
4355 // init BackendFetcher
4456 var fetcher observer.BackendFetcher
4557 healthCheckCfg := config .NewDefaultHealthCheckConfig ()
46- if ! reflect .ValueOf (mgr .tpFetcher ).IsNil () {
58+ if mgr . tpFetcher != nil && ! reflect .ValueOf (mgr .tpFetcher ).IsNil () {
4759 fetcher = observer .NewPDFetcher (mgr .tpFetcher , logger .Named ("be_fetcher" ), healthCheckCfg )
4860 } else {
4961 fetcher = observer .NewStaticFetcher (cfg .Backend .Instances )
@@ -65,7 +77,7 @@ func (mgr *NamespaceManager) buildNamespace(cfg *config.Namespace) (*Namespace,
6577 }, nil
6678}
6779
68- func (mgr * NamespaceManager ) CommitNamespaces (nss []* config.Namespace , nss_delete []bool ) error {
80+ func (mgr * namespaceManager ) CommitNamespaces (nss []* config.Namespace , nssDelete []bool ) error {
6981 nsm := make (map [string ]* Namespace )
7082 mgr .RLock ()
7183 for k , v := range mgr .nsm {
@@ -74,7 +86,7 @@ func (mgr *NamespaceManager) CommitNamespaces(nss []*config.Namespace, nss_delet
7486 mgr .RUnlock ()
7587
7688 for i , nsc := range nss {
77- if nss_delete != nil && nss_delete [i ] {
89+ if nssDelete != nil && nssDelete [i ] {
7890 delete (nsm , nsc .Namespace )
7991 continue
8092 }
@@ -92,7 +104,7 @@ func (mgr *NamespaceManager) CommitNamespaces(nss []*config.Namespace, nss_delet
92104 return nil
93105}
94106
95- func (mgr * NamespaceManager ) Init (logger * zap.Logger , nscs []* config.Namespace , tpFetcher observer.TopologyFetcher ,
107+ func (mgr * namespaceManager ) Init (logger * zap.Logger , nscs []* config.Namespace , tpFetcher observer.TopologyFetcher ,
96108 promFetcher metricsreader.PromInfoFetcher , httpCli * http.Client , cfgMgr * mconfig.ConfigManager ,
97109 metricsReader metricsreader.MetricsReader ) error {
98110 mgr .Lock ()
@@ -106,15 +118,15 @@ func (mgr *NamespaceManager) Init(logger *zap.Logger, nscs []*config.Namespace,
106118 return mgr .CommitNamespaces (nscs , nil )
107119}
108120
109- func (mgr * NamespaceManager ) GetNamespace (nm string ) (* Namespace , bool ) {
121+ func (mgr * namespaceManager ) GetNamespace (nm string ) (* Namespace , bool ) {
110122 mgr .RLock ()
111123 defer mgr .RUnlock ()
112124
113125 ns , ok := mgr .nsm [nm ]
114126 return ns , ok
115127}
116128
117- func (mgr * NamespaceManager ) GetNamespaceByUser (user string ) (* Namespace , bool ) {
129+ func (mgr * namespaceManager ) GetNamespaceByUser (user string ) (* Namespace , bool ) {
118130 mgr .RLock ()
119131 defer mgr .RUnlock ()
120132
@@ -126,7 +138,7 @@ func (mgr *NamespaceManager) GetNamespaceByUser(user string) (*Namespace, bool)
126138 return nil , false
127139}
128140
129- func (mgr * NamespaceManager ) RedirectConnections () []error {
141+ func (mgr * namespaceManager ) RedirectConnections () []error {
130142 mgr .RLock ()
131143 defer mgr .RUnlock ()
132144
@@ -140,7 +152,21 @@ func (mgr *NamespaceManager) RedirectConnections() []error {
140152 return errs
141153}
142154
143- func (mgr * NamespaceManager ) Close () error {
155+ func (mgr * namespaceManager ) Ready () bool {
156+ mgr .RLock ()
157+ defer mgr .RUnlock ()
158+ if len (mgr .nsm ) == 0 {
159+ return false
160+ }
161+ for _ , ns := range mgr .nsm {
162+ if ns .GetRouter ().HealthyBackendCount () <= 0 {
163+ return false
164+ }
165+ }
166+ return true
167+ }
168+
169+ func (mgr * namespaceManager ) Close () error {
144170 mgr .RLock ()
145171 for _ , ns := range mgr .nsm {
146172 ns .Close ()
0 commit comments