Commit 14b4c822 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

updated peerstream: fixed hanging bug

peerstream would hang when it got many temporary errors.
temp errors should not count as an error. Now, it will
only exit when an error is not temporary.

I kept the acceptErr channel because it will no longer
cause a bad hang. The goroutine is exiting, so if it
blocks until acceptErr is read, it's fine. If users
launch tons of listers and see goroutines building up,
they know they should be reading + logging those.
parent b7a4e921
......@@ -144,7 +144,7 @@
},
{
"ImportPath": "github.com/jbenet/go-peerstream",
"Rev": "55792f89d00cf62166668ded3288536cbe6a72cc"
"Rev": "cddf450fca891e45aa471d882dae2c28ac642fb4"
},
{
"ImportPath": "github.com/jbenet/go-random",
......
......@@ -2,8 +2,10 @@ package peerstream
import (
"errors"
"fmt"
"net"
"time"
tec "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-temp-err-catcher"
)
type Listener struct {
......@@ -62,36 +64,19 @@ func (l *Listener) accept() {
// catching the error here is odd. doing what net/http does:
// http://golang.org/src/net/http/server.go?s=51504:51550#L1728
var tempDelay time.Duration // how long to sleep on accept failure
isTemporaryErr := func(e error) bool {
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
time.Sleep(tempDelay)
return true
}
return false
}
// Using the lib: https://godoc.org/github.com/jbenet/go-temp-err-catcher
var catcher tec.TempErrCatcher
// loop forever accepting connections
for {
conn, err := l.netList.Accept()
if err != nil {
l.acceptErr <- err
if isTemporaryErr(err) {
if catcher.IsTemporary(err) {
continue
}
l.acceptErr <- fmt.Errorf("peerstream listener failed: %s", err)
return // ok, problems. bail.
}
tempDelay = 0
// add conn to swarm and listen for incoming streams
// log.Printf("accepted conn %s\n", conn.RemoteAddr())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment