Further reduce the amount of calls to AfterFunc

This commit is contained in:
Andy Wang 2020-12-06 00:16:29 +00:00
parent 208a7f249f
commit 0327d0ffb3
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
2 changed files with 34 additions and 24 deletions

View File

@ -42,17 +42,20 @@ func (d *datagramBufferedPipe) Read(target []byte) (int, error) {
return 0, io.EOF return 0, io.EOF
} }
if !d.rDeadline.IsZero() { hasRDeadline := !d.rDeadline.IsZero()
delta := time.Until(d.rDeadline) if hasRDeadline {
if delta <= 0 { if time.Until(d.rDeadline) <= 0 {
return 0, ErrTimeout return 0, ErrTimeout
} }
d.broadcastAfter(delta)
} }
if len(d.pLens) > 0 { if len(d.pLens) > 0 {
break break
} }
if hasRDeadline {
d.broadcastAfter(time.Until(d.rDeadline))
}
d.rwCond.Wait() d.rwCond.Wait()
} }
dataLen := d.pLens[0] dataLen := d.pLens[0]
@ -76,15 +79,12 @@ func (d *datagramBufferedPipe) WriteTo(w io.Writer) (n int64, err error) {
if d.closed && len(d.pLens) == 0 { if d.closed && len(d.pLens) == 0 {
return 0, io.EOF return 0, io.EOF
} }
if !d.rDeadline.IsZero() {
delta := time.Until(d.rDeadline) hasRDeadline := !d.rDeadline.IsZero()
if delta <= 0 { if hasRDeadline {
if time.Until(d.rDeadline) <= 0 {
return 0, ErrTimeout return 0, ErrTimeout
} }
if d.wtTimeout == 0 {
// if there hasn't been a scheduled broadcast
d.broadcastAfter(delta)
}
} }
if len(d.pLens) > 0 { if len(d.pLens) > 0 {
@ -98,7 +98,11 @@ func (d *datagramBufferedPipe) WriteTo(w io.Writer) (n int64, err error) {
} }
d.rwCond.Broadcast() d.rwCond.Broadcast()
} else { } else {
if d.wtTimeout != 0 { if d.wtTimeout == 0 {
if hasRDeadline {
d.broadcastAfter(time.Until(d.rDeadline))
}
} else {
d.rDeadline = time.Now().Add(d.wtTimeout) d.rDeadline = time.Now().Add(d.wtTimeout)
d.broadcastAfter(d.wtTimeout) d.broadcastAfter(d.wtTimeout)
} }

View File

@ -39,16 +39,20 @@ func (p *streamBufferedPipe) Read(target []byte) (int, error) {
if p.closed && p.buf.Len() == 0 { if p.closed && p.buf.Len() == 0 {
return 0, io.EOF return 0, io.EOF
} }
if !p.rDeadline.IsZero() {
d := time.Until(p.rDeadline) hasRDeadline := !p.rDeadline.IsZero()
if d <= 0 { if hasRDeadline {
if time.Until(p.rDeadline) <= 0 {
return 0, ErrTimeout return 0, ErrTimeout
} }
p.broadcastAfter(d)
} }
if p.buf.Len() > 0 { if p.buf.Len() > 0 {
break break
} }
if hasRDeadline {
p.broadcastAfter(time.Until(p.rDeadline))
}
p.rwCond.Wait() p.rwCond.Wait()
} }
n, err := p.buf.Read(target) n, err := p.buf.Read(target)
@ -67,15 +71,12 @@ func (p *streamBufferedPipe) WriteTo(w io.Writer) (n int64, err error) {
if p.closed && p.buf.Len() == 0 { if p.closed && p.buf.Len() == 0 {
return 0, io.EOF return 0, io.EOF
} }
if !p.rDeadline.IsZero() {
d := time.Until(p.rDeadline) hasRDeadline := !p.rDeadline.IsZero()
if d <= 0 { if hasRDeadline {
if time.Until(p.rDeadline) <= 0 {
return 0, ErrTimeout return 0, ErrTimeout
} }
if p.wtTimeout == 0 {
// if there hasn't been a scheduled broadcast
p.broadcastAfter(d)
}
} }
if p.buf.Len() > 0 { if p.buf.Len() > 0 {
written, er := p.buf.WriteTo(w) written, er := p.buf.WriteTo(w)
@ -86,10 +87,15 @@ func (p *streamBufferedPipe) WriteTo(w io.Writer) (n int64, err error) {
} }
p.rwCond.Broadcast() p.rwCond.Broadcast()
} else { } else {
if p.wtTimeout != 0 { if p.wtTimeout == 0 {
if hasRDeadline {
p.broadcastAfter(time.Until(p.rDeadline))
}
} else {
p.rDeadline = time.Now().Add(p.wtTimeout) p.rDeadline = time.Now().Add(p.wtTimeout)
p.broadcastAfter(p.wtTimeout) p.broadcastAfter(p.wtTimeout)
} }
p.rwCond.Wait() p.rwCond.Wait()
} }
} }