Skip to content

Commit

Permalink
add metric for faster health check and remove unhealth server node
Browse files Browse the repository at this point in the history
  • Loading branch information
yushaolong committed Jul 10, 2021
1 parent 5dde8fb commit ab6334c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gopool
import (
"context"
"fmt"
"github.com/alwaysthanks/melon"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -31,7 +32,7 @@ func NewPoolCluster(addrList []string, cfg Config, factory IConnFactory) *PoolCl
cancel: cancel,
poolList: make([]*Pool, 0),
checkInterval: func() {
time.Sleep(time.Second * 3)
time.Sleep(time.Second * 2)
},
}
for _, addr := range addrList {
Expand All @@ -51,6 +52,7 @@ func NewPoolCluster(addrList []string, cfg Config, factory IConnFactory) *PoolCl
idleList: make([]*Conn, 0, cfg.MaxIdleNum),
reqBlocks: make(map[int64]chan *Conn),
available: true,
melon: melon.New(5000, melon.OptionAnchor(20, 18), melon.OptionAnchor(100, 70)),
}
pc.poolList = append(pc.poolList, p)
}
Expand Down
13 changes: 13 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gopool
import (
"context"
"fmt"
"github.com/alwaysthanks/melon"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -61,6 +62,8 @@ type Pool struct {
closed bool
//pool health
available bool
//melon metric
melon *melon.Ring
}

//get connection
Expand Down Expand Up @@ -180,6 +183,8 @@ func (p *Pool) get(ctx context.Context) (conn *Conn, err error) {
return conn, nil
}
p.mutex.Unlock()
//set get conn err metric
p.melon.Feed(false)
return nil, ErrNewConn
}

Expand All @@ -192,6 +197,10 @@ func (p *Pool) put(conn *Conn, broken bool) {
conn.mutex.Unlock()
return
}

//add metric
p.melon.Feed(broken)

conn.inUse = false
conn.mutex.Unlock()
//pool closed
Expand Down Expand Up @@ -345,6 +354,10 @@ func (p *Pool) health() bool {
if p.healthFailedNum > 3 {
return false
}
//add metric check for ok
if !p.melon.OK() {
return false
}
return true
}

Expand Down

0 comments on commit ab6334c

Please sign in to comment.