Skip to content

Commit

Permalink
support adding/removing clients from LBClient
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Mar 10, 2022
1 parent 59f94a3 commit 624b4e0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lbclient.go
Expand Up @@ -50,6 +50,7 @@ type LBClient struct {
cs []*lbClient

once sync.Once
mu sync.Mutex
}

// DefaultLBClientTimeout is the default request timeout used by LBClient
Expand Down Expand Up @@ -91,6 +92,37 @@ func (cc *LBClient) init() {
}
}

// AddClient adds a new HostClient to the balanced clients
// returns the new total number of clients
func (cc *LBClient) AddClient(c *HostClient) int {
cc.mu.Lock()
cc.cs = append(cc.cs, &lbClient{
c: c,
healthCheck: cc.HealthCheck,
})
cc.mu.Unlock()
return len(cc.cs)
}

// RemoveClients removes clients using the provided callback
// if rc returns true, the passed client will be removed
// returns the new total number of clients
func (cc *LBClient) RemoveClients(rc func(*HostClient) bool) int {
cc.mu.Lock()
for idx, cs := range cc.cs {
// ignore non HostClient BalancingClients
hc, ok := cs.c.(*HostClient)
if !ok {
continue
}
if rc(hc) {
cc.cs = append(cc.cs[:idx], cc.cs[idx+1])
}
}
cc.mu.Unlock()
return len(cc.cs)
}

func (cc *LBClient) get() *lbClient {
cc.once.Do(cc.init)

Expand Down

0 comments on commit 624b4e0

Please sign in to comment.