diff options
| author | Chris Lu <chris.lu@gmail.com> | 2019-05-06 13:56:08 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2019-05-06 13:56:08 -0700 |
| commit | cf58fc0e6355e858a66d6456cfebb5e336fde81f (patch) | |
| tree | 2c4ac00c7f3d0baa9ce55e58a31e429cfbb0fbc2 /weed/util/throttler.go | |
| parent | 4e42e7b5e7cb72600c4f283b076c5184dd4c9386 (diff) | |
| download | seaweedfs-cf58fc0e6355e858a66d6456cfebb5e336fde81f.tar.xz seaweedfs-cf58fc0e6355e858a66d6456cfebb5e336fde81f.zip | |
refactor: extract out the write throttler
Diffstat (limited to 'weed/util/throttler.go')
| -rw-r--r-- | weed/util/throttler.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/weed/util/throttler.go b/weed/util/throttler.go new file mode 100644 index 000000000..873161e37 --- /dev/null +++ b/weed/util/throttler.go @@ -0,0 +1,34 @@ +package util + +import "time" + +type WriteThrottler struct { + compactionBytePerSecond int64 + lastSizeCounter int64 + lastSizeCheckTime time.Time +} + +func NewWriteThrottler(bytesPerSecond int64) *WriteThrottler { + return &WriteThrottler{ + compactionBytePerSecond: bytesPerSecond, + lastSizeCheckTime: time.Now(), + } +} + +func (wt *WriteThrottler) MaybeSlowdown(delta int64) { + if wt.compactionBytePerSecond > 0 { + wt.lastSizeCounter += delta + now := time.Now() + elapsedDuration := now.Sub(wt.lastSizeCheckTime) + if elapsedDuration > 100*time.Millisecond { + overLimitBytes := wt.lastSizeCounter - wt.compactionBytePerSecond/10 + if overLimitBytes > 0 { + overRatio := float64(overLimitBytes) / float64(wt.compactionBytePerSecond) + sleepTime := time.Duration(overRatio*1000) * time.Millisecond + // glog.V(0).Infof("currently %d bytes, limit to %d bytes, over by %d bytes, sleeping %v over %.4f", wt.lastSizeCounter, wt.compactionBytePerSecond/10, overLimitBytes, sleepTime, overRatio) + time.Sleep(sleepTime) + } + wt.lastSizeCounter, wt.lastSizeCheckTime = 0, time.Now() + } + } +} |
