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

reduce the packet count #29

Merged
merged 2 commits into from
Oct 12, 2018
Merged
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
32 changes: 17 additions & 15 deletions multiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type Multiplex struct {
wrLock sync.Mutex

nstreams chan *Stream
hdrBuf []byte

channels map[streamID]*Stream
chLock sync.Mutex
Expand All @@ -70,7 +69,6 @@ func NewMultiplex(con net.Conn, initiator bool) *Multiplex {
closed: make(chan struct{}),
shutdown: make(chan struct{}),
nstreams: make(chan *Stream, 16),
hdrBuf: make([]byte, 20),
}

go mp.handleIncoming()
Expand Down Expand Up @@ -137,26 +135,30 @@ func (mp *Multiplex) sendMsg(header uint64, data []byte, dl time.Time) error {
return err
}
}
n := binary.PutUvarint(mp.hdrBuf, header)
n += binary.PutUvarint(mp.hdrBuf[n:], uint64(len(data)))
_, err := mp.con.Write(mp.hdrBuf[:n])
if err != nil {
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)

written, err := mp.con.Write(buf[:n])
if err != nil && written > 0 {
// Bail. We've written partial message and can't do anything
// about this.
mp.con.Close()
return err
}

if len(data) != 0 {
_, err = mp.con.Write(data)
if err != nil {
return err
}
}
if !dl.IsZero() {
if err := mp.con.SetWriteDeadline(time.Time{}); err != nil {
return err
// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

so we were eating the original error here? nice catch!

return err2
}
}

return nil
return err
}

func (mp *Multiplex) nextChanID() uint64 {
Expand Down