Keep track of which connections have been established

This commit is contained in:
Bryan Boreham committed 2014-09-08 15:36:19 +01:00
1 parent dec794eb72
commit 5cf2cb0c50
3 files changed
+28 -16

No files matched your search

+1
View File
@@ -447,6 +447,7 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder, usingPassword bool
// them and thus has informed us via TCP that it considers // them and thus has informed us via TCP that it considers
// the connection is now up. We now do a fetchAll on it. // the connection is now up. We now do a fetchAll on it.
conn.SetEstablished() conn.SetEstablished()
conn.Router.ConnectionMaker.ConnectionEstablished(conn.remoteTCPAddr)
conn.SendTCP(ProtocolFetchAllByte) conn.SendTCP(ProtocolFetchAllByte)
} else if msg[0] == ProtocolStartFragmentationTest { } else if msg[0] == ProtocolStartFragmentationTest {
conn.Forward(false, &ForwardedFrame{ conn.Forward(false, &ForwardedFrame{
+26 -15
View File
@@ -6,7 +6,6 @@ import (
"log" "log"
"math/rand" "math/rand"
"net" "net"
"strconv"
"time" "time"
) )
@@ -16,6 +15,7 @@ const (
MaxAttemptCount = 100 MaxAttemptCount = 100
CMEnsure = iota CMEnsure = iota
CMStatus = iota CMStatus = iota
CMEstablished = iota
CMConnSucceeded = iota CMConnSucceeded = iota
CMConnFailed = iota CMConnFailed = iota
) )
@@ -38,12 +38,26 @@ func (cm *ConnectionMaker) InitiateConnection(address string) {
} }
func (cm *ConnectionMaker) EnsureConnection(address string) { func (cm *ConnectionMaker) EnsureConnection(address string) {
// If we've been given a port number, take it off
if addrHost, _, err := net.SplitHostPort(address); err == nil {
address = addrHost
}
cm.queryChan <- &ConnectionMakerInteraction{ cm.queryChan <- &ConnectionMakerInteraction{
Interaction: Interaction{code: CMEnsure}, Interaction: Interaction{code: CMEnsure},
acceptAnyPeer: false, acceptAnyPeer: false,
address: address} address: address}
} }
func (cm *ConnectionMaker) ConnectionEstablished(address string) {
// If we've been given a port number, take it off
if addrHost, _, err := net.SplitHostPort(address); err == nil {
address = addrHost
}
cm.queryChan <- &ConnectionMakerInteraction{
Interaction: Interaction{code: CMEstablished},
address: address}
}
func (cm *ConnectionMaker) String() string { func (cm *ConnectionMaker) String() string {
resultChan := make(chan interface{}, 0) resultChan := make(chan interface{}, 0)
cm.queryChan <- &ConnectionMakerInteraction{ cm.queryChan <- &ConnectionMakerInteraction{
@@ -72,6 +86,12 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
maybeTick() maybeTick()
case query.code == CMStatus: case query.code == CMStatus:
query.resultChan <- cm.status() query.resultChan <- cm.status()
case query.code == CMEstablished:
target := cm.targets[query.address]
log.Println("Connection established to", query.address, target)
if target != nil {
target.established = true
}
case query.code == CMConnSucceeded: case query.code == CMConnSucceeded:
cm.targets[query.address].attempting = false cm.targets[query.address].attempting = false
case query.code == CMConnFailed: case query.code == CMConnFailed:
@@ -86,7 +106,7 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
} }
case now := <-tick: case now := <-tick:
for address, target := range cm.targets { for address, target := range cm.targets {
if target.conn == nil && !target.attempting && now.After(target.tryAfter) { if !target.established && !target.attempting && now.After(target.tryAfter) {
target.attemptCount += 1 target.attemptCount += 1
target.attempting = true; target.attempting = true;
go cm.attemptConnection(address, target.acceptAnyPeer) go cm.attemptConnection(address, target.acceptAnyPeer)
@@ -107,25 +127,16 @@ func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
tryInterval: interval, tryInterval: interval,
tryAfter: after} tryAfter: after}
} }
target.established = false // this seems a crap place to do this
// FIXME: what does it mean if an address is added twice? // FIXME: what does it mean if an address is added twice?
addrHost, addrPortStr, err := net.SplitHostPort(address) cm.targets[address] = target
if err == nil {
// ensure port-less version is there
cm.targets[addrHost] = target
if addrPort, err := strconv.Atoi(addrPortStr); err == nil && addrPort != Port {
cm.targets[address] = target
}
} else {
// can't split it, assume it must not have port on it
cm.targets[address] = target
}
} }
func (cm *ConnectionMaker) status() string { func (cm *ConnectionMaker) status() string {
var buf bytes.Buffer var buf bytes.Buffer
for address, target := range cm.targets { for address, target := range cm.targets {
if (target.conn != nil) { if (target.established) {
buf.WriteString(fmt.Sprintf("%s connected to: %v\n", address, target.conn)) buf.WriteString(fmt.Sprintf("%s connected\n", address))
} else if target.attempting { } else if target.attempting {
buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter)) buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter))
} else { } else {
+1 -1
View File
@@ -169,7 +169,7 @@ type ConnectionMaker struct {
type Target struct { type Target struct {
acceptAnyPeer bool // was this address given to us on the command line? acceptAnyPeer bool // was this address given to us on the command line?
attempting bool // are we currently attempting to connect to this address? attempting bool // are we currently attempting to connect to this address?
conn *Connection established bool // has the connection been established
tryAfter time.Time // next time to try this address tryAfter time.Time // next time to try this address
tryInterval time.Duration // backoff time on next failure tryInterval time.Duration // backoff time on next failure
attemptCount int attemptCount int