From 6eccfdb9e3e6e246de568a87208d511cd5eac647 Mon Sep 17 00:00:00 2001 From: xhe Date: Thu, 22 Dec 2022 15:19:41 +0800 Subject: [PATCH] router: add ConnCount (#156) --- pkg/manager/router/router.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/manager/router/router.go b/pkg/manager/router/router.go index 2b45967a..e81c9b7d 100644 --- a/pkg/manager/router/router.go +++ b/pkg/manager/router/router.go @@ -30,9 +30,12 @@ import ( type Router interface { Route(RedirectableConn) (string, error) RedirectConnections() error + ConnCount() int Close() } +var _ Router = &ScoreBasedRouter{} + var ( ErrNoInstanceToSelect = errors.New("no instances to route") ) @@ -416,6 +419,17 @@ func (router *ScoreBasedRouter) removeBackendIfEmpty(be *list.Element) bool { return false } +func (router *ScoreBasedRouter) ConnCount() int { + router.Lock() + defer router.Unlock() + j := 0 + for be := router.backends.Front(); be != nil; be = be.Next() { + backend := be.Value.(*backendWrapper) + j += backend.connList.Len() + } + return j +} + // Close implements Router.Close interface. func (router *ScoreBasedRouter) Close() { router.Lock()