Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve user specified flush delay automatically by statistics #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,35 +281,65 @@ func (p *pipe) _background() {
atomic.StoreInt32(&p.state, 4)
}

type flush struct {
works time.Duration
waits time.Duration
bytes int
}

const flushesKeep = 16
const flushesMask = flushesKeep - 1

func (p *pipe) _backgroundWrite() (err error) {
var (
ones = make([]cmds.Completed, 1)
multi []cmds.Completed
ch chan RedisResult

flushDelay = p.maxFlushDelay
flushStart = time.Time{}
flushes [flushesKeep]flush
flushId = 0
bytes = 1 // avoid zero
works = time.Duration(0)
waits = time.Duration(0)

userDelay = p.maxFlushDelay
)

var ts1 = time.Now()
for atomic.LoadInt32(&p.state) < 3 {
if ones[0], multi, ch = p.queue.NextWriteCmd(); ch == nil {
if flushDelay != 0 {
flushStart = time.Now()
}
if p.w.Buffered() == 0 {
buf := p.w.Buffered()
if buf == 0 {
err = p.Error()
} else if userDelay == 0 {
err = p.w.Flush()
} else {
ts2 := time.Now()
err = p.w.Flush()
dur, gap := time.Since(ts2), ts2.Sub(ts1)
bytes = bytes + buf - flushes[flushId].bytes
works = works + dur - flushes[flushId].works
waits = waits + gap - flushes[flushId].waits
flushes[flushId] = flush{bytes: buf, works: dur, waits: gap}
flushId = (flushId + 1) & flushesMask
ts1 = ts2
}

if err == nil {
if atomic.LoadInt32(&p.state) == 1 {
ones[0], multi, ch = p.queue.WaitForWrite()
} else {
runtime.Gosched()
continue
}
if flushDelay != 0 && atomic.LoadInt32(&p.waits) > 1 { // do not delay for sequential usage
time.Sleep(flushDelay - time.Since(flushStart)) // ref: https://github.com/rueian/rueidis/issues/156
if userDelay != 0 && atomic.LoadInt32(&p.waits) > 1 { // do not delay for sequential usage
byteWork := works / time.Duration(bytes)
byteWait := waits / time.Duration(bytes)
delay := byteWait * (works / flushesKeep) / (byteWait + byteWork + 1) // avoid zero
if delay > userDelay {
delay = userDelay
}
time.Sleep(delay) // ref: https://github.com/rueian/rueidis/issues/156
}
}
}
Expand Down