-
Notifications
You must be signed in to change notification settings - Fork 20
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
coalesce writes #54
coalesce writes #54
Changes from 5 commits
2c1c36f
95dcec5
9c074ae
0465341
6cf6509
c0e7a8d
7a55767
03d48f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ var ErrInvalidState = errors.New("received an unexpected message from the peer") | |
var ( | ||
NewStreamTimeout = time.Minute | ||
ResetStreamTimeout = 2 * time.Minute | ||
|
||
WriteCoalesceDelay = 1 * time.Millisecond | ||
) | ||
|
||
// +1 for initiator | ||
|
@@ -59,7 +61,9 @@ type Multiplex struct { | |
shutdownErr error | ||
shutdownLock sync.Mutex | ||
|
||
wrTkn chan struct{} | ||
writeCh chan []byte | ||
writeTimer *time.Timer | ||
writeTimerFired bool | ||
|
||
nstreams chan *Stream | ||
|
||
|
@@ -70,19 +74,19 @@ type Multiplex struct { | |
// NewMultiplex creates a new multiplexer session. | ||
func NewMultiplex(con net.Conn, initiator bool) *Multiplex { | ||
mp := &Multiplex{ | ||
con: con, | ||
initiator: initiator, | ||
buf: bufio.NewReader(con), | ||
channels: make(map[streamID]*Stream), | ||
closed: make(chan struct{}), | ||
shutdown: make(chan struct{}), | ||
wrTkn: make(chan struct{}, 1), | ||
nstreams: make(chan *Stream, 16), | ||
con: con, | ||
initiator: initiator, | ||
buf: bufio.NewReader(con), | ||
channels: make(map[streamID]*Stream), | ||
closed: make(chan struct{}), | ||
shutdown: make(chan struct{}), | ||
writeCh: make(chan []byte, 16), | ||
writeTimer: time.NewTimer(0), | ||
nstreams: make(chan *Stream, 16), | ||
} | ||
|
||
go mp.handleIncoming() | ||
|
||
mp.wrTkn <- struct{}{} | ||
go mp.handleOutgoing() | ||
|
||
return mp | ||
} | ||
|
@@ -146,45 +150,107 @@ func (mp *Multiplex) IsClosed() bool { | |
|
||
func (mp *Multiplex) sendMsg(ctx context.Context, header uint64, data []byte) error { | ||
buf := pool.Get(len(data) + 20) | ||
defer pool.Put(buf) | ||
|
||
n := 0 | ||
n += binary.PutUvarint(buf[n:], header) | ||
n += binary.PutUvarint(buf[n:], uint64(len(data))) | ||
n += copy(buf[n:], data) | ||
|
||
select { | ||
case tkn := <-mp.wrTkn: | ||
defer func() { mp.wrTkn <- tkn }() | ||
case mp.writeCh <- buf[:n]: | ||
return nil | ||
case <-mp.shutdown: | ||
return ErrShutdown | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
|
||
if mp.isShutdown() { | ||
return ErrShutdown | ||
} | ||
func (mp *Multiplex) handleOutgoing() { | ||
for { | ||
select { | ||
case <-mp.shutdown: | ||
return | ||
|
||
dl, hasDl := ctx.Deadline() | ||
if hasDl { | ||
if err := mp.con.SetWriteDeadline(dl); err != nil { | ||
return err | ||
case data := <-mp.writeCh: | ||
err := mp.writeMsg(data) | ||
if err != nil { | ||
log.Warningf("Error writing data: %s", err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we return here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably should. There is an implicit return because the write failure closes the connection, which triggers the shutdown channel, but better to be explicit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
} | ||
} | ||
} | ||
} | ||
|
||
func (mp *Multiplex) writeMsg(data []byte) error { | ||
if len(data) >= 512 { | ||
return mp.doWriteMsg(data) | ||
} | ||
|
||
written, err := mp.con.Write(buf[:n]) | ||
if err != nil && (written > 0 || isFatalNetworkError(err)) { | ||
// Bail. We've written partial message or it's a fatal error and can't do anything | ||
// about this. | ||
mp.closeNoWait() | ||
return err | ||
buf := pool.Get(4096) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we hold on to a static byte slice to avoid the pool operation? This method is not called concurrently, so it should be safe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, that's probably a worthwhile optimization. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'll take 4KiB when idle which can add up pretty quickly. |
||
defer pool.Put(buf) | ||
|
||
n := copy(buf, data) | ||
pool.Put(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be simpler to just use a pool of buffered writers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hrm, maybe. I find it simpler to reason with the buffer actually. |
||
|
||
if !mp.writeTimerFired { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just combine this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we'd have to explicitly pass it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion was to ditch |
||
if !mp.writeTimer.Stop() { | ||
<-mp.writeTimer.C | ||
} | ||
} | ||
mp.writeTimer.Reset(WriteCoalesceDelay) | ||
mp.writeTimerFired = false | ||
|
||
if hasDl { | ||
// only return this error if we don't *already* have an error from the write. | ||
if err2 := mp.con.SetWriteDeadline(time.Time{}); err == nil && err2 != nil { | ||
return err2 | ||
for { | ||
select { | ||
case data = <-mp.writeCh: | ||
wr := copy(buf[n:], data) | ||
if wr < len(data) { | ||
// we filled the buffer, send it | ||
err := mp.doWriteMsg(buf) | ||
if err != nil { | ||
pool.Put(data) | ||
return err | ||
} | ||
|
||
if len(data)-wr >= 512 { | ||
// the remaining data is not a small write, send it | ||
err := mp.doWriteMsg(data[wr:]) | ||
pool.Put(data) | ||
return err | ||
} | ||
|
||
n = copy(buf, data[wr:]) | ||
|
||
// we've written some, reset the timer to coalesce the rest | ||
if !mp.writeTimer.Stop() { | ||
<-mp.writeTimer.C | ||
} | ||
mp.writeTimer.Reset(WriteCoalesceDelay) | ||
} else { | ||
n += wr | ||
} | ||
|
||
pool.Put(data) | ||
|
||
case <-mp.writeTimer.C: | ||
mp.writeTimerFired = true | ||
return mp.doWriteMsg(buf[:n]) | ||
|
||
case <-mp.shutdown: | ||
return ErrShutdown | ||
} | ||
} | ||
} | ||
|
||
func (mp *Multiplex) doWriteMsg(data []byte) error { | ||
if mp.isShutdown() { | ||
return ErrShutdown | ||
} | ||
|
||
_, err := mp.con.Write(data) | ||
if err != nil { | ||
mp.closeNoWait() | ||
} | ||
|
||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's default to 100us for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, is that the default in yamux too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah it's 10us in yamux.