diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2020-06-02 12:42:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-02 12:42:36 +0100 |
commit | 794c63e757ce59fb8f934099b6ca2b1ccc0fa31e (patch) | |
tree | 86575790e8f49e6f804385bacbc31fcc77101673 /internal | |
parent | 484b6f694c74104c641cfcb240219f287754e090 (diff) |
Reset backoff on incoming federation (#1080)
* Reset backoffs in response to incoming federation requests
* Federation wakeups no more than once per minute per origin
Diffstat (limited to 'internal')
-rw-r--r-- | internal/httpapi.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/httpapi.go b/internal/httpapi.go index 07bbacdd..bacd1768 100644 --- a/internal/httpapi.go +++ b/internal/httpapi.go @@ -1,17 +1,20 @@ package internal import ( + "context" "io" "net/http" "net/http/httptest" "net/http/httputil" "os" "strings" + "sync" "time" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + federationsenderAPI "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/httpapis" "github.com/matrix-org/gomatrixserverlib" @@ -170,6 +173,7 @@ func MakeFedAPI( metricsName string, serverName gomatrixserverlib.ServerName, keyRing gomatrixserverlib.KeyRing, + wakeup *FederationWakeups, f func(*http.Request, *gomatrixserverlib.FederationRequest) util.JSONResponse, ) http.Handler { h := func(req *http.Request) util.JSONResponse { @@ -179,11 +183,38 @@ func MakeFedAPI( if fedReq == nil { return errResp } + go wakeup.Wakeup(req.Context(), fedReq.Origin()) return f(req, fedReq) } return MakeExternalAPI(metricsName, h) } +type FederationWakeups struct { + FsAPI federationsenderAPI.FederationSenderInternalAPI + origins sync.Map +} + +func (f *FederationWakeups) Wakeup(ctx context.Context, origin gomatrixserverlib.ServerName) { + key, keyok := f.origins.Load(origin) + if keyok { + lastTime, ok := key.(time.Time) + if ok && time.Since(lastTime) < time.Minute { + return + } + } + aliveReq := federationsenderAPI.PerformServersAliveRequest{ + Servers: []gomatrixserverlib.ServerName{origin}, + } + aliveRes := federationsenderAPI.PerformServersAliveResponse{} + if err := f.FsAPI.PerformServersAlive(ctx, &aliveReq, &aliveRes); err != nil { + util.GetLogger(ctx).WithError(err).WithFields(logrus.Fields{ + "origin": origin, + }).Warn("incoming federation request failed to notify server alive") + } else { + f.origins.Store(origin, time.Now()) + } +} + // SetupHTTPAPI registers an HTTP API mux under /api and sets up a metrics // listener. func SetupHTTPAPI(servMux *http.ServeMux, publicApiMux *mux.Router, internalApiMux *mux.Router, cfg *config.Dendrite, enableHTTPAPIs bool) { |