Skip to content

Commit

Permalink
Merge pull request #16 from libp2p/fix/defer-keepalive
Browse files Browse the repository at this point in the history
fix: don't keepalive when the connection is busy
  • Loading branch information
Stebalien committed Mar 13, 2020
2 parents ffc6806 + 922d36f commit 51522d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
os:
- linux

language: go

go:
- 1.14.x

env:
global:
- GOTFLAGS="-race"
- BUILD_DEPTYPE=gomod


# disable travis install
install:
- true

script:
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)


cache:
directories:
- $GOPATH/pkg/mod
- $HOME/.cache/go-build

notifications:
email: false
14 changes: 12 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ func (s *Session) Ping() (time.Duration, error) {

// Wait for a response
start := time.Now()
timer := time.NewTimer(s.config.ConnectionWriteTimeout)
defer timer.Stop()
select {
case <-ch:
case <-time.After(s.config.ConnectionWriteTimeout):
case <-timer.C:
s.pingLock.Lock()
delete(s.pings, id) // Ignore it if a response comes later.
s.pingLock.Unlock()
Expand All @@ -316,7 +318,7 @@ func (s *Session) Ping() (time.Duration, error) {
}

// Compute the RTT
return time.Now().Sub(start), nil
return time.Since(start), nil
}

// startKeepalive starts the keepalive process.
Expand Down Expand Up @@ -506,6 +508,14 @@ func (s *Session) recvLoop() error {
return err
}

// Reset the keepalive timer every time we receive data.
// There's no reason to keepalive if we're active. Worse, if the
// peer is busy sending us stuff, the pong might get stuck
// behind a bunch of data.
if s.keepaliveTimer != nil {
s.keepaliveTimer.Reset(s.config.KeepAliveInterval)
}

// Verify the version
if hdr.Version() != protoVersion {
s.logger.Printf("[ERR] yamux: Invalid protocol version: %d", hdr.Version())
Expand Down

0 comments on commit 51522d4

Please sign in to comment.