refactor: 'enum' -> bool
This commit is contained in:
1 parent
ce2d3ff30a
commit
4e442393b2
2 files changed
+6
-13
No files matched your search
+5
-12
@@ -20,11 +20,6 @@ const (
|
|||||||
CMFailed = iota
|
CMFailed = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
CSUnconnected ConnectionState = iota
|
|
||||||
CSAttempting = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
func StartConnectionMaker(router *Router) *ConnectionMaker {
|
func StartConnectionMaker(router *Router) *ConnectionMaker {
|
||||||
queryChan := make(chan *ConnectionMakerInteraction, ChannelSize)
|
queryChan := make(chan *ConnectionMakerInteraction, ChannelSize)
|
||||||
state := &ConnectionMaker{
|
state := &ConnectionMaker{
|
||||||
@@ -79,7 +74,7 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
query.resultChan <- cm.status()
|
query.resultChan <- cm.status()
|
||||||
case query.code == CMFailed:
|
case query.code == CMFailed:
|
||||||
if target, found := cm.targets[query.address]; found {
|
if target, found := cm.targets[query.address]; found {
|
||||||
target.state = CSUnconnected
|
target.attempting = false
|
||||||
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
||||||
maybeTick()
|
maybeTick()
|
||||||
}
|
}
|
||||||
@@ -140,13 +135,13 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections(now time.Time) {
|
|||||||
|
|
||||||
now = time.Now() // make sure we catch items just added
|
now = time.Now() // make sure we catch items just added
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
if target.state == CSUnconnected {
|
if !target.attempting {
|
||||||
if our_connected_targets[address] || !validTarget[address] {
|
if our_connected_targets[address] || !validTarget[address] {
|
||||||
//log.Println("Deleting target no longer valid:", address)
|
//log.Println("Deleting target no longer valid:", address)
|
||||||
delete(cm.targets, address)
|
delete(cm.targets, address)
|
||||||
} else if now.After(target.tryAfter) {
|
} else if now.After(target.tryAfter) {
|
||||||
|
target.attempting = true
|
||||||
target.attemptCount += 1
|
target.attemptCount += 1
|
||||||
target.state = CSAttempting
|
|
||||||
go cm.attemptConnection(address, cm.cmdLineAddress[address])
|
go cm.attemptConnection(address, cm.cmdLineAddress[address])
|
||||||
}
|
}
|
||||||
} else if our_connected_targets[address] {
|
} else if our_connected_targets[address] {
|
||||||
@@ -160,9 +155,7 @@ func (cm *ConnectionMaker) addToTargets(address string) {
|
|||||||
address = NormalisePeerAddr(address)
|
address = NormalisePeerAddr(address)
|
||||||
target := cm.targets[address]
|
target := cm.targets[address]
|
||||||
if target == nil {
|
if target == nil {
|
||||||
target = &Target{
|
target = &Target{}
|
||||||
state: CSUnconnected,
|
|
||||||
}
|
|
||||||
target.tryAfter, target.tryInterval = tryImmediately()
|
target.tryAfter, target.tryInterval = tryImmediately()
|
||||||
cm.targets[address] = target
|
cm.targets[address] = target
|
||||||
}
|
}
|
||||||
@@ -171,7 +164,7 @@ func (cm *ConnectionMaker) addToTargets(address string) {
|
|||||||
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.state == CSAttempting {
|
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 {
|
||||||
buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter))
|
buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter))
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ type ConnectionState int
|
|||||||
|
|
||||||
// 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 {
|
||||||
state ConnectionState
|
attempting bool // are we currently attempting to connect there?
|
||||||
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
|
||||||
|
|||||||
Reference in new issue
Block a user