bsc/les/utils/timeutils.go
Felföldi Zsolt 4996fce25a
les, les/lespay/server: refactor client pool (#21236)
* les, les/lespay/server: refactor client pool

* les: use ns.Operation and sub calls where needed

* les: fixed tests

* les: removed active/inactive logic from peerSet

* les: removed active/inactive peer logic

* les: fixed linter warnings

* les: fixed more linter errors and added missing metrics

* les: addressed comments

* cmd/geth: fixed TestPriorityClient

* les: simplified clientPool state machine

* les/lespay/server: do not use goroutine for balance callbacks

* internal/web3ext: fix addBalance required parameters

* les: removed freeCapacity, always connect at minCapacity initially

* les: only allow capacity change with priority status

Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
2020-09-14 22:44:20 +02:00

70 lines
1.8 KiB
Go

// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package utils
import (
"sync"
"time"
"github.com/ethereum/go-ethereum/common/mclock"
)
type UpdateTimer struct {
clock mclock.Clock
lock sync.Mutex
last mclock.AbsTime
threshold time.Duration
}
func NewUpdateTimer(clock mclock.Clock, threshold time.Duration) *UpdateTimer {
// We don't accept the update threshold less than 0.
if threshold < 0 {
return nil
}
// Don't panic for lazy users
if clock == nil {
clock = mclock.System{}
}
return &UpdateTimer{
clock: clock,
last: clock.Now(),
threshold: threshold,
}
}
func (t *UpdateTimer) Update(callback func(diff time.Duration) bool) bool {
return t.UpdateAt(t.clock.Now(), callback)
}
func (t *UpdateTimer) UpdateAt(at mclock.AbsTime, callback func(diff time.Duration) bool) bool {
t.lock.Lock()
defer t.lock.Unlock()
diff := time.Duration(at - t.last)
if diff < 0 {
diff = 0
}
if diff < t.threshold {
return false
}
if callback(diff) {
t.last = at
return true
}
return false
}