Skip to content

Commit

Permalink
re-enable write coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed May 23, 2019
1 parent 6639721 commit 26f5e38
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,25 +403,20 @@ func (s *Session) sendLoop() error {
return nil
}

writer := s.conn
writer := pool.Writer{W: s.conn}

// FIXME: https://github.com/libp2p/go-libp2p/issues/644
// Write coalescing is disabled for now.
var writeTimeout *time.Timer
var writeTimeoutCh <-chan time.Time
if s.config.WriteCoalesceDelay > 0 {
writeTimeout = time.NewTimer(s.config.WriteCoalesceDelay)
defer writeTimeout.Stop()

//writer := pool.Writer{W: s.conn}

//var writeTimeout *time.Timer
//var writeTimeoutCh <-chan time.Time
//if s.config.WriteCoalesceDelay > 0 {
// writeTimeout = time.NewTimer(s.config.WriteCoalesceDelay)
// defer writeTimeout.Stop()

// writeTimeoutCh = writeTimeout.C
//} else {
// ch := make(chan time.Time)
// close(ch)
// writeTimeoutCh = ch
//}
writeTimeoutCh = writeTimeout.C
} else {
ch := make(chan time.Time)
close(ch)
writeTimeoutCh = ch
}

for {
// yield after processing the last message, if we've shutdown.
Expand All @@ -439,29 +434,29 @@ func (s *Session) sendLoop() error {
case buf = <-s.sendCh:
case <-s.shutdownCh:
return nil
//default:
// select {
// case buf = <-s.sendCh:
// case <-s.shutdownCh:
// return nil
// case <-writeTimeoutCh:
// if err := writer.Flush(); err != nil {
// if os.IsTimeout(err) {
// err = ErrConnectionWriteTimeout
// }
// return err
// }

// select {
// case buf = <-s.sendCh:
// case <-s.shutdownCh:
// return nil
// }

// if writeTimeout != nil {
// writeTimeout.Reset(s.config.WriteCoalesceDelay)
// }
// }
default:
select {
case buf = <-s.sendCh:
case <-s.shutdownCh:
return nil
case <-writeTimeoutCh:
if err := writer.Flush(); err != nil {
if os.IsTimeout(err) {
err = ErrConnectionWriteTimeout
}
return err
}

select {
case buf = <-s.sendCh:
case <-s.shutdownCh:
return nil
}

if writeTimeout != nil {
writeTimeout.Reset(s.config.WriteCoalesceDelay)
}
}
}

if err := extendWriteDeadline(); err != nil {
Expand Down

0 comments on commit 26f5e38

Please sign in to comment.