Add env var to tune header writes delay (#464)

This commit is contained in:
Ivan Daniluk 2017-11-22 21:13:11 +02:00 committed by GitHub
parent 74d3e5e625
commit 4cd362213f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 0 deletions

View File

@ -23,6 +23,8 @@ import (
"math"
"math/big"
mrand "math/rand"
"os"
"strconv"
"time"
"github.com/ethereum/go-ethereum/common"
@ -40,6 +42,21 @@ const (
numberCacheLimit = 2048
)
var writeDelay time.Duration = 0
func init() {
str, ok := os.LookupEnv("FEATURE_SYNC_DELAY")
if !ok {
return
}
delay, err := strconv.ParseInt(str, 10, 0)
if err != nil {
panic(fmt.Errorf("FEATURE_SYNC_DELAY value is invalid:", err))
}
writeDelay = time.Duration(delay) * time.Microsecond
}
// HeaderChain implements the basic block header chain logic that is shared by
// core.BlockChain and light.LightChain. It is not usable in itself, only as
// a part of either structure.
@ -103,6 +120,10 @@ func NewHeaderChain(chainDb ethdb.Database, config *params.ChainConfig, engine c
}
hc.currentHeaderHash = hc.currentHeader.Hash()
if writeDelay > 0 {
log.Info("Header writes are slowed down", "delay", writeDelay)
}
return hc, nil
}
@ -271,6 +292,11 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCa
stats.ignored++
continue
}
// introduce delay for the expensive write to
// trade off sync time with CPU usage
time.Sleep(writeDelay)
if err := writeHeader(header); err != nil {
return i, err
}