Error handling

This commit is contained in:
Jakob Borg 2013-12-18 18:29:15 +01:00
parent 251b109d14
commit f774b0a5dc
3 changed files with 27 additions and 8 deletions

View File

@ -30,6 +30,7 @@ package discover
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"log"
"net" "net"
"sync" "sync"
"time" "time"
@ -45,6 +46,11 @@ type Discoverer struct {
registryLock sync.RWMutex registryLock sync.RWMutex
} }
// We tolerate a certain amount of errors because we might be running in
// laptops that sleep and wake, have intermittent network connectivity, etc.
// When we hit this many errors in succession, we stop.
const maxErrors = 30
func NewDiscoverer(id string, port int) (*Discoverer, error) { func NewDiscoverer(id string, port int) (*Discoverer, error) {
local4 := &net.UDPAddr{IP: net.IP{0, 0, 0, 0}, Port: 21025} local4 := &net.UDPAddr{IP: net.IP{0, 0, 0, 0}, Port: 21025}
conn, err := net.ListenUDP("udp4", local4) conn, err := net.ListenUDP("udp4", local4)
@ -77,22 +83,31 @@ func (d *Discoverer) sendAnnouncements() {
binary.BigEndian.PutUint16(buf[6:], uint16(len(idbs))) binary.BigEndian.PutUint16(buf[6:], uint16(len(idbs)))
copy(buf[8:], idbs) copy(buf[8:], idbs)
for { var errCounter = 0
_, _, err := d.conn.WriteMsgUDP(buf, nil, remote4) var err error
for errCounter < maxErrors {
_, _, err = d.conn.WriteMsgUDP(buf, nil, remote4)
if err != nil { if err != nil {
panic(err) errCounter++
} else {
errCounter = 0
} }
time.Sleep(d.BroadcastIntv) time.Sleep(d.BroadcastIntv)
} }
log.Println("discover/write: stopping due to too many errors:", err)
} }
func (d *Discoverer) recvAnnouncements() { func (d *Discoverer) recvAnnouncements() {
var buf = make([]byte, 1024) var buf = make([]byte, 1024)
for { var errCounter = 0
var err error
for errCounter < maxErrors {
_, addr, err := d.conn.ReadFromUDP(buf) _, addr, err := d.conn.ReadFromUDP(buf)
if err != nil { if err != nil {
panic(err) time.Sleep(time.Second)
continue
} }
errCounter = 0
magic := binary.BigEndian.Uint32(buf) magic := binary.BigEndian.Uint32(buf)
if magic != 0x121025 { if magic != 0x121025 {
continue continue
@ -111,6 +126,7 @@ func (d *Discoverer) recvAnnouncements() {
d.registryLock.Unlock() d.registryLock.Unlock()
} }
} }
log.Println("discover/read: stopping due to too many errors:", err)
} }
func (d *Discoverer) Lookup(node string) (string, bool) { func (d *Discoverer) Lookup(node string) (string, bool) {

View File

@ -188,6 +188,9 @@ func (c *Connection) readerLoop() {
case messageTypeRequest: case messageTypeRequest:
c.processRequest(hdr.msgID) c.processRequest(hdr.msgID)
if c.mreader.err != nil || c.mwriter.err != nil {
c.close()
}
case messageTypeResponse: case messageTypeResponse:
data := c.mreader.readResponse() data := c.mreader.readResponse()
@ -214,6 +217,9 @@ func (c *Connection) readerLoop() {
c.mwriter.writeUint32(encodeHeader(header{0, hdr.msgID, messageTypePong})) c.mwriter.writeUint32(encodeHeader(header{0, hdr.msgID, messageTypePong}))
c.flush() c.flush()
c.wLock.Unlock() c.wLock.Unlock()
if c.mwriter.err != nil {
c.close()
}
case messageTypePong: case messageTypePong:
c.wLock.RLock() c.wLock.RLock()

View File

@ -111,9 +111,6 @@ func Walk(dir string, model *Model, followSymlinks bool) []File {
d.Close() d.Close()
for _, fi := range fis { for _, fi := range fis {
if fi.Mode()&os.ModeSymlink != 0 { if fi.Mode()&os.ModeSymlink != 0 {
if traceFile {
debugf("Following symlink %q", fi.Name())
}
err := filepath.Walk(path.Join(dir, fi.Name())+"/", fn) err := filepath.Walk(path.Join(dir, fi.Name())+"/", fn)
if err != nil { if err != nil {
warnln(err) warnln(err)