mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-22 11:41:38 +00:00
More key tweaks (#1116)
This commit is contained in:
parent
ec7718e7f8
commit
079d8fe8fb
@ -2,6 +2,7 @@ package caching
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
@ -23,9 +24,17 @@ func (c Caches) GetServerKey(
|
|||||||
request gomatrixserverlib.PublicKeyLookupRequest,
|
request gomatrixserverlib.PublicKeyLookupRequest,
|
||||||
) (gomatrixserverlib.PublicKeyLookupResult, bool) {
|
) (gomatrixserverlib.PublicKeyLookupResult, bool) {
|
||||||
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
|
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
|
||||||
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
||||||
val, found := c.ServerKeys.Get(key)
|
val, found := c.ServerKeys.Get(key)
|
||||||
if found && val != nil {
|
if found && val != nil {
|
||||||
if keyLookupResult, ok := val.(gomatrixserverlib.PublicKeyLookupResult); ok {
|
if keyLookupResult, ok := val.(gomatrixserverlib.PublicKeyLookupResult); ok {
|
||||||
|
if !keyLookupResult.WasValidAt(now, true) {
|
||||||
|
// We appear to be past the key validity so don't return this
|
||||||
|
// with the results. This ensures that the cache doesn't return
|
||||||
|
// values that are not useful to us.
|
||||||
|
c.ServerKeys.Unset(key)
|
||||||
|
return gomatrixserverlib.PublicKeyLookupResult{}, false
|
||||||
|
}
|
||||||
return keyLookupResult, true
|
return keyLookupResult, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,4 +12,5 @@ type Caches struct {
|
|||||||
type Cache interface {
|
type Cache interface {
|
||||||
Get(key string) (value interface{}, ok bool)
|
Get(key string) (value interface{}, ok bool)
|
||||||
Set(key string, value interface{})
|
Set(key string, value interface{})
|
||||||
|
Unset(key string)
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,13 @@ func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
|
|||||||
c.lru.Add(key, value)
|
c.lru.Add(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *InMemoryLRUCachePartition) Unset(key string) {
|
||||||
|
if !c.mutable {
|
||||||
|
panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %q", key))
|
||||||
|
}
|
||||||
|
c.lru.Remove(key)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool) {
|
func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool) {
|
||||||
return c.lru.Get(key)
|
return c.lru.Get(key)
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing {
|
|||||||
// and keeping the cache up-to-date.
|
// and keeping the cache up-to-date.
|
||||||
return &gomatrixserverlib.KeyRing{
|
return &gomatrixserverlib.KeyRing{
|
||||||
KeyDatabase: s,
|
KeyDatabase: s,
|
||||||
KeyFetchers: []gomatrixserverlib.KeyFetcher{s},
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,15 +45,17 @@ func (s *ServerKeyAPI) FetchKeys(
|
|||||||
// because the caller gives up waiting.
|
// because the caller gives up waiting.
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
||||||
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
||||||
// First consult our local database and see if we have the requested
|
// First consult our local database and see if we have the requested
|
||||||
// keys. These might come from a cache, depending on the database
|
// keys. These might come from a cache, depending on the database
|
||||||
// implementation used.
|
// implementation used.
|
||||||
now := gomatrixserverlib.AsTimestamp(time.Now())
|
|
||||||
if dbResults, err := s.OurKeyRing.KeyDatabase.FetchKeys(ctx, requests); err == nil {
|
if dbResults, err := s.OurKeyRing.KeyDatabase.FetchKeys(ctx, requests); err == nil {
|
||||||
// We successfully got some keys. Add them to the results and
|
// We successfully got some keys. Add them to the results and
|
||||||
// remove them from the request list.
|
// remove them from the request list.
|
||||||
for req, res := range dbResults {
|
for req, res := range dbResults {
|
||||||
if now > res.ValidUntilTS && res.ExpiredTS == gomatrixserverlib.PublicKeyNotExpired {
|
if !res.WasValidAt(now, true) {
|
||||||
|
// We appear to be past the key validity. Don't return this
|
||||||
|
// key with the results.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
results[req] = res
|
results[req] = res
|
||||||
@ -71,6 +73,11 @@ func (s *ServerKeyAPI) FetchKeys(
|
|||||||
// We successfully got some keys. Add them to the results and
|
// We successfully got some keys. Add them to the results and
|
||||||
// remove them from the request list.
|
// remove them from the request list.
|
||||||
for req, res := range fetcherResults {
|
for req, res := range fetcherResults {
|
||||||
|
if !res.WasValidAt(now, true) {
|
||||||
|
// We appear to be past the key validity. Don't return this
|
||||||
|
// key with the results.
|
||||||
|
continue
|
||||||
|
}
|
||||||
results[req] = res
|
results[req] = res
|
||||||
delete(requests, req)
|
delete(requests, req)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/caching"
|
"github.com/matrix-org/dendrite/internal/caching"
|
||||||
internalHTTP "github.com/matrix-org/dendrite/internal/http"
|
internalHTTP "github.com/matrix-org/dendrite/internal/http"
|
||||||
@ -50,7 +49,7 @@ func (s *httpServerKeyInternalAPI) KeyRing() *gomatrixserverlib.KeyRing {
|
|||||||
// the other end of the API.
|
// the other end of the API.
|
||||||
return &gomatrixserverlib.KeyRing{
|
return &gomatrixserverlib.KeyRing{
|
||||||
KeyDatabase: s,
|
KeyDatabase: s,
|
||||||
KeyFetchers: []gomatrixserverlib.KeyFetcher{s},
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,12 +89,8 @@ func (s *httpServerKeyInternalAPI) FetchKeys(
|
|||||||
response := api.QueryPublicKeysResponse{
|
response := api.QueryPublicKeysResponse{
|
||||||
Results: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
|
Results: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
|
||||||
}
|
}
|
||||||
now := gomatrixserverlib.AsTimestamp(time.Now())
|
|
||||||
for req, ts := range requests {
|
for req, ts := range requests {
|
||||||
if res, ok := s.cache.GetServerKey(req); ok {
|
if res, ok := s.cache.GetServerKey(req); ok {
|
||||||
if now > res.ValidUntilTS && res.ExpiredTS == gomatrixserverlib.PublicKeyNotExpired {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
result[req] = res
|
result[req] = res
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/matrix-org/dendrite/internal"
|
"github.com/matrix-org/dendrite/internal"
|
||||||
"github.com/matrix-org/dendrite/internal/caching"
|
"github.com/matrix-org/dendrite/internal/caching"
|
||||||
"github.com/matrix-org/dendrite/serverkeyapi/api"
|
"github.com/matrix-org/dendrite/serverkeyapi/api"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,12 +34,7 @@ func AddRoutes(s api.ServerKeyInternalAPI, internalAPIMux *mux.Router, cache cac
|
|||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
store := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
|
if err := s.StoreKeys(req.Context(), request.Keys); err != nil {
|
||||||
for req, res := range request.Keys {
|
|
||||||
store[req] = res
|
|
||||||
cache.StoreServerKey(req, res)
|
|
||||||
}
|
|
||||||
if err := s.StoreKeys(req.Context(), store); err != nil {
|
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||||
|
Loading…
Reference in New Issue
Block a user