You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.3 KiB

7 years ago
  1. package addrbook
  2. import "time"
  3. // knownAddress tracks information about a known network address
  4. // that is used to determine how viable an address is.
  5. type knownAddress struct {
  6. Addr *NetAddress
  7. Src *NetAddress
  8. Attempts int32
  9. LastAttempt time.Time
  10. LastSuccess time.Time
  11. BucketType byte
  12. Buckets []int
  13. }
  14. func newKnownAddress(addr *NetAddress, src *NetAddress) *knownAddress {
  15. return &knownAddress{
  16. Addr: addr,
  17. Src: src,
  18. Attempts: 0,
  19. LastAttempt: time.Now(),
  20. BucketType: bucketTypeNew,
  21. Buckets: nil,
  22. }
  23. }
  24. func (ka *knownAddress) ID() ID {
  25. return ka.Addr.ID
  26. }
  27. func (ka *knownAddress) copy() *knownAddress {
  28. return &knownAddress{
  29. Addr: ka.Addr,
  30. Src: ka.Src,
  31. Attempts: ka.Attempts,
  32. LastAttempt: ka.LastAttempt,
  33. LastSuccess: ka.LastSuccess,
  34. BucketType: ka.BucketType,
  35. Buckets: ka.Buckets,
  36. }
  37. }
  38. func (ka *knownAddress) isOld() bool {
  39. return ka.BucketType == bucketTypeOld
  40. }
  41. func (ka *knownAddress) isNew() bool {
  42. return ka.BucketType == bucketTypeNew
  43. }
  44. func (ka *knownAddress) markAttempt() {
  45. now := time.Now()
  46. ka.LastAttempt = now
  47. ka.Attempts += 1
  48. }
  49. func (ka *knownAddress) markGood() {
  50. now := time.Now()
  51. ka.LastAttempt = now
  52. ka.Attempts = 0
  53. ka.LastSuccess = now
  54. }
  55. func (ka *knownAddress) addBucketRef(bucketIdx int) int {
  56. for _, bucket := range ka.Buckets {
  57. if bucket == bucketIdx {
  58. // TODO refactor to return error?
  59. // log.Warn(Fmt("Bucket already exists in ka.Buckets: %v", ka))
  60. return -1
  61. }
  62. }
  63. ka.Buckets = append(ka.Buckets, bucketIdx)
  64. return len(ka.Buckets)
  65. }
  66. func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
  67. buckets := []int{}
  68. for _, bucket := range ka.Buckets {
  69. if bucket != bucketIdx {
  70. buckets = append(buckets, bucket)
  71. }
  72. }
  73. if len(buckets) != len(ka.Buckets)-1 {
  74. // TODO refactor to return error?
  75. // log.Warn(Fmt("bucketIdx not found in ka.Buckets: %v", ka))
  76. return -1
  77. }
  78. ka.Buckets = buckets
  79. return len(ka.Buckets)
  80. }
  81. /*
  82. An address is bad if the address in question is a New address, has not been tried in the last
  83. minute, and meets one of the following criteria:
  84. 1) It claims to be from the future
  85. 2) It hasn't been seen in over a week
  86. 3) It has failed at least three times and never succeeded
  87. 4) It has failed ten times in the last week
  88. All addresses that meet these criteria are assumed to be worthless and not
  89. worth keeping hold of.
  90. XXX: so a good peer needs us to call MarkGood before the conditions above are reached!
  91. */
  92. func (ka *knownAddress) isBad() bool {
  93. // Is Old --> good
  94. if ka.BucketType == bucketTypeOld {
  95. return false
  96. }
  97. // Has been attempted in the last minute --> good
  98. if ka.LastAttempt.Before(time.Now().Add(-1 * time.Minute)) {
  99. return false
  100. }
  101. // Too old?
  102. // XXX: does this mean if we've kept a connection up for this long we'll disconnect?!
  103. // and shouldn't it be .Before ?
  104. if ka.LastAttempt.After(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
  105. return true
  106. }
  107. // Never succeeded?
  108. if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
  109. return true
  110. }
  111. // Hasn't succeeded in too long?
  112. // XXX: does this mean if we've kept a connection up for this long we'll disconnect?!
  113. if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
  114. ka.Attempts >= maxFailures {
  115. return true
  116. }
  117. return false
  118. }