Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions util/service/breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package rocserv
import (
"context"
"sync"
"time"

"gitlab.pri.ibanyu.com/middleware/dolphin/circuit_breaker"
"gitlab.pri.ibanyu.com/middleware/seaweed/xlog"
Expand All @@ -29,70 +28,16 @@ type Breaker struct {
servName string // 形如 {servGroup}/{servName}
clientLookup ClientLookup
rwMutex sync.RWMutex

statCounter map[string]*BreakerStat
statChan chan *BreakerStat
}

func NewBreaker(clientLookup ClientLookup) *Breaker {
m := &Breaker{
clientLookup: clientLookup,
servName: clientLookup.ServKey(),
statCounter: make(map[string]*BreakerStat),
statChan: make(chan *BreakerStat, 1024*10),
}

go m.monitor()
return m
}

func (m *Breaker) monitor() {
fun := "Breaker.monitor -->"
ctx := context.Background()

ticker := time.NewTicker(60 * time.Second)
for {
select {
case <-ticker.C:
for _, stat := range m.statCounter {
if stat.fail > 5 && stat.total > 5 &&
(float64(stat.fail)/float64(stat.total)) > 0.02 {
xlog.Errorf(ctx, "%s breaker stat, key:%s, total:%d, fail:%d", fun, stat.key, stat.total, stat.fail)
}
}
m.statCounter = make(map[string]*BreakerStat)

case stat := <-m.statChan:
tmp := &BreakerStat{
key: stat.key,
}
if item, ok := m.statCounter[stat.key]; ok {
tmp = item
}

tmp.fail += stat.fail
tmp.total += stat.total
m.statCounter[tmp.key] = tmp
}
}
}

func (m *Breaker) doStat(key string, total, fail int64) {
fun := "Breaker.doStat -->"

stat := &BreakerStat{
key: key,
total: total,
fail: fail,
}

select {
case m.statChan <- stat:
default:
xlog.Errorf(context.Background(), "%s drop, key:%s, total:%d, fail:%d", fun, stat.key, stat.total, stat.fail)
}
}

func (m *Breaker) Do(ctx context.Context, funcName string, run func(ctx context.Context) error, fallback func(context.Context, error) error) error {
fun := "Breaker.Do -->"
fail := int64(0)
Expand All @@ -111,7 +56,6 @@ func (m *Breaker) Do(ctx context.Context, funcName string, run func(ctx context.
}

xlog.Debugf(ctx, "Breaker key:%s fail:%d", key, fail)
m.doStat(key, 1, fail)
return err
}

Expand Down