代码拉取完成,页面将自动刷新
package synchro
import (
"sync"
"time"
"gitee.com/aiou-official/go-aiou/chainbase"
"gitee.com/aiou-official/go-aiou/ilog"
)
// rangeController will control the sync range.
type rangeController struct {
start int64
mutex *sync.RWMutex
head int64
cBase *chainbase.ChainBase
quitCh chan struct{}
done *sync.WaitGroup
}
func newRangeController(cBase *chainbase.ChainBase) *rangeController {
r := &rangeController{
start: 0,
mutex: new(sync.RWMutex),
head: 0,
cBase: cBase,
quitCh: make(chan struct{}),
done: new(sync.WaitGroup),
}
r.done.Add(1)
go r.controller()
return r
}
// Close will close the rangeController.
func (r *rangeController) Close() {
close(r.quitCh)
r.done.Wait()
ilog.Infof("Stopped range controller.")
}
// SyncRange return the range of synchronization required.
func (r *rangeController) SyncRange() (start int64, end int64) {
r.mutex.RLock()
defer r.mutex.RUnlock()
if r.start < 0 {
return 0, maxSyncRange - 1
}
return r.start, r.start + maxSyncRange - 1
}
func (r *rangeController) setStart(start int64) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.start = start
}
func (r *rangeController) updateStart() {
head := r.cBase.HeadBlock().Head.Number
lib := r.cBase.LIBlock().Head.Number
if head > r.head {
// Normal case
r.head = head
if lib+1 < r.head-maxSyncRange/2 {
// TODO: This affects the maximum synchronization speed.
// Sync End: head+500 is not enough. head+1000 is ok.
r.setStart(r.head - maxSyncRange/2)
} else {
r.setStart(lib + 1)
}
} else {
// When the network does not reach a consensus for a long time.
r.setStart(lib + 1)
for r.start < r.head-maxSyncRange/2 {
time.Sleep(2 * time.Second)
r.setStart(r.start + maxSyncRange/10)
}
}
}
func (r *rangeController) controller() {
for {
select {
case <-time.After(2 * time.Second):
r.updateStart()
case <-r.quitCh:
r.done.Done()
return
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。