diff --git a/federationsender/federationsender.go b/federationsender/federationsender.go index b59bc0601..17d218c23 100644 --- a/federationsender/federationsender.go +++ b/federationsender/federationsender.go @@ -21,8 +21,8 @@ import ( "github.com/matrix-org/dendrite/federationsender/internal" "github.com/matrix-org/dendrite/federationsender/inthttp" "github.com/matrix-org/dendrite/federationsender/queue" + "github.com/matrix-org/dendrite/federationsender/statistics" "github.com/matrix-org/dendrite/federationsender/storage" - "github.com/matrix-org/dendrite/federationsender/storage/statistics" "github.com/matrix-org/dendrite/internal/setup" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" diff --git a/federationsender/internal/api.go b/federationsender/internal/api.go index b58157d93..9a9880ce1 100644 --- a/federationsender/internal/api.go +++ b/federationsender/internal/api.go @@ -2,8 +2,8 @@ package internal import ( "github.com/matrix-org/dendrite/federationsender/queue" + "github.com/matrix-org/dendrite/federationsender/statistics" "github.com/matrix-org/dendrite/federationsender/storage" - "github.com/matrix-org/dendrite/federationsender/storage/statistics" "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" diff --git a/federationsender/queue/destinationqueue.go b/federationsender/queue/destinationqueue.go index 940adb05c..cd4a2778d 100644 --- a/federationsender/queue/destinationqueue.go +++ b/federationsender/queue/destinationqueue.go @@ -21,9 +21,9 @@ import ( "sync" "time" + "github.com/matrix-org/dendrite/federationsender/statistics" "github.com/matrix-org/dendrite/federationsender/storage" "github.com/matrix-org/dendrite/federationsender/storage/shared" - "github.com/matrix-org/dendrite/federationsender/storage/statistics" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" diff --git a/federationsender/queue/queue.go b/federationsender/queue/queue.go index 8fdcbaf89..5651fba26 100644 --- a/federationsender/queue/queue.go +++ b/federationsender/queue/queue.go @@ -21,8 +21,8 @@ import ( "fmt" "sync" + "github.com/matrix-org/dendrite/federationsender/statistics" "github.com/matrix-org/dendrite/federationsender/storage" - "github.com/matrix-org/dendrite/federationsender/storage/statistics" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" diff --git a/federationsender/storage/statistics/statistics.go b/federationsender/statistics/statistics.go similarity index 92% rename from federationsender/storage/statistics/statistics.go rename to federationsender/statistics/statistics.go index 5f3fca19c..8e0be5729 100644 --- a/federationsender/storage/statistics/statistics.go +++ b/federationsender/statistics/statistics.go @@ -49,6 +49,12 @@ func (s *Statistics) ForServer(serverName gomatrixserverlib.ServerName) *ServerS } s.servers[serverName] = server s.mutex.Unlock() + blacklisted, err := s.DB.IsServerBlacklisted(serverName) + if err != nil { + logrus.WithError(err).Errorf("Failed to get blacklist entry %q", serverName) + } else { + server.blacklisted.Store(blacklisted) + } } return server } @@ -60,6 +66,7 @@ func (s *Statistics) ForServer(serverName gomatrixserverlib.ServerName) *ServerS type ServerStatistics struct { statistics *Statistics // serverName gomatrixserverlib.ServerName // + blacklisted atomic.Bool // is the node blacklisted backoffUntil atomic.Value // time.Time to wait until before sending requests failCounter atomic.Uint32 // how many times have we failed? successCounter atomic.Uint32 // how many times have we succeeded? @@ -74,6 +81,8 @@ func (s *ServerStatistics) Success() { s.failCounter.Store(0) if err := s.statistics.DB.RemoveServerFromBlacklist(s.serverName); err != nil { logrus.WithError(err).Errorf("Failed to remove %q from blacklist", s.serverName) + } else { + s.blacklisted.Store(false) } } @@ -93,7 +102,8 @@ func (s *ServerStatistics) Failure() bool { // give up. if err := s.statistics.DB.AddServerToBlacklist(s.serverName); err != nil { logrus.WithError(err).Errorf("Failed to add %q to blacklist", s.serverName) - return false + } else { + s.blacklisted.Store(true) } return true } @@ -124,13 +134,7 @@ func (s *ServerStatistics) BackoffDuration() (bool, time.Duration) { // Blacklisted returns true if the server is blacklisted and false // otherwise. func (s *ServerStatistics) Blacklisted() bool { - blacklisted, err := s.statistics.DB.IsServerBlacklisted(s.serverName) - if err != nil { - logrus.WithError(err).Errorf("Failed to get blacklist entry %q", s.serverName) - // why did this happen? - return false - } - return blacklisted + return s.blacklisted.Load() } // SuccessCount returns the number of successful requests. This is