Remove broken Ping latency measurement

This commit is contained in:
Jakob Borg 2013-12-30 20:52:36 -05:00
parent 9560265adc
commit af3e64a5a7
3 changed files with 46 additions and 61 deletions

View File

@ -88,9 +88,7 @@ func (m *Model) printStatsLoop() {
func (m *Model) printConnectionStats() { func (m *Model) printConnectionStats() {
for node, conn := range m.nodes { for node, conn := range m.nodes {
stats := conn.Statistics() stats := conn.Statistics()
if (stats.InBytesPerSec > 0 || stats.OutBytesPerSec > 0) && stats.Latency > 0 { if stats.InBytesPerSec > 0 || stats.OutBytesPerSec > 0 {
infof("%s: %sB/s in, %sB/s out, %0.02f ms", node, toSI(stats.InBytesPerSec), toSI(stats.OutBytesPerSec), stats.Latency.Seconds()*1000)
} else if stats.InBytesPerSec > 0 || stats.OutBytesPerSec > 0 {
infof("%s: %sB/s in, %sB/s out", node, toSI(stats.InBytesPerSec), toSI(stats.OutBytesPerSec)) infof("%s: %sB/s in, %sB/s out", node, toSI(stats.InBytesPerSec), toSI(stats.OutBytesPerSec))
} }
} }

View File

@ -46,17 +46,16 @@ type Model interface {
type Connection struct { type Connection struct {
sync.RWMutex sync.RWMutex
ID string ID string
receiver Model receiver Model
reader io.Reader reader io.Reader
mreader *marshalReader mreader *marshalReader
writer io.Writer writer io.Writer
mwriter *marshalWriter mwriter *marshalWriter
closed bool closed bool
awaiting map[int]chan asyncResult awaiting map[int]chan asyncResult
nextId int nextId int
peerLatency time.Duration indexSent map[string]int64
indexSent map[string]int64
lastStatistics Statistics lastStatistics Statistics
statisticsLock sync.Mutex statisticsLock sync.Mutex
@ -132,7 +131,7 @@ func (c *Connection) Index(idx []FileInfo) {
c.nextId = (c.nextId + 1) & 0xfff c.nextId = (c.nextId + 1) & 0xfff
c.Unlock() c.Unlock()
if err != nil || c.mwriter.err != nil { if err != nil || c.mwriter.err != nil {
c.close() c.Close()
return return
} }
} }
@ -150,13 +149,13 @@ func (c *Connection) Request(name string, offset uint64, size uint32, hash []byt
c.mwriter.writeRequest(request{name, offset, size, hash}) c.mwriter.writeRequest(request{name, offset, size, hash})
if c.mwriter.err != nil { if c.mwriter.err != nil {
c.Unlock() c.Unlock()
c.close() c.Close()
return nil, c.mwriter.err return nil, c.mwriter.err
} }
err := c.flush() err := c.flush()
if err != nil { if err != nil {
c.Unlock() c.Unlock()
c.close() c.Close()
return nil, err return nil, err
} }
c.nextId = (c.nextId + 1) & 0xfff c.nextId = (c.nextId + 1) & 0xfff
@ -169,27 +168,26 @@ func (c *Connection) Request(name string, offset uint64, size uint32, hash []byt
return res.val, res.err return res.val, res.err
} }
func (c *Connection) Ping() (time.Duration, bool) { func (c *Connection) Ping() bool {
c.Lock() c.Lock()
if c.closed { if c.closed {
c.Unlock() c.Unlock()
return 0, false return false
} }
rc := make(chan asyncResult, 1) rc := make(chan asyncResult, 1)
c.awaiting[c.nextId] = rc c.awaiting[c.nextId] = rc
t0 := time.Now()
c.mwriter.writeHeader(header{0, c.nextId, messageTypePing}) c.mwriter.writeHeader(header{0, c.nextId, messageTypePing})
err := c.flush() err := c.flush()
if err != nil || c.mwriter.err != nil { if err != nil || c.mwriter.err != nil {
c.Unlock() c.Unlock()
c.close() c.Close()
return 0, false return false
} }
c.nextId = (c.nextId + 1) & 0xfff c.nextId = (c.nextId + 1) & 0xfff
c.Unlock() c.Unlock()
_, ok := <-rc res, ok := <-rc
return time.Since(t0), ok return ok && res.err == nil
} }
func (c *Connection) Stop() { func (c *Connection) Stop() {
@ -206,7 +204,7 @@ func (c *Connection) flush() error {
return nil return nil
} }
func (c *Connection) close() { func (c *Connection) Close() {
c.Lock() c.Lock()
if c.closed { if c.closed {
c.Unlock() c.Unlock()
@ -232,12 +230,12 @@ func (c *Connection) readerLoop() {
for !c.isClosed() { for !c.isClosed() {
hdr := c.mreader.readHeader() hdr := c.mreader.readHeader()
if c.mreader.err != nil { if c.mreader.err != nil {
c.close() c.Close()
break break
} }
if hdr.version != 0 { if hdr.version != 0 {
log.Printf("Protocol error: %s: unknown message version %#x", c.ID, hdr.version) log.Printf("Protocol error: %s: unknown message version %#x", c.ID, hdr.version)
c.close() c.Close()
break break
} }
@ -249,7 +247,7 @@ func (c *Connection) readerLoop() {
case messageTypeIndex: case messageTypeIndex:
files := c.mreader.readIndex() files := c.mreader.readIndex()
if c.mreader.err != nil { if c.mreader.err != nil {
c.close() c.Close()
} else { } else {
c.receiver.Index(c.ID, files) c.receiver.Index(c.ID, files)
} }
@ -257,7 +255,7 @@ func (c *Connection) readerLoop() {
case messageTypeIndexUpdate: case messageTypeIndexUpdate:
files := c.mreader.readIndex() files := c.mreader.readIndex()
if c.mreader.err != nil { if c.mreader.err != nil {
c.close() c.Close()
} else { } else {
c.receiver.IndexUpdate(c.ID, files) c.receiver.IndexUpdate(c.ID, files)
} }
@ -269,7 +267,7 @@ func (c *Connection) readerLoop() {
data := c.mreader.readResponse() data := c.mreader.readResponse()
if c.mreader.err != nil { if c.mreader.err != nil {
c.close() c.Close()
} else { } else {
c.Lock() c.Lock()
rc, ok := c.awaiting[hdr.msgID] rc, ok := c.awaiting[hdr.msgID]
@ -288,7 +286,7 @@ func (c *Connection) readerLoop() {
err := c.flush() err := c.flush()
c.Unlock() c.Unlock()
if err != nil || c.mwriter.err != nil { if err != nil || c.mwriter.err != nil {
c.close() c.Close()
} }
case messageTypePong: case messageTypePong:
@ -307,7 +305,7 @@ func (c *Connection) readerLoop() {
default: default:
log.Printf("Protocol error: %s: unknown message type %#x", c.ID, hdr.msgType) log.Printf("Protocol error: %s: unknown message type %#x", c.ID, hdr.msgType)
c.close() c.Close()
} }
} }
} }
@ -315,7 +313,7 @@ func (c *Connection) readerLoop() {
func (c *Connection) processRequest(msgID int) { func (c *Connection) processRequest(msgID int) {
req := c.mreader.readRequest() req := c.mreader.readRequest()
if c.mreader.err != nil { if c.mreader.err != nil {
c.close() c.Close()
} else { } else {
go func() { go func() {
data, _ := c.receiver.Request(c.ID, req.name, req.offset, req.size, req.hash) data, _ := c.receiver.Request(c.ID, req.name, req.offset, req.size, req.hash)
@ -326,36 +324,27 @@ func (c *Connection) processRequest(msgID int) {
c.Unlock() c.Unlock()
buffers.Put(data) buffers.Put(data)
if c.mwriter.err != nil || err != nil { if c.mwriter.err != nil || err != nil {
c.close() c.Close()
} }
}() }()
} }
} }
func (c *Connection) pingerLoop() { func (c *Connection) pingerLoop() {
var rc = make(chan time.Duration, 1) var rc = make(chan bool, 1)
for !c.isClosed() { for !c.isClosed() {
c.lastReceiveLock.RLock() time.Sleep(pingIdleTime / 2)
lr := c.lastReceive go func() {
c.lastReceiveLock.RUnlock() rc <- c.Ping()
}()
if time.Since(lr) > pingIdleTime { select {
go func() { case ok := <-rc:
t, ok := c.Ping() if !ok {
if ok { c.Close()
rc <- t
}
}()
select {
case lat := <-rc:
c.Lock()
c.peerLatency = (c.peerLatency + lat) / 2
c.Unlock()
case <-time.After(pingTimeout):
c.close()
} }
case <-time.After(pingTimeout):
c.Close()
} }
time.Sleep(time.Second)
} }
} }
@ -365,7 +354,6 @@ type Statistics struct {
InBytesPerSec int InBytesPerSec int
OutBytesTotal int OutBytesTotal int
OutBytesPerSec int OutBytesPerSec int
Latency time.Duration
} }
func (c *Connection) Statistics() Statistics { func (c *Connection) Statistics() Statistics {
@ -381,7 +369,6 @@ func (c *Connection) Statistics() Statistics {
InBytesPerSec: int(float64(rt-c.lastStatistics.InBytesTotal) / secs), InBytesPerSec: int(float64(rt-c.lastStatistics.InBytesTotal) / secs),
OutBytesTotal: wt, OutBytesTotal: wt,
OutBytesPerSec: int(float64(wt-c.lastStatistics.OutBytesTotal) / secs), OutBytesPerSec: int(float64(wt-c.lastStatistics.OutBytesTotal) / secs),
Latency: c.peerLatency,
} }
c.lastStatistics = stats c.lastStatistics = stats
return stats return stats

View File

@ -46,10 +46,10 @@ func TestPing(t *testing.T) {
c0 := NewConnection("c0", ar, bw, nil) c0 := NewConnection("c0", ar, bw, nil)
c1 := NewConnection("c1", br, aw, nil) c1 := NewConnection("c1", br, aw, nil)
if _, ok := c0.Ping(); !ok { if ok := c0.Ping(); !ok {
t.Error("c0 ping failed") t.Error("c0 ping failed")
} }
if _, ok := c1.Ping(); !ok { if ok := c1.Ping(); !ok {
t.Error("c1 ping failed") t.Error("c1 ping failed")
} }
} }
@ -70,7 +70,7 @@ func TestPingErr(t *testing.T) {
c0 := NewConnection("c0", ar, ebw, m0) c0 := NewConnection("c0", ar, ebw, m0)
NewConnection("c1", br, eaw, m1) NewConnection("c1", br, eaw, m1)
_, res := c0.Ping() res := c0.Ping()
if (i < 4 || j < 4) && res { if (i < 4 || j < 4) && res {
t.Errorf("Unexpected ping success; i=%d, j=%d", i, j) t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
} else if (i >= 8 && j >= 8) && !res { } else if (i >= 8 && j >= 8) && !res {
@ -190,7 +190,7 @@ func TestClose(t *testing.T) {
c0 := NewConnection("c0", ar, bw, m0) c0 := NewConnection("c0", ar, bw, m0)
NewConnection("c1", br, aw, m1) NewConnection("c1", br, aw, m1)
c0.close() c0.Close()
ok := c0.isClosed() ok := c0.isClosed()
if !ok { if !ok {
@ -199,7 +199,7 @@ func TestClose(t *testing.T) {
// None of these should panic, some should return an error // None of these should panic, some should return an error
_, ok = c0.Ping() ok = c0.Ping()
if ok { if ok {
t.Error("Ping should not return true") t.Error("Ping should not return true")
} }