Merge pull request #343 from rade/340_improve_connection_logging

LGTM.  Closes #340 - I tested with UDP blocked and got desired log.
This commit is contained in:
Bryan Boreham committed 2015-01-13 16:53:03 +00:00
commit ee6f92cd24
5 files changed
+27 -22

No files matched your search

+18 -17
View File
@@ -30,7 +30,7 @@ func (conn *RemoteConnection) RemoteTCPAddr() string {
return conn.remoteTCPAddr return conn.remoteTCPAddr
} }
func (conn *RemoteConnection) Shutdown() { func (conn *RemoteConnection) Shutdown(error) {
} }
func (conn *RemoteConnection) Established() bool { func (conn *RemoteConnection) Established() bool {
@@ -86,11 +86,9 @@ func (conn *LocalConnection) RemoteUDPAddr() *net.UDPAddr {
// peer actor process. Do not call this from the connection's actor // peer actor process. Do not call this from the connection's actor
// process itself. // process itself.
func (conn *LocalConnection) CheckFatal(err error) error { func (conn *LocalConnection) CheckFatal(err error) error {
if err == nil { if err != nil {
return nil conn.Shutdown(err)
} }
conn.log("error:", err)
conn.Shutdown()
return err return err
} }
@@ -130,8 +128,10 @@ const (
) )
// Async // Async
func (conn *LocalConnection) Shutdown() { func (conn *LocalConnection) Shutdown(err error) {
conn.queryChan <- &ConnectionInteraction{Interaction: Interaction{code: CShutdown}} conn.queryChan <- &ConnectionInteraction{
Interaction: Interaction{code: CShutdown},
payload: err}
} }
// Async // Async
@@ -161,10 +161,11 @@ func (conn *LocalConnection) SendTCP(msg []byte) {
func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction, acceptNewPeer bool) { func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction, acceptNewPeer bool) {
err := conn.handshake(acceptNewPeer) err := conn.handshake(acceptNewPeer)
if err != nil { if err != nil {
log.Printf("->[%s] encountered error during handshake: %v\n", conn.remoteTCPAddr, err) log.Printf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err)
conn.handleShutdown() conn.handleShutdown()
return return
} }
log.Printf("->[%s] completed handshake with %s\n", conn.remoteTCPAddr, conn.remote.Name)
conn.Router.Ourself.AddConnection(conn) conn.Router.Ourself.AddConnection(conn)
if conn.remoteUDPAddr != nil { if conn.remoteUDPAddr != nil {
if err = conn.ensureForwarders(); err == nil { if err = conn.ensureForwarders(); err == nil {
@@ -173,11 +174,7 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction,
} }
} }
terminate := false terminate := false
for !terminate { for !terminate && err == nil {
if err != nil {
conn.log("error:", err)
break
}
select { select {
case query, ok := <-queryChan: case query, ok := <-queryChan:
if !ok { if !ok {
@@ -185,6 +182,7 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction,
} }
switch query.code { switch query.code {
case CShutdown: case CShutdown:
err = query.payload.(error)
terminate = true terminate = true
case CSetEstablished: case CSetEstablished:
err = conn.handleSetEstablished() err = conn.handleSetEstablished()
@@ -203,6 +201,13 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction,
} }
} }
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && !conn.established {
conn.log("connection shutting down due to timeout; possibly caused by blocked UDP connectivity")
} else if err != nil {
conn.log("connection shutting down due to error:", err)
} else {
conn.log("connection shutting down")
}
conn.handleShutdown() conn.handleShutdown()
} }
@@ -258,10 +263,6 @@ func (conn *LocalConnection) handleSendTCP(msg []byte) error {
} }
func (conn *LocalConnection) handleShutdown() { func (conn *LocalConnection) handleShutdown() {
if conn.remote != nil {
conn.log("connection shutting down")
}
// Whilst some of these elements may have been written to whilst // Whilst some of these elements may have been written to whilst
// holding locks, they were only written to by the connection // holding locks, they were only written to by the connection
// actor process. handleShutdown is only called by the connection // actor process. handleShutdown is only called by the connection
+2 -2
View File
@@ -198,9 +198,9 @@ func (cm *ConnectionMaker) status() string {
} }
func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) { func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) {
log.Println("Attempting connection to", address) log.Printf("->[%s] attempting connection\n", address)
if err := cm.ourself.CreateConnection(address, acceptNewPeer); err != nil { if err := cm.ourself.CreateConnection(address, acceptNewPeer); err != nil {
log.Println(err) log.Printf("->[%s] error during connection attempt: %v\n", address, err)
cm.ConnectionTerminated(address) cm.ConnectionTerminated(address)
} }
} }
+3 -1
View File
@@ -210,6 +210,7 @@ func (peer *LocalPeer) handleAddConnection(conn *LocalConnection) {
peer.Lock() peer.Lock()
peer.connections[toName] = conn peer.connections[toName] = conn
peer.Unlock() peer.Unlock()
conn.log("connection added")
} }
func (peer *LocalPeer) handleDeleteConnection(conn *LocalConnection) { func (peer *LocalPeer) handleDeleteConnection(conn *LocalConnection) {
@@ -226,6 +227,7 @@ func (peer *LocalPeer) handleDeleteConnection(conn *LocalConnection) {
peer.Lock() peer.Lock()
delete(peer.connections, toName) delete(peer.connections, toName)
peer.Unlock() peer.Unlock()
conn.log("connection deleted")
broadcast := false broadcast := false
if conn.Established() { if conn.Established() {
peer.Lock() peer.Lock()
@@ -252,7 +254,7 @@ func (peer *LocalPeer) handleConnectionEstablished(conn *LocalConnection) {
peer.Lock() peer.Lock()
peer.version += 1 peer.version += 1
peer.Unlock() peer.Unlock()
log.Println("Peer", peer.Name, "established active connection to remote peer", conn.Remote().Name, "at", conn.RemoteTCPAddr()) conn.log("connection fully established")
peer.broadcastPeerUpdate(conn.Remote()) peer.broadcastPeerUpdate(conn.Remote())
} }
+3 -1
View File
@@ -156,7 +156,9 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) {
// someone else is dialing us, so our udp sender is the conn // someone else is dialing us, so our udp sender is the conn
// on Port and we wait for them to send us something on UDP to // on Port and we wait for them to send us something on UDP to
// start. // start.
connRemote := NewRemoteConnection(router.Ourself.Peer, nil, tcpConn.RemoteAddr().String()) remoteAddrStr := tcpConn.RemoteAddr().String()
log.Printf("->[%s] connection accepted\n", remoteAddrStr)
connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr)
NewLocalConnection(connRemote, true, tcpConn, nil, router) NewLocalConnection(connRemote, true, tcpConn, nil, router)
} }
+1 -1
View File
@@ -30,7 +30,7 @@ type Connection interface {
Remote() *Peer Remote() *Peer
RemoteTCPAddr() string RemoteTCPAddr() string
Established() bool Established() bool
Shutdown() Shutdown(error)
} }
type RemoteConnection struct { type RemoteConnection struct {