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

coalesce writes #54

Merged
merged 8 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
128 changes: 97 additions & 31 deletions multiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Contributor Author

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.

)

// +1 for initiator
Expand All @@ -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

Expand All @@ -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
}
Expand Down Expand Up @@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we return here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, that's probably a worthwhile optimization.

Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just combine this with handleOutgoing and avoid storing the timer on the instance (makes it clear that it's local only).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we'd have to explicitly pass it to writeMsg and mutate in there, which makes it a little messy.
It seemed nicer to just have the timer in the instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion was to ditch writeMsg and combine everything. But we can do that later.

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
}
Expand Down
4 changes: 2 additions & 2 deletions multiplex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSlowReader(t *testing.T) {

// 100 is large enough that the buffer of the underlying connection will
// fill up.
for i := 0; i < 100; i++ {
for i := 0; i < 10000; i++ {
_, err = sa.Write(mes)
if err != nil {
break
Expand Down Expand Up @@ -346,7 +346,7 @@ func TestReset(t *testing.T) {
t.Fatalf("successfully wrote to reset stream")
}

time.Sleep(10 * time.Millisecond)
time.Sleep(200 * time.Millisecond)

n, err = sb.Write([]byte("test"))
if n != 0 {
Expand Down