mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-21 05:43:09 -06:00
Fetcher timeouts
This commit is contained in:
parent
d4e30f2838
commit
094ef9bd6a
|
|
@ -83,6 +83,7 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
origRequests[k] = v
|
origRequests[k] = v
|
||||||
}
|
}
|
||||||
now := gomatrixserverlib.AsTimestamp(time.Now())
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
||||||
|
|
||||||
// First, check if any of these key checks are for our own keys. If
|
// First, check if any of these key checks are for our own keys. If
|
||||||
// they are then we will satisfy them directly.
|
// they are then we will satisfy them directly.
|
||||||
for req := range requests {
|
for req := range requests {
|
||||||
|
|
@ -91,12 +92,14 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
// keys. Remove it from the request list so we don't hit the
|
// keys. Remove it from the request list so we don't hit the
|
||||||
// database or the fetchers for it.
|
// database or the fetchers for it.
|
||||||
delete(requests, req)
|
delete(requests, req)
|
||||||
|
|
||||||
// Look up our own keys.
|
// Look up our own keys.
|
||||||
request := &api.QueryLocalKeysRequest{}
|
request := &api.QueryLocalKeysRequest{}
|
||||||
response := &api.QueryLocalKeysResponse{}
|
response := &api.QueryLocalKeysResponse{}
|
||||||
if err := s.QueryLocalKeys(ctx, request, response); err != nil {
|
if err := s.QueryLocalKeys(ctx, request, response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Depending on whether the key is expired or not, we'll need
|
// Depending on whether the key is expired or not, we'll need
|
||||||
// to write slightly different
|
// to write slightly different
|
||||||
if verifyKeys, ok := response.ServerKeys.VerifyKeys[req.KeyID]; ok {
|
if verifyKeys, ok := response.ServerKeys.VerifyKeys[req.KeyID]; ok {
|
||||||
|
|
@ -134,9 +137,21 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
// For any key requests that we still have outstanding, next try to
|
// For any key requests that we still have outstanding, next try to
|
||||||
// fetch them directly. We'll go through each of the key fetchers to
|
// fetch them directly. We'll go through each of the key fetchers to
|
||||||
// ask for the remaining keys.
|
// ask for the remaining keys.
|
||||||
|
var fetcherCtx context.Context
|
||||||
|
var fetcherCancel context.CancelFunc
|
||||||
|
defer func() {
|
||||||
|
if fetcherCancel != nil {
|
||||||
|
fetcherCancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, fetcher := range s.OurKeyRing.KeyFetchers {
|
for _, fetcher := range s.OurKeyRing.KeyFetchers {
|
||||||
fetcherCtx, fetcherCancel := context.WithTimeout(ctx, time.Second*10)
|
// If there's a context active from a previous fetcher then cancel
|
||||||
defer fetcherCancel()
|
// it. Set up a new context that lmits how long we will wait.
|
||||||
|
if fetcherCancel != nil {
|
||||||
|
fetcherCancel()
|
||||||
|
}
|
||||||
|
fetcherCtx, fetcherCancel = context.WithTimeout(ctx, time.Second*20)
|
||||||
|
|
||||||
// If there are no more keys to look up then stop.
|
// If there are no more keys to look up then stop.
|
||||||
if len(requests) == 0 {
|
if len(requests) == 0 {
|
||||||
break
|
break
|
||||||
|
|
@ -144,11 +159,13 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"fetcher_name": fetcher.FetcherName(),
|
"fetcher_name": fetcher.FetcherName(),
|
||||||
}).Infof("Fetching %d key(s)", len(requests))
|
}).Infof("Fetching %d key(s)", len(requests))
|
||||||
|
|
||||||
if fetcherResults, err := fetcher.FetchKeys(fetcherCtx, requests); err == nil {
|
if fetcherResults, err := fetcher.FetchKeys(fetcherCtx, requests); err == nil {
|
||||||
// Build a map of the results that we want to commit to the
|
// Build a map of the results that we want to commit to the
|
||||||
// database. We do this in a separate map because otherwise we
|
// database. We do this in a separate map because otherwise we
|
||||||
// might end up trying to rewrite database entries.
|
// might end up trying to rewrite database entries.
|
||||||
storeResults := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
storeResults := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
||||||
|
|
||||||
// Now let's look at the results that we got from this fetcher.
|
// Now let's look at the results that we got from this fetcher.
|
||||||
for req, res := range fetcherResults {
|
for req, res := range fetcherResults {
|
||||||
if prev, ok := results[req]; ok {
|
if prev, ok := results[req]; ok {
|
||||||
|
|
@ -172,6 +189,7 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
// Update the results map with this new result. If nothing
|
// Update the results map with this new result. If nothing
|
||||||
// else, we can try verifying against this key.
|
// else, we can try verifying against this key.
|
||||||
results[req] = res
|
results[req] = res
|
||||||
|
|
||||||
// If the key is valid right now then we can remove it from the
|
// If the key is valid right now then we can remove it from the
|
||||||
// request list as we won't need to re-fetch it.
|
// request list as we won't need to re-fetch it.
|
||||||
if res.WasValidAt(now, true) {
|
if res.WasValidAt(now, true) {
|
||||||
|
|
@ -186,15 +204,20 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
}).Errorf("Failed to store keys in the database")
|
}).Errorf("Failed to store keys in the database")
|
||||||
return nil, fmt.Errorf("server key API failed to store retrieved keys: %w", err)
|
return nil, fmt.Errorf("server key API failed to store retrieved keys: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debugging output.
|
||||||
|
if len(storeResults) > 0 {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"fetcher_name": fetcher.FetcherName(),
|
"fetcher_name": fetcher.FetcherName(),
|
||||||
}).Infof("Updated %d of %d key(s) in database", len(storeResults), len(results))
|
}).Infof("Updated %d of %d key(s) in database", len(storeResults), len(results))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.WithError(err).WithFields(logrus.Fields{
|
logrus.WithError(err).WithFields(logrus.Fields{
|
||||||
"fetcher_name": fetcher.FetcherName(),
|
"fetcher_name": fetcher.FetcherName(),
|
||||||
}).Errorf("Failed to retrieve %d key(s)", len(requests))
|
}).Errorf("Failed to retrieve %d key(s)", len(requests))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that we've actually satisfied all of the key requests that we
|
// Check that we've actually satisfied all of the key requests that we
|
||||||
// were given. We should report an error if we didn't.
|
// were given. We should report an error if we didn't.
|
||||||
for req := range origRequests {
|
for req := range origRequests {
|
||||||
|
|
@ -209,8 +232,8 @@ func (s *ServerKeyAPI) FetchKeys(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the keys.
|
// Return the keys.
|
||||||
logrus.Infof("Retrieved %d key(s) for %d request(s)", len(results), len(origRequests))
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue