refactor: better representation of 3-valued target state
This commit is contained in:
1 parent
520c7286c4
commit
b8b3e6f9cd
2 files changed
+17
-12
No files matched your search
+15
-10
@@ -26,10 +26,17 @@ type ConnectionMaker struct {
|
|||||||
actionChan chan<- ConnectionMakerAction
|
actionChan chan<- ConnectionMakerAction
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TargetState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
TargetWaiting TargetState = iota // we are waiting to connect there
|
||||||
|
TargetAttempting // we are attempting to connect there
|
||||||
|
TargetConnected // we are connected to there
|
||||||
|
)
|
||||||
|
|
||||||
// Information about an address where we may find a peer
|
// Information about an address where we may find a peer
|
||||||
type Target struct {
|
type Target struct {
|
||||||
attempting bool // are we currently attempting to connect there?
|
state TargetState
|
||||||
connected bool // are we currently connected to there?
|
|
||||||
lastError error // reason for disconnection last time
|
lastError error // reason for disconnection last time
|
||||||
tryAfter time.Time // next time to try this address
|
tryAfter time.Time // next time to try this address
|
||||||
tryInterval time.Duration // retry delay on next failure
|
tryInterval time.Duration // retry delay on next failure
|
||||||
@@ -95,7 +102,7 @@ func (cm *ConnectionMaker) ForgetConnections(peers []string) {
|
|||||||
func (cm *ConnectionMaker) ConnectionAborted(address string, err error) {
|
func (cm *ConnectionMaker) ConnectionAborted(address string, err error) {
|
||||||
cm.actionChan <- func() bool {
|
cm.actionChan <- func() bool {
|
||||||
target := cm.targets[address]
|
target := cm.targets[address]
|
||||||
target.attempting = false
|
target.state = TargetWaiting
|
||||||
target.lastError = err
|
target.lastError = err
|
||||||
target.retry()
|
target.retry()
|
||||||
return true
|
return true
|
||||||
@@ -107,8 +114,7 @@ func (cm *ConnectionMaker) ConnectionCreated(conn Connection) {
|
|||||||
cm.connections[conn] = void
|
cm.connections[conn] = void
|
||||||
if conn.Outbound() {
|
if conn.Outbound() {
|
||||||
target := cm.targets[conn.RemoteTCPAddr()]
|
target := cm.targets[conn.RemoteTCPAddr()]
|
||||||
target.attempting = false
|
target.state = TargetConnected
|
||||||
target.connected = true
|
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -119,8 +125,7 @@ func (cm *ConnectionMaker) ConnectionTerminated(conn Connection, err error) {
|
|||||||
delete(cm.connections, conn)
|
delete(cm.connections, conn)
|
||||||
if conn.Outbound() {
|
if conn.Outbound() {
|
||||||
target := cm.targets[conn.RemoteTCPAddr()]
|
target := cm.targets[conn.RemoteTCPAddr()]
|
||||||
target.attempting = false
|
target.state = TargetWaiting
|
||||||
target.connected = false
|
|
||||||
target.lastError = err
|
target.lastError = err
|
||||||
switch {
|
switch {
|
||||||
case err == ErrConnectToSelf:
|
case err == ErrConnectToSelf:
|
||||||
@@ -169,7 +174,7 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
|
|||||||
if _, found := cm.targets[address]; found {
|
if _, found := cm.targets[address]; found {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
target := &Target{}
|
target := &Target{state: TargetWaiting}
|
||||||
target.tryNow()
|
target.tryNow()
|
||||||
cm.targets[address] = target
|
cm.targets[address] = target
|
||||||
}
|
}
|
||||||
@@ -256,7 +261,7 @@ func (cm *ConnectionMaker) connectToTargets(validTarget map[string]struct{}, dir
|
|||||||
now := time.Now() // make sure we catch items just added
|
now := time.Now() // make sure we catch items just added
|
||||||
after := MaxDuration
|
after := MaxDuration
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
if target.attempting || target.connected {
|
if target.state != TargetWaiting {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, valid := validTarget[address]; !valid {
|
if _, valid := validTarget[address]; !valid {
|
||||||
@@ -268,7 +273,7 @@ func (cm *ConnectionMaker) connectToTargets(validTarget map[string]struct{}, dir
|
|||||||
}
|
}
|
||||||
switch duration := target.tryAfter.Sub(now); {
|
switch duration := target.tryAfter.Sub(now); {
|
||||||
case duration <= 0:
|
case duration <= 0:
|
||||||
target.attempting = true
|
target.state = TargetAttempting
|
||||||
_, isCmdLineTarget := directTarget[address]
|
_, isCmdLineTarget := directTarget[address]
|
||||||
go cm.attemptConnection(address, isCmdLineTarget)
|
go cm.attemptConnection(address, isCmdLineTarget)
|
||||||
case duration < after:
|
case duration < after:
|
||||||
|
|||||||
+2
-2
@@ -175,12 +175,12 @@ func NewLocalConnectionStatusSlice(cm *ConnectionMaker) []LocalConnectionStatus
|
|||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
var state, info string
|
var state, info string
|
||||||
switch {
|
switch {
|
||||||
case target.connected:
|
case target.state == TargetConnected:
|
||||||
continue
|
continue
|
||||||
case target.lastError == nil:
|
case target.lastError == nil:
|
||||||
state = "connecting"
|
state = "connecting"
|
||||||
info = ""
|
info = ""
|
||||||
case target.attempting:
|
case target.state == TargetAttempting:
|
||||||
state = "retrying"
|
state = "retrying"
|
||||||
info = target.lastError.Error()
|
info = target.lastError.Error()
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in new issue
Block a user