Skip to content

Commit

Permalink
fix RT may be 0 (#26)
Browse files Browse the repository at this point in the history
* fix RT may be 0


Co-authored-by: chenzhihui <chenzhihui@bilibili.com>
  • Loading branch information
tonybase and chenzhihui committed Jun 21, 2022
1 parent ba04480 commit 028e85a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PKG_DIR?=''

LINTER := bin/golangci-lint
$(LINTER):
curl -L https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.42.0
curl -SL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s latest

.PHONY: test
test:
Expand All @@ -21,4 +21,4 @@ lint: $(LINTER)

.PHONY: build
build:
${TOOLS_SH} build ${PKG_DIR}
${TOOLS_SH} build ${PKG_DIR}
12 changes: 11 additions & 1 deletion ratelimit/bbr/bbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ func cpuproc() {
for range ticker.C {
stat := &cpu.Stat{}
cpu.ReadStat(stat)
stat.Usage = min(stat.Usage, 1000)
prevCPU := atomic.LoadInt64(&gCPU)
curCPU := int64(float64(prevCPU)*decay + float64(stat.Usage)*(1.0-decay))
atomic.StoreInt64(&gCPU, curCPU)
}
}

func min(l, r uint64) uint64 {
if l < r {
return l
}
return r
}

// Stat contains the metrics snapshot of bbr.
type Stat struct {
CPU int64
Expand Down Expand Up @@ -288,8 +296,10 @@ func (l *BBR) Allow() (ratelimit.DoneFunc, error) {
}
atomic.AddInt64(&l.inFlight, 1)
start := time.Now().UnixNano()
ms := float64(time.Millisecond)
return func(ratelimit.DoneInfo) {
rt := (time.Now().UnixNano() - start) / int64(time.Millisecond)
//nolint
rt := int64(math.Ceil(float64(time.Now().UnixNano()-start)) / ms)
l.rtStat.Add(rt)
atomic.AddInt64(&l.inFlight, -1)
l.passStat.Add(1)
Expand Down

0 comments on commit 028e85a

Please sign in to comment.